Check Base Branch Before Merge #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Base Branch Before Merge | |
on: | |
pull_request_review: | |
types: [submitted] | |
jobs: | |
check-base-branch-before-merge: | |
if: github.event.review.state == 'APPROVED' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Check base branch | |
run: | | |
git fetch origin --prune | |
CURRENT_BRANCH=${{ github.head_ref }} | |
TARGET_BRANCH="${{ github.event.pull_request.base.ref }}" | |
echo $CURRENT_BRANCH | |
echo $TARGET_BRANCH | |
COMMON_ANCESTOR=$(git merge-base origin/$CURRENT_BRANCH origin/$TARGET_BRANCH) | |
COMMITS_BEHIND=$(git rev-list --count origin/$TARGET_BRANCH ^$COMMON_ANCESTOR) | |
if [ "$COMMITS_BEHIND" -eq 0 ]; then | |
echo "Your branch is up-to-date with the target branch." | |
elif [ "$COMMITS_BEHIND" -eq 1 ]; then | |
echo "Your branch is 1 commit behind the target branch." | |
exit 1 | |
else | |
echo "Your branch is $COMMITS_BEHIND commits behind the target branch." | |
exit 1 | |
fi |