Skip to content

Commit 0ca758d

Browse files
committed
Support project branches, move further logic within find-gh-base-ref.sh
1 parent a4c9d0b commit 0ca758d

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

.gitlab-ci.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,11 @@ default:
119119

120120
.gitlab_base_ref_params: &gitlab_base_ref_params
121121
- |
122-
if [[ ! $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then
123-
export GIT_BASE_REF=$(.gitlab/find-gh-base-ref.sh)
124-
if [[ -n "$GIT_BASE_REF" ]]; then
125-
export GRADLE_PARAMS="$GRADLE_PARAMS -PgitBaseRef=origin/$GIT_BASE_REF"
126-
else
127-
echo "Failed to find base ref for PR" >&2
128-
fi
122+
export GIT_BASE_REF=$(.gitlab/find-gh-base-ref.sh)
123+
if [[ -n "$GIT_BASE_REF" ]]; then
124+
export GRADLE_PARAMS="$GRADLE_PARAMS -PgitBaseRef=origin/$GIT_BASE_REF"
125+
else
126+
echo "Failed to find base ref for PR" >&2
129127
fi
130128
131129
.gradle_build: &gradle_build

.gitlab/find-gh-base-ref.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
# Determines the base branch for the current PR (if we are running in a PR).
33
set -euo pipefail
44

5+
if [[ -n "${CI_COMMIT_BRANCH:-}" ]]; then
6+
echo "CI_COMMIT_BRANCH is set to $CI_COMMIT_BRANCH" >&2
7+
else
8+
echo "CI_COMMIT_BRANCH is not set, skipping base ref detection" >&2
9+
exit 1
10+
fi
11+
12+
if [[ $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then
13+
echo "CI_COMMIT_BRANCH is a master or release branch, skipping base ref detection" >&2
14+
exit 1
15+
fi
16+
517
CURRENT_HEAD_SHA="$(git rev-parse HEAD)"
618
if [[ -z "${CURRENT_HEAD_SHA:-}" ]]; then
719
echo "Failed to determine current HEAD SHA" >&2
@@ -56,7 +68,15 @@ get_distance_from_merge_base() {
5668
# NOTE: GitHub API is more robust for this task, but we hit rate limits.
5769
BEST_CANDIDATES=(origin/master)
5870
BEST_DISTANCE=$(get_distance_from_merge_base origin/master)
59-
mapfile -t CANDIDATE_BASES < <(git branch -a --sort=committerdate --format='%(refname:short)' --list 'origin/release/v*' | tac)
71+
72+
# If the current branch is not a project/ branch, project/ branches are candidates.
73+
# This accounts for the case when the project/ branch is being merged to master.
74+
if [[ ! "$CI_COMMIT_BRANCH" =~ ^project/.*$ ]]; then
75+
mapfile -t CANDIDATE_BASES < <(git branch -a --sort=committerdate --format='%(refname:short)' --list 'origin/release/v*' --list 'origin/project/*' | tac)
76+
else
77+
mapfile -t CANDIDATE_BASES < <(git branch -a --sort=committerdate --format='%(refname:short)' --list 'origin/release/v*' | tac)
78+
fi
79+
6080
for candidate_base in "${CANDIDATE_BASES[@]}"; do
6181
distance=$(get_distance_from_merge_base "$candidate_base")
6282
if [[ $distance -lt $BEST_DISTANCE ]]; then

0 commit comments

Comments
 (0)