From fc2921c7e43d557c7f0fbcc737b497284b0102b7 Mon Sep 17 00:00:00 2001 From: Enrique Mingorance Cano Date: Mon, 1 Jun 2020 10:03:18 +0200 Subject: [PATCH 1/2] BXMSPROD-802 getPRAuthor() method --- vars/treebuild.groovy | 20 +++++++++++++++++++- vars/util.groovy | 1 - 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/vars/treebuild.groovy b/vars/treebuild.groovy index f42f8b20..6035acd0 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 = env.CHANGE_AUTHOR ?: ghprbPullAuthorLogin + def changeAuthor = getPRAuthor(name) def changeBranch = env.CHANGE_BRANCH ?: ghprbSourceBranch def changeTarget = env.CHANGE_TARGET ?: ghprbTargetBranch println "Checking out author [${changeAuthor}] branch [${changeBranch}] target [${changeTarget}]" @@ -106,4 +106,22 @@ 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 a6e453ae..a8e2086f 100644 --- a/vars/util.groovy +++ b/vars/util.groovy @@ -34,5 +34,4 @@ GIT INFORMATION REPORT } else { println '[WARNING] The variable GIT_INFORMATION_REPORT does not exist' } - } \ No newline at end of file From aa1845962171e449432b2f0149da16c369767ccd Mon Sep 17 00:00:00 2001 From: Enrique Mingorance Cano Date: Mon, 1 Jun 2020 10:48:23 +0200 Subject: [PATCH 2/2] BXMSPROD-802 checkoutIfExists mechanism improved --- vars/githubscm.groovy | 38 +++++++++++++++++++++++++++++--------- vars/treebuild.groovy | 20 +------------------- vars/util.groovy | 2 +- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/vars/githubscm.groovy b/vars/githubscm.groovy index 936b8081..b4b94a5d 100644 --- a/vars/githubscm.groovy +++ b/vars/githubscm.groovy @@ -4,7 +4,7 @@ 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, @@ -12,16 +12,17 @@ def resolveRepository(String repository, String author, String branches, boolean } 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 +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() @@ -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() +} 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 }