Skip to content

Commit d8aa796

Browse files
Enforce single commit PRs
Change: single-commit-pr
1 parent 84f9cc1 commit d8aa796

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# .github/workflows/pr-single-commit-up-to-date.yml
2+
name: Enforce single commit & up-to-date
3+
4+
on:
5+
pull_request:
6+
types: [opened, synchronize, reopened, ready_for_review]
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
check-ahead-behind:
13+
runs-on: ubuntu-22.04
14+
steps:
15+
- name: Checkout PR HEAD (full history)
16+
uses: actions/checkout@v4
17+
with:
18+
ref: ${{ github.event.pull_request.head.sha }}
19+
fetch-depth: 0
20+
21+
- name: Fetch base branch
22+
run: |
23+
git fetch origin ${{ github.event.pull_request.base.ref }}
24+
25+
- name: Compute ahead/behind
26+
id: ab
27+
shell: bash
28+
run: |
29+
BASE_SHA=${{ github.event.pull_request.base.sha }}
30+
read BEHIND AHEAD < <(git rev-list --left-right --count ${BASE_SHA}...HEAD)
31+
echo "head=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
32+
echo "base=${BASE_SHA}" >> $GITHUB_OUTPUT
33+
echo "behind=$BEHIND" >> $GITHUB_OUTPUT
34+
echo "ahead=$AHEAD" >> $GITHUB_OUTPUT
35+
echo "Behind: $BEHIND, Ahead: $AHEAD"
36+
37+
- name: Enforce rule
38+
shell: bash
39+
run: |
40+
BASE=${{ steps.ab.outputs.base }}
41+
HEAD=${{ steps.ab.outputs.head}}
42+
BEHIND=${{ steps.ab.outputs.behind }}
43+
AHEAD=${{ steps.ab.outputs.ahead }}
44+
if [[ "$BEHIND" -ne 0 || "$AHEAD" -ne 1 ]]; then
45+
echo "PR must be exactly 1 commit ahead and 0 behind the base branch."
46+
echo "base=$BASE, HEAD=$HEAD"
47+
echo "Ahead=$AHEAD, Behind=$BEHIND"
48+
echo "Rebase and squash to fix."
49+
exit 1
50+
fi
51+
echo "OK: PR is exactly 1 ahead and 0 behind."

0 commit comments

Comments
 (0)