|
| 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, converted_to_draft] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + check-ahead-behind: |
| 14 | + if: ${{ !github.event.pull_request.draft }} |
| 15 | + runs-on: ubuntu-22.04 |
| 16 | + steps: |
| 17 | + - name: Checkout PR HEAD (full history) |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + ref: ${{ github.event.pull_request.head.sha }} |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Fetch base branch |
| 24 | + run: | |
| 25 | + git fetch origin ${{ github.event.pull_request.base.ref }} |
| 26 | +
|
| 27 | + - name: Compute ahead/behind |
| 28 | + id: ab |
| 29 | + shell: bash |
| 30 | + run: | |
| 31 | + BASE_SHA=${{ github.event.pull_request.base.sha }} |
| 32 | + read BEHIND AHEAD < <(git rev-list --left-right --count ${BASE_SHA}...HEAD) |
| 33 | + echo "head=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |
| 34 | + echo "base=${BASE_SHA}" >> $GITHUB_OUTPUT |
| 35 | + echo "behind=$BEHIND" >> $GITHUB_OUTPUT |
| 36 | + echo "ahead=$AHEAD" >> $GITHUB_OUTPUT |
| 37 | + echo "Behind: $BEHIND, Ahead: $AHEAD" |
| 38 | +
|
| 39 | + - name: Enforce rule |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + BASE=${{ steps.ab.outputs.base }} |
| 43 | + HEAD=${{ steps.ab.outputs.head}} |
| 44 | + BEHIND=${{ steps.ab.outputs.behind }} |
| 45 | + AHEAD=${{ steps.ab.outputs.ahead }} |
| 46 | + if [[ "$BEHIND" -ne 0 || "$AHEAD" -ne 1 ]]; then |
| 47 | + echo "PR must be exactly 1 commit ahead and 0 behind the base branch." |
| 48 | + echo "base=$BASE, HEAD=$HEAD" |
| 49 | + echo "Ahead=$AHEAD, Behind=$BEHIND" |
| 50 | + echo "Rebase and squash to fix." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + echo "OK: PR is exactly 1 ahead and 0 behind." |
| 54 | +
|
| 55 | + - name: Convert PR to draft on failure |
| 56 | + if: failure() |
| 57 | + uses: peter-evans/convert-to-draft@v2 |
0 commit comments