Skip to content

Check Base Branch Before Merge #5

Check Base Branch Before Merge

Check Base Branch Before Merge #5

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.event.pull_request.head.ref }}
TARGET_BRANCH="${{ github.event.pull_request.base.ref }}"
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