Skip to content

Commit 7b3c3ab

Browse files
committed
CI: add an action to prever merge commits in a branch of an opened PR
1 parent 27e4f18 commit 7b3c3ab

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Detect Merge Commits
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
check-merge-commits:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 0 # Needed to access full commit history
15+
16+
- name: Fetch PR commits
17+
id: commits
18+
run: |
19+
echo "COMMITS<<EOF" >> $GITHUB_OUTPUT
20+
gh pr view ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --json commits --jq '.commits[].oid' >> $GITHUB_OUTPUT
21+
echo "EOF" >> $GITHUB_OUTPUT
22+
env:
23+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
25+
- name: Check for merge commits
26+
id: merge_check
27+
run: |
28+
has_merge_commits=0
29+
for sha in $(git rev-list origin/${{ github.event.pull_request.base.ref }}..HEAD); do
30+
parents=$(git rev-list --parents -n 1 "$sha" | wc -w)
31+
if [ "$parents" -gt 2 ]; then
32+
echo "Merge commit detected: $sha"
33+
has_merge_commits=1
34+
fi
35+
done
36+
echo "has_merge_commits=$has_merge_commits" >> $GITHUB_OUTPUT
37+
38+
- name: Comment on PR if merge commit is found
39+
if: steps.merge_check.outputs.has_merge_commits == '1'
40+
run: |
41+
gh pr comment ${{ github.event.pull_request.number }} \
42+
--body "❌ Merge commit(s) detected in this PR. Please rebase or squash before merging."
43+
env:
44+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
46+
- name: Fail if merge commits are present
47+
if: steps.merge_check.outputs.has_merge_commits == '1'
48+
run: |
49+
echo "Merge commits found in PR, aborting."
50+
exit 1

0 commit comments

Comments
 (0)