File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments