1 name: Remove Automerge on New Commit
2
3 # When a new (non-merge) commit is pushed to a PR opened by a non-team
4 # contributor that already carries the `automerge` label, remove the label and
5 # ask the contributor to request a re-review. This prevents unreviewed changes
6 # from being merged automatically after a maintainer has approved an earlier
7 # revision.
8 #
9 # Uses `pull_request_target` (not `pull_request`) so the Medusa GitHub App
10 # credentials are available even for PRs opened from forks. No PR code is
11 # checked out or executed — the only checkout is the trusted base ref, used to
12 # read `.github/teams.yml`.
13
14 on:
15 pull_request_target:
16 types: [synchronize]
17
18 permissions:
19 contents: read
20
21 jobs:
22 remove-automerge:
23 name: Remove automerge label
24 runs-on: ubuntu-latest
25 # Only run when the PR already carries the automerge label. Team-membership
26 # and merge-commit checks happen in the "Evaluate conditions" step below.
27 if: contains(github.event.pull_request.labels.*.name, 'automerge')
28 steps:
29 - name: Checkout repository
30 uses: actions/checkout@v4
31 with:
32 fetch-depth: 1
33
34 - name: Evaluate conditions
35 id: check
36 env:
37 GH_TOKEN: ${{ github.token }}
38 run: |
39 set -euo pipefail
40
41 AUTHOR="${{ github.event.pull_request.user.login }}"
42
43 # Team members manage automerge on their own PRs; don't interfere.
44 if grep -qF "\"@${AUTHOR}\"" ./.github/teams.yml; then
45 echo "Author @${AUTHOR} is a Medusa team member; leaving automerge label."
46 echo "should_remove=false" >> "$GITHUB_OUTPUT"
47 exit 0
48 fi
49
50 # Ignore merge commits (e.g. merging the base branch into the PR
51 # branch). Those aren't new work from the contributor, so they
52 # shouldn't invalidate a maintainer's automerge decision.
53 PARENTS=$(gh api "repos/${{ github.repository }}/commits/${{ github.event.pull_request.head.sha }}" --jq '.parents | length')
54 if [ "$PARENTS" -gt 1 ]; then
55 echo "Head commit ${{ github.event.pull_request.head.sha }} is a merge commit; leaving automerge label."
56 echo "should_remove=false" >> "$GITHUB_OUTPUT"
57 exit 0
58 fi
59
60 echo "New non-merge commit from non-team contributor @${AUTHOR}; removing automerge label."
61 echo "should_remove=true" >> "$GITHUB_OUTPUT"
62
63 # Mint a short-lived token from the Medusa GitHub App so the label change
64 # and comment are attributed to the Medusa bot (same app used by the PR
65 # review / triage workflows).
66 - name: Generate GitHub App token
67 if: steps.check.outputs.should_remove == 'true'
68 id: app-token
69 uses: actions/create-github-app-token@v1
70 with:
71 app-id: ${{ secrets.MEDUSA_APP_ID }}
72 private-key: ${{ secrets.MEDUSA_APP_PRIVATE_KEY }}
73
74 - name: Remove automerge label
75 if: steps.check.outputs.should_remove == 'true'
76 env:
77 GH_TOKEN: ${{ steps.app-token.outputs.token }}
78 run: |
79 set -euo pipefail
80 gh pr edit "${{ github.event.pull_request.number }}" \
81 --repo "${{ github.repository }}" \
82 --remove-label "automerge"
83
84 - name: Comment on the PR
85 if: steps.check.outputs.should_remove == 'true'
86 env:
87 GH_TOKEN: ${{ steps.app-token.outputs.token }}
88 run: |
89 set -euo pipefail
90 # Write the body flush-left so no accidental leading whitespace turns
91 # a paragraph into a Markdown code block.
92 cat > automerge-comment.md <<'EOF'
93 The **automerge** label was removed because a new commit was pushed to this PR after the label was applied. Automerge is enabled once a maintainer has reviewed a specific revision, so new changes need to be looked at again before the PR can merge automatically.
94
95 Once your changes are ready, please request a re-review from a maintainer. A maintainer will re-apply the automerge label after the new commit has been reviewed.
96 EOF
97 gh pr comment "${{ github.event.pull_request.number }}" \
98 --repo "${{ github.repository }}" \
99 --body-file automerge-comment.md