Skip to content

Commit

Permalink
Updated algorithm for finding the base branch
Browse files Browse the repository at this point in the history
DRY some near duplication.

Change variable name to be more consistent with Jenkins variables.

Skip-func-test-el9: false
Skip-func-test-leap15: false
Skip-func-hw-test: true
Skip-PR-comments: true
Test-tag: always_passes

Required-githooks: true

Signed-off-by: Brian J. Murrell <[email protected]>
  • Loading branch information
brianjmurrell committed Feb 5, 2024
1 parent b65ddba commit 245b43b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
11 changes: 6 additions & 5 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,20 @@ pipeline {
}
}
stage('Prepare Environment Variables') {
// TODO: Could/should these be moved to the environment block?
parallel {
stage('Get Commit Message') {
steps {
pragmasToEnv()
}
}
stage('Determine Base Branch') {
stage('Determine Release Branch') {
steps {
script {
env.BASE_BRANCH_NAME = sh(label: 'Determine base branch name',
script: 'utils/rpms/packaging/get_base_branch',
returnStdout: true).trim()
echo 'Base branch == ' + env.BASE_BRANCH_NAME
env.RELEASE_BRANCH = sh(label: 'Determine release branch name',
script: 'utils/rpms/packaging/get_release_branch',
returnStdout: true).trim()
echo 'Release branch == ' + env.RELEASE_BRANCH
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions utils/githooks/find_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi

# Try and use the gh command to work out the target branch, or if not installed
# then assume origin/master.
if command -v gh > /dev/null 2>&1; then
if ${USE_GH:-true} && command -v gh > /dev/null 2>&1; then
# If there is no PR created yet then do not check anything.
if ! TARGET="$ORIGIN"/$(gh pr view "$BRANCH" --json baseRefName -t "{{.baseRefName}}"); then
TARGET=""
Expand All @@ -36,19 +36,7 @@ if [ -z "$TARGET" ]; then
# as the target, calculated as the sum of the commits this branch is ahead and
# behind.
# check master, then current release branches, then current feature branches.
all_bases=("master" "release/2.4" "feature/cat_recovery" "feature/multiprovider")
TARGET="$ORIGIN/master"
min_diff=-1
for base in "${all_bases[@]}"; do
git rev-parse --verify "$ORIGIN/$base" &> /dev/null || continue
commits_ahead=$(git log --oneline "$ORIGIN/$base..HEAD" | wc -l)
commits_behind=$(git log --oneline "HEAD..$ORIGIN/$base" | wc -l)
commits_diff=$((commits_ahead + commits_behind))
if [ "$min_diff" -eq -1 ] || [ "$min_diff" -gt "$commits_diff" ]; then
TARGET="$ORIGIN/$base"
min_diff=$commits_diff
fi
done
TARGET="$ORIGIN/$(utils/rpms/packaging/get_release_branch "feature/cat_recovery feature/multiprovider")"
echo " Install gh command to auto-detect target branch, assuming $TARGET."
fi

Expand Down
19 changes: 0 additions & 19 deletions utils/rpms/packaging/get_base_branch

This file was deleted.

22 changes: 22 additions & 0 deletions utils/rpms/packaging/get_release_branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# find the base branch of the current branch

set -eux -o pipefail
IFS=' ' read -r -a add_bases <<< "${1:-}"
origin=origin
mapfile -t all_bases < <(echo "master"
git branch -r | sed -ne "/^ $origin\\/release\\/[0-9]/s/^ $origin\\///p")
all_bases+=("${add_bases[@]}")
TARGET="master"
min_diff=-1
for base in "${all_bases[@]}"; do
git rev-parse --verify "$origin/$base" &> /dev/null || continue
commits_ahead=$(git log --oneline "$origin/$base..HEAD" | wc -l)
if [ "$min_diff" -eq -1 ] || [ "$min_diff" -gt "$commits_ahead" ]; then
TARGET="$base"
min_diff=$commits_ahead
fi
done
echo "$TARGET"
exit 0

0 comments on commit 245b43b

Please sign in to comment.