22# Determines the base branch for the current PR (if we are running in a PR).
33set -euo pipefail
44
5+ CURRENT_HEAD_SHA=" $( git rev-parse HEAD) "
6+ if [[ -z " ${CURRENT_HEAD_SHA:- } " ]]; then
7+ echo " Failed to determine current HEAD SHA" >&2
8+ exit 1
9+ fi
10+
11+ # 'workspace' is declared in the ci pipeline cache
12+ CACHE_PATH=workspace/find-gh-base-ref.cache
13+ save_cache () {
14+ local base_ref=" $1 "
15+ local head_sha=" $2 "
16+ mkdir -p workspace
17+ echo " CACHED_BASE_REF=${base_ref} " > " $CACHE_PATH "
18+ echo " CACHED_HEAD_SHA=${head_sha} " >> " $CACHE_PATH "
19+ }
20+
21+ # Get cached result (if HEAD commit matches)
22+ if [[ -f $CACHE_PATH ]]; then
23+ set -a
24+ source " $CACHE_PATH "
25+ set +a
26+ if [[ " $CURRENT_HEAD_SHA " == " ${CACHED_HEAD_SHA:- } " && -n " ${CACHED_BASE_REF:- } " ]]; then
27+ echo " Cache hit on $CACHE_PATH " >&2
28+ echo " $CACHED_BASE_REF "
29+ exit 0
30+ else
31+ echo " Cache miss on $CACHE_PATH " >&2
32+ fi
33+ fi
34+
535# Happy path: if we're just one commit away from master, base ref is master.
636if [[ $( git log --pretty=oneline origin/master..HEAD | wc -l) -eq 1 ]]; then
737 echo " We are just one commit away from master, base ref is master" >&2
38+ save_cache " master" " $CURRENT_HEAD_SHA "
839 echo " master"
940 exit 0
1041fi
@@ -17,14 +48,19 @@ if [[ -z "${CI_COMMIT_REF_NAME}" ]]; then
1748 exit 1
1849fi
1950
51+ # In GitLab, CI_PROJECT_NAME is set, otherwise, set it for testing.
52+ export CI_PROJECT_NAME=" ${CI_PROJECT_NAME:- dd-trace-java} "
53+
2054if [[ -z " ${GITHUB_TOKEN:- } " ]]; then
2155 echo " GITHUB_TOKEN is not set, fetching from AWS SSM" >&2
2256 if ! command -v aws > /dev/null 2>&1 ; then
2357 echo " aws is not installed, please install it" >&2
2458 exit 1
2559 fi
60+ set +e
2661 GITHUB_TOKEN=$( aws ssm get-parameter --name " ci.$CI_PROJECT_NAME .gh_release_token" --with-decryption --query " Parameter.Value" --output text)
27- if [[ -z " ${GITHUB_TOKEN} " ]]; then
62+ set -e
63+ if [[ -z " ${GITHUB_TOKEN:- } " ]]; then
2864 echo " Failed to fetch GITHUB_TOKEN from AWS SSM" >&2
2965 exit 1
3066 fi
@@ -57,9 +93,12 @@ while true; do
5793 if [[ ${exit_code} -eq 0 ]]; then
5894 PR_NUMBER=$( echo " $PR_DATA " | sed ' 1,/^[[:space:]]*$/d' | jq -r ' .[].number' )
5995 PR_BASE_REF=$( echo " $PR_DATA " | sed ' 1,/^[[:space:]]*$/d' | jq -r ' .[].base.ref' )
60- echo " PR is https://github.com/datadog/dd-trace-java/pull/${PR_NUMBER} and base ref is ${PR_BASE_REF} " >&2
61- echo " ${PR_BASE_REF} "
62- exit 0
96+ if [[ -n " ${PR_BASE_REF:- } " ]]; then
97+ echo " PR is https://github.com/datadog/dd-trace-java/pull/${PR_NUMBER} and base ref is ${PR_BASE_REF} " >&2
98+ save_cache " ${PR_BASE_REF} " " $CURRENT_HEAD_SHA "
99+ echo " ${PR_BASE_REF} "
100+ exit 0
101+ fi
63102 fi
64103 if echo " $PR_DATA " | grep -q " ^x-ratelimit-reset:" ; then
65104 reset_timestamp=$( echo -n " $PR_DATA " | grep " ^x-ratelimit-reset:" | sed -e ' s/^x-ratelimit-reset: //' -e ' s/\r//' )
@@ -70,7 +109,5 @@ while true; do
70109 continue
71110 fi
72111 echo -e " GitHub request failed for an unknown reason:\n$( echo " $PR_DATA " | sed ' /^$/q' ) " >&2
73- echo " Assuming base ref is master" >&2
74- echo " master"
75- exit 0
112+ exit 1
76113done
0 commit comments