Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ default:
- |
if [[ ! $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then
export GIT_BASE_REF=$(.gitlab/find-gh-base-ref.sh)
export GRADLE_PARAMS="$GRADLE_PARAMS -PgitBaseRef=origin/$GIT_BASE_REF"
if [[ -n "$GIT_BASE_REF" ]]; then
export GRADLE_PARAMS="$GRADLE_PARAMS -PgitBaseRef=origin/$GIT_BASE_REF"
else
echo "Failed to find base ref for PR" >&2
fi
fi

.gradle_build: &gradle_build
Expand Down
51 changes: 44 additions & 7 deletions .gitlab/find-gh-base-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,40 @@
# Determines the base branch for the current PR (if we are running in a PR).
set -euo pipefail

CURRENT_HEAD_SHA="$(git rev-parse HEAD)"
if [[ -z "${CURRENT_HEAD_SHA:-}" ]]; then
echo "Failed to determine current HEAD SHA" >&2
exit 1
fi

# 'workspace' is declared in the ci pipeline cache
CACHE_PATH=workspace/find-gh-base-ref.cache
save_cache() {
local base_ref="$1"
local head_sha="$2"
mkdir -p workspace
echo "CACHED_BASE_REF=${base_ref}" > "$CACHE_PATH"
echo "CACHED_HEAD_SHA=${head_sha}" >> "$CACHE_PATH"
}

# Get cached result (if HEAD commit matches)
if [[ -f $CACHE_PATH ]]; then
set -a
source "$CACHE_PATH"
set +a
if [[ "$CURRENT_HEAD_SHA" == "${CACHED_HEAD_SHA:-}" && -n "${CACHED_BASE_REF:-}" ]]; then
echo "Cache hit on $CACHE_PATH" >&2
echo "$CACHED_BASE_REF"
exit 0
else
echo "Cache miss on $CACHE_PATH" >&2
fi
fi

# Happy path: if we're just one commit away from master, base ref is master.
if [[ $(git log --pretty=oneline origin/master..HEAD | wc -l) -eq 1 ]]; then
echo "We are just one commit away from master, base ref is master" >&2
save_cache "master" "$CURRENT_HEAD_SHA"
echo "master"
exit 0
fi
Expand All @@ -17,14 +48,19 @@ if [[ -z "${CI_COMMIT_REF_NAME}" ]]; then
exit 1
fi

# In GitLab, CI_PROJECT_NAME is set, otherwise, set it for testing.
export CI_PROJECT_NAME="${CI_PROJECT_NAME:-dd-trace-java}"

if [[ -z "${GITHUB_TOKEN:-}" ]]; then
echo "GITHUB_TOKEN is not set, fetching from AWS SSM" >&2
if ! command -v aws >/dev/null 2>&1; then
echo "aws is not installed, please install it" >&2
exit 1
fi
set +e
GITHUB_TOKEN=$(aws ssm get-parameter --name "ci.$CI_PROJECT_NAME.gh_release_token" --with-decryption --query "Parameter.Value" --output text)
if [[ -z "${GITHUB_TOKEN}" ]]; then
set -e
if [[ -z "${GITHUB_TOKEN:-}" ]]; then
echo "Failed to fetch GITHUB_TOKEN from AWS SSM" >&2
exit 1
fi
Expand Down Expand Up @@ -57,9 +93,12 @@ while true; do
if [[ ${exit_code} -eq 0 ]]; then
PR_NUMBER=$(echo "$PR_DATA" | sed '1,/^[[:space:]]*$/d' | jq -r '.[].number')
PR_BASE_REF=$(echo "$PR_DATA" | sed '1,/^[[:space:]]*$/d' | jq -r '.[].base.ref')
echo "PR is https://github.com/datadog/dd-trace-java/pull/${PR_NUMBER} and base ref is ${PR_BASE_REF}">&2
echo "${PR_BASE_REF}"
exit 0
if [[ -n "${PR_BASE_REF:-}" ]]; then
echo "PR is https://github.com/datadog/dd-trace-java/pull/${PR_NUMBER} and base ref is ${PR_BASE_REF}">&2
save_cache "${PR_BASE_REF}" "$CURRENT_HEAD_SHA"
echo "${PR_BASE_REF}"
exit 0
fi
fi
if echo "$PR_DATA" | grep -q "^x-ratelimit-reset:"; then
reset_timestamp=$(echo -n "$PR_DATA" | grep "^x-ratelimit-reset:" | sed -e 's/^x-ratelimit-reset: //' -e 's/\r//')
Expand All @@ -70,7 +109,5 @@ while true; do
continue
fi
echo -e "GitHub request failed for an unknown reason:\n$(echo "$PR_DATA" | sed '/^$/q')" >&2
echo "Assuming base ref is master" >&2
echo "master"
exit 0
exit 1
done