2
2
# Determines the base branch for the current PR (if we are running in a PR).
3
3
set -euo pipefail
4
4
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
+
5
35
# Happy path: if we're just one commit away from master, base ref is master.
6
36
if [[ $( git log --pretty=oneline origin/master..HEAD | wc -l) -eq 1 ]]; then
7
37
echo " We are just one commit away from master, base ref is master" >&2
38
+ save_cache " master" " $CURRENT_HEAD_SHA "
8
39
echo " master"
9
40
exit 0
10
41
fi
@@ -17,14 +48,19 @@ if [[ -z "${CI_COMMIT_REF_NAME}" ]]; then
17
48
exit 1
18
49
fi
19
50
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
+
20
54
if [[ -z " ${GITHUB_TOKEN:- } " ]]; then
21
55
echo " GITHUB_TOKEN is not set, fetching from AWS SSM" >&2
22
56
if ! command -v aws > /dev/null 2>&1 ; then
23
57
echo " aws is not installed, please install it" >&2
24
58
exit 1
25
59
fi
60
+ set +e
26
61
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
28
64
echo " Failed to fetch GITHUB_TOKEN from AWS SSM" >&2
29
65
exit 1
30
66
fi
@@ -57,9 +93,12 @@ while true; do
57
93
if [[ ${exit_code} -eq 0 ]]; then
58
94
PR_NUMBER=$( echo " $PR_DATA " | sed ' 1,/^[[:space:]]*$/d' | jq -r ' .[].number' )
59
95
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
63
102
fi
64
103
if echo " $PR_DATA " | grep -q " ^x-ratelimit-reset:" ; then
65
104
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
70
109
continue
71
110
fi
72
111
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
76
113
done
0 commit comments