Skip to content

Commit

Permalink
BXMSPROD-802 checkoutIfExists mechanism improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginxo committed Jun 1, 2020
1 parent fc2921c commit 7923920
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
40 changes: 29 additions & 11 deletions vars/githubscm.groovy
Expand Up @@ -4,24 +4,23 @@ 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],
[$class: 'org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait', strategyId: 1],
[$class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait', strategyId: 1, trust: [$class: 'TrustPermission']]]),
traits: [[$class: 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait', strategyId: 1]]),
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 +29,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 +73,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()
}
20 changes: 1 addition & 19 deletions vars/treebuild.groovy
Expand Up @@ -77,7 +77,7 @@ def checkoutProjects(List<String> projectCollection, String limitProject) {
* @param group project group
*/
def checkoutProject(String name, String group) {
def changeAuthor = getPRAuthor(name)
def changeAuthor = env.CHANGE_AUTHOR ?: ghprbPullAuthorLogin
def changeBranch = env.CHANGE_BRANCH ?: ghprbSourceBranch
def changeTarget = env.CHANGE_TARGET ?: ghprbTargetBranch
println "Checking out author [${changeAuthor}] branch [${changeBranch}] target [${changeTarget}]"
Expand Down Expand Up @@ -106,22 +106,4 @@ def getProjectGroupName(String project, String defaultGroup = "kiegroup") {
return [group, name]
}

/**
* Returns the change author
*
* @return the change author
*/
def getPRAuthor(String projectName) {
def author = env.CHANGE_AUTHOR ?: env.ghprbPullAuthorLogin
if(env.ghprbAuthorRepoGitUrl) {
def projectMatch = env.ghprbAuthorRepoGitUrl =~ /(github.com\/)([a-zA-Z0-9\-]*)\/([a-zA-Z0-9\-]*)/
if(projectName == projectMatch[0][3]){
println "[WARNING] author taken from ghprbAuthorRepoGitUrl:${ghprbAuthorRepoGitUrl}"
author = projectMatch[0][2]
}
}
println "[INFO] author: [${author}]"
return author
}

return this;
2 changes: 1 addition & 1 deletion 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

0 comments on commit 7923920

Please sign in to comment.