Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically pull new commit #10

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/main/groovy/com/rundeck/plugin/GitManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class GitManager {
String gitURL

GitManager(Properties configuration) {
this.gitURL=configuration.getProperty(GitResourceModelFactory.GIT_URL)
this.gitURL = configuration.getProperty(GitResourceModelFactory.GIT_URL)
this.branch = configuration.getProperty(GitResourceModelFactory.GIT_BRANCH)
this.fileName=configuration.getProperty(GitResourceModelFactory.GIT_FILE)
this.strictHostKeyChecking=configuration.getProperty(GitResourceModelFactory.GIT_HOSTKEY_CHECKING)
this.fileName = configuration.getProperty(GitResourceModelFactory.GIT_FILE)
this.strictHostKeyChecking = configuration.getProperty(GitResourceModelFactory.GIT_HOSTKEY_CHECKING)

}

Expand All @@ -59,17 +59,19 @@ class GitManager {
def config = agit.getRepository().getConfig()
def found = config.getString("remote", REMOTE_NAME, "url")

def needsClone=false;
def needsClone = false;

if (found != this.gitURL) {
logger.debug("url differs, re-cloning ${found}!=${this.gitURL}")
needsClone = true
}else if (agit.repository.getFullBranch() != "refs/heads/$branch") {
} else if (agit.repository.getFullBranch() != "refs/heads/$branch") {
logger.debug("branch differs, re-cloning")
needsClone = true
} else {
performPull(agit)
}

if(needsClone){
if (needsClone) {
removeWorkdir(base)
performClone(base)
return
Expand Down Expand Up @@ -109,10 +111,32 @@ class GitManager {
repo = git.getRepository()
}


private void performPull(Git git) {

def pullCommand = git.pull()
.setRebase(true)

try {
setupTransportAuthentication(sshConfig, pullCommand, this.gitURL)
PullResult result = pullCommand.call()
if (!result.isSuccessful()) {
logger.info("Pull is not successful.")
} else {
logger.debug("Pull is not successful.")
}
} catch (Exception e) {
e.printStackTrace()
logger.debug("Failed pulling the repository from ${this.gitURL}: ${e.message}", e)
throw new Exception("Failed pulling the repository from ${this.gitURL}: ${e.message}", e)
}
repo = git.getRepository()
}

void setupTransportAuthentication(
Map<String, String> sshConfig,
TransportCommand command,
String url = null) throws Exception{
String url = null) throws Exception {
if (!url) {
url = command.repository.config.getString('remote', REMOTE_NAME, 'url')
}
Expand All @@ -122,15 +146,15 @@ class GitManager {

URIish u = new URIish(url);
logger.debug("transport url ${u}, scheme ${u.scheme}, user ${u.user}")
if ((u.scheme == null || u.scheme == 'ssh') && u.user && (sshPrivateKeyPath||sshPrivateKey) ) {
if ((u.scheme == null || u.scheme == 'ssh') && u.user && (sshPrivateKeyPath || sshPrivateKey)) {

byte[] keyData
if (sshPrivateKeyPath) {
logger.debug("using ssh private key path ${sshPrivateKeyPath}")
Path path = Paths.get(sshPrivateKeyPath);
keyData = Files.readAllBytes(path);

}else if(sshPrivateKey){
} else if (sshPrivateKey) {
logger.debug("using ssh private key")
keyData = sshPrivateKey.getBytes()
}
Expand All @@ -149,11 +173,11 @@ class GitManager {

PullResult gitPull(Git git1 = null) {
def pullCommand = (git1 ?: git).pull().setRemote(REMOTE_NAME).setRemoteBranchName(branch)
setupTransportAuthentication(sshConfig,pullCommand)
setupTransportAuthentication(sshConfig, pullCommand)
pullCommand.call()
}

def gitCommitAndPush(){
def gitCommitAndPush() {

////PERFORM COMMIT
git.add()
Expand Down Expand Up @@ -187,7 +211,7 @@ class GitManager {
sb.append it.toString()
}

String message=""
String message = ""
def failedUpdates = updates.findAll { it.status != RemoteRefUpdate.Status.OK }
if (failedUpdates) {
message = "Some updates failed: " + failedUpdates
Expand All @@ -203,14 +227,14 @@ class GitManager {

File base = new File(localPath)

if(!base){
if (!base) {
base.mkdir()
}

//start the new repo, if the repo is create nothing will be done
this.cloneOrCreate(base)

File file = new File(localPath+"/"+fileName)
File file = new File(localPath + "/" + fileName)

//always perform a pull
//TODO: check if it is needed check for the repo status
Expand Down