diff --git a/vars/githubscm.groovy b/vars/githubscm.groovy index 936b8081..d1971d8d 100644 --- a/vars/githubscm.groovy +++ b/vars/githubscm.groovy @@ -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 } @@ -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() @@ -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() +} diff --git a/vars/treebuild.groovy b/vars/treebuild.groovy index 6035acd0..f42f8b20 100644 --- a/vars/treebuild.groovy +++ b/vars/treebuild.groovy @@ -77,7 +77,7 @@ def checkoutProjects(List 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}]" @@ -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; diff --git a/vars/util.groovy b/vars/util.groovy index a8e2086f..500f2e45 100644 --- a/vars/util.groovy +++ b/vars/util.groovy @@ -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 }