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

BXMSPROD-802 checkoutIfExists mechanism improved #29

Merged
merged 2 commits into from Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 29 additions & 9 deletions vars/githubscm.groovy
Expand Up @@ -4,24 +4,25 @@ def resolveRepository(String repository, String author, String branches, boolean
credentialsId: 'kie-ci',
repoOwner: author,
repository: repository,
traits: [[$class: 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait', strategyId: 1],
traits: [[$class: 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait', strategyId: 3],
[$class: 'org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait', strategyId: 1],
[$class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait', strategyId: 1, trust: [$class: 'TrustPermission']]]),
ignoreErrors: ignoreErrors,
targets: [branches])
}

def checkoutIfExists(String repository, String author, String branches, String defaultAuthor, String defaultBranches, boolean mergeTarget = false) {
def repositoryScm = null
try {
repositoryScm = resolveRepository(repository, author, branches, true)
} catch (Exception ex) {
echo 'Branches [' + branches + '] from repository ' + repository + ' not found in ' + author + ' organisation.'
echo 'Checking branches ' + defaultBranches + ' from organisation ' + defaultAuthor + ' instead.'
def sourceAuthor = author
// Checks source group and branch (for cases where the branch has been created in the author's forked project)
def repositoryScm = getRepositoryScm(repository, author, branches)
if (repositoryScm == null) {
// Checks target group and and source branch (for cases where the branch has been created in the target project itself
repositoryScm = getRepositoryScm(repository, defaultAuthor, branches)
sourceAuthor = repositoryScm ? defaultAuthor : author
}
if (repositoryScm != null) {
if(mergeTarget) {
mergeSourceIntoTarget(repository, author, branches, defaultAuthor, defaultBranches)
mergeSourceIntoTarget(repository, sourceAuthor, branches, defaultAuthor, defaultBranches)
} else {
checkout repositoryScm
}
Expand All @@ -30,8 +31,19 @@ def checkoutIfExists(String repository, String author, String branches, String d
}
}

def getRepositoryScm(String repository, String author, String branches) {
println "[INFO] Resolving repository ${repository} author ${author} branches ${branches}"
def repositoryScm = null
try {
repositoryScm = resolveRepository(repository, author, branches, true)
} catch (Exception ex) {
println "[WARNING] Branches [${branches}] from repository ${repository} not found in ${author} organisation."
}
return repositoryScm
}

def mergeSourceIntoTarget(String repository, String sourceAuthor, String sourceBranches, String targetAuthor, String targetBranches) {
println "Merging source [${repository}/${sourceAuthor}:${sourceBranches}] into target [${repository}/${targetAuthor}:${targetBranches}]..."
println "[INFO] Merging source [${repository}/${sourceAuthor}:${sourceBranches}] into target [${repository}/${targetAuthor}:${targetBranches}]..."
checkout(resolveRepository(repository, targetAuthor, targetBranches, false))
def targetCommit = getCommit()

Expand Down Expand Up @@ -63,3 +75,11 @@ Target: ${targetCommit}
def getCommit() {
return sh(returnStdout: true, script: 'git log --oneline -1').trim()
}

def getBranch() {
return sh(returnStdout: true, script: 'git branch --all --contains HEAD').trim()
}

def getRemoteInfo(String remoteName, String configName) {
return sh(returnStdout: true, script: "git config --get remote.${remoteName}.${configName}").trim()
}
3 changes: 1 addition & 2 deletions vars/util.groovy
Expand Up @@ -6,7 +6,7 @@
*/
def storeGitInformation(String projectName) {
def gitInformationReport = env.GIT_INFORMATION_REPORT ? "${env.GIT_INFORMATION_REPORT}; " : ""
gitInformationReport += "${projectName}=${githubscm.getCommit().replace(';', '').replace('=', '')}"
gitInformationReport += "${projectName}=${githubscm.getCommit().replace(';', '').replace('=', '')} Branch [${githubscm.getBranch().replace(';', '').replace('=', '')}] Remote [${githubscm.getRemoteInfo('origin', 'url').replace(';', '').replace('=', '')}]"
env.GIT_INFORMATION_REPORT = gitInformationReport
}

Expand Down Expand Up @@ -34,5 +34,4 @@ GIT INFORMATION REPORT
} else {
println '[WARNING] The variable GIT_INFORMATION_REPORT does not exist'
}

}