From 00d72432a24d7de6e37f3be8044d2116874d4493 Mon Sep 17 00:00:00 2001 From: Spencer Date: Wed, 27 May 2020 11:22:04 -0700 Subject: [PATCH] [ci/stats] fix merge base detection (#67030) Co-authored-by: spalger Co-authored-by: Elastic Machine --- .../run_failed_tests_reporter_cli.ts | 3 +- src/dev/ci_setup/checkout_sibling_es.sh | 2 +- src/dev/ci_setup/extract_bootstrap_cache.sh | 2 +- vars/getCheckoutInfo.groovy | 41 +++++++++++++++++++ vars/workers.groovy | 35 ++++++---------- 5 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 vars/getCheckoutInfo.groovy diff --git a/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts b/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts index b298c08f162bf3..3bcea44cf73b6c 100644 --- a/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts +++ b/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts @@ -49,8 +49,7 @@ export function runFailedTestsReporterCli() { } const isPr = !!process.env.ghprbPullId; - const isMasterOrVersion = - branch.match(/^(origin\/){0,1}master$/) || branch.match(/^(origin\/){0,1}\d+\.(x|\d+)$/); + const isMasterOrVersion = branch === 'master' || branch.match(/^\d+\.(x|\d+)$/); if (!isMasterOrVersion || isPr) { log.info('Failure issues only created on master/version branch jobs'); updateGithub = false; diff --git a/src/dev/ci_setup/checkout_sibling_es.sh b/src/dev/ci_setup/checkout_sibling_es.sh index 2eec99a690134f..915759d4214f9a 100755 --- a/src/dev/ci_setup/checkout_sibling_es.sh +++ b/src/dev/ci_setup/checkout_sibling_es.sh @@ -43,7 +43,7 @@ function checkout_sibling { fi cloneAuthor="elastic" - cloneBranch="${PR_SOURCE_BRANCH:-${GIT_BRANCH#*/}}" # GIT_BRANCH starts with the repo, i.e., origin/master + cloneBranch="$GIT_BRANCH" if clone_target_is_valid ; then return 0 fi diff --git a/src/dev/ci_setup/extract_bootstrap_cache.sh b/src/dev/ci_setup/extract_bootstrap_cache.sh index 4a5977a68169eb..fdd8bb6b904060 100755 --- a/src/dev/ci_setup/extract_bootstrap_cache.sh +++ b/src/dev/ci_setup/extract_bootstrap_cache.sh @@ -2,7 +2,7 @@ set -e -targetBranch="${PR_TARGET_BRANCH:-${GIT_BRANCH#*/}}" +targetBranch="${PR_TARGET_BRANCH:-$GIT_BRANCH}" bootstrapCache="$HOME/.kibana/bootstrap_cache/$targetBranch.tar" ### diff --git a/vars/getCheckoutInfo.groovy b/vars/getCheckoutInfo.groovy new file mode 100644 index 00000000000000..7a3a7a9d2eccc7 --- /dev/null +++ b/vars/getCheckoutInfo.groovy @@ -0,0 +1,41 @@ +def call(branchOverride) { + def repoInfo = [ + branch: branchOverride ?: env.ghprbSourceBranch, + targetBranch: env.ghprbTargetBranch, + ] + + if (repoInfo.branch == null) { + if (!(params.branch_specifier instanceof String)) { + throw new Exception( + "Unable to determine branch automatically, either pass a branch name to getCheckoutInfo() or use the branch_specifier param." + ) + } + + // strip prefix from the branch specifier to make it consistent with ghprbSourceBranch + repoInfo.branch = params.branch_specifier.replaceFirst(/^(refs\/heads\/|origin\/)/, "") + } + + repoInfo.commit = sh( + script: "git rev-parse HEAD", + label: "determining checked out sha", + returnStdout: true + ).trim() + + if (repoInfo.targetBranch) { + sh( + script: "git fetch origin ${repoInfo.targetBranch}", + label: "fetch latest from '${repoInfo.targetBranch}' at origin" + ) + repoInfo.mergeBase = sh( + script: "git merge-base HEAD FETCH_HEAD", + label: "determining merge point with '${repoInfo.targetBranch}' at origin", + returnStdout: true + ).trim() + } + + print "repoInfo: ${repoInfo}" + + return repoInfo +} + +return this diff --git a/vars/workers.groovy b/vars/workers.groovy index b4e4a115f20111..d2cc19787bc5f8 100644 --- a/vars/workers.groovy +++ b/vars/workers.groovy @@ -51,33 +51,24 @@ def base(Map params, Closure closure) { } } - def scmVars = [:] + def checkoutInfo = [:] if (config.scm) { // Try to clone from Github up to 8 times, waiting 15 secs between attempts retryWithDelay(8, 15) { - scmVars = checkout scm - - def mergeBase - if (env.ghprbTargetBranch) { - sh( - script: "cd kibana && git fetch origin ${env.ghprbTargetBranch}", - label: "update reference to target branch 'origin/${env.ghprbTargetBranch}'" - ) - mergeBase = sh( - script: "cd kibana && git merge-base HEAD FETCH_HEAD", - label: "determining merge point with target branch 'origin/${env.ghprbTargetBranch}'", - returnStdout: true - ).trim() - } + checkout scm + } - ciStats.reportGitInfo( - env.ghprbSourceBranch ?: scmVars.GIT_LOCAL_BRANCH ?: scmVars.GIT_BRANCH, - scmVars.GIT_COMMIT, - env.ghprbTargetBranch, - mergeBase - ) + dir("kibana") { + checkoutInfo = getCheckoutInfo() } + + ciStats.reportGitInfo( + checkoutInfo.branch, + checkoutInfo.commit, + checkoutInfo.targetBranch, + checkoutInfo.mergeBase + ) } withEnv([ @@ -87,7 +78,7 @@ def base(Map params, Closure closure) { "PR_TARGET_BRANCH=${env.ghprbTargetBranch ?: ''}", "PR_AUTHOR=${env.ghprbPullAuthorLogin ?: ''}", "TEST_BROWSER_HEADLESS=1", - "GIT_BRANCH=${scmVars.GIT_BRANCH ?: ''}", + "GIT_BRANCH=${checkoutInfo.branch}", ]) { withCredentials([ string(credentialsId: 'vault-addr', variable: 'VAULT_ADDR'),