slash-commands: write commit status directly from /lgtm handler
/lgtm was adding the approved label and posting the bot review, but the merge-gate commit status stayed pending forever. Root cause: the status sub-action was designed to react to the labeled/unlabeled events emitted when main.js mutates labels, but main.js writes via GITHUB_TOKEN and GitHub suppresses workflow runs for GITHUB_TOKEN-triggered events. The event cascade never happened. Fix: main.js now calls createCommitStatus directly after each of the four label mutations (/lgtm, /lgtm cancel, /hold, /unhold), computing the new state from the pre-mutation label snapshot so cross-label cases (lgtm while held, unhold while approved) resolve correctly. The status sub-action is reduced to setting pending on open and on push. It no longer reads the approved label at all — that branch is removed, and the approved-label input with it. This closes a bypass: previously anyone with triage permissions could manually add the approved label, and on the resulting labeled event the sub-action would set success. Now the only success writer is the auth-gated /lgtm path. Caller workflows should trim pull_request_target types to [opened, synchronize]. The labeled/unlabeled/reopened triggers are dead weight: labeled/unlabeled never fired for bot writes, and reopened would overwrite a valid success with pending (commit statuses persist on the SHA across close/reopen).
This commit is contained in:
parent
f3ad04958f
commit
332da5ae1a
1 changed files with 85 additions and 53 deletions
|
|
@ -1,10 +1,10 @@
|
|||
name: PR Slash Commands — Merge-Gate Status
|
||||
description: >-
|
||||
Sets a commit status on the PR head SHA based on approval/hold labels.
|
||||
Stays `pending` until the approved label is present, goes `failure` when the
|
||||
hold label is present, and `success` when approved-and-not-held. Use this
|
||||
instead of an exit-code-based status job so PRs show a yellow pending check
|
||||
(not a red failure) while awaiting /lgtm.
|
||||
Sets the initial pending commit status on the PR head SHA at open time and
|
||||
forces it back to pending on push (new SHA, approval invalidated). Never
|
||||
sets success — only the main slash-commands action's auth-gated /lgtm
|
||||
handler may do that, so a manually-added approved label cannot bypass the
|
||||
merge gate. Sets failure if the hold label is present.
|
||||
|
||||
inputs:
|
||||
github-token:
|
||||
|
|
@ -13,10 +13,6 @@ inputs:
|
|||
this token `statuses: write`.
|
||||
required: false
|
||||
default: ${{ github.token }}
|
||||
approved-label:
|
||||
description: Label that marks the PR as approved (set by /lgtm)
|
||||
required: false
|
||||
default: approved
|
||||
hold-label:
|
||||
description: Label that blocks merging (set by /hold)
|
||||
required: false
|
||||
|
|
@ -30,17 +26,14 @@ inputs:
|
|||
invalidate-on-push:
|
||||
description: >-
|
||||
When 'true', a push (synchronize event) forces the status to pending
|
||||
regardless of labels. This must match the main slash-commands action's
|
||||
invalidate-on-push setting. Without this, on a push the event payload
|
||||
still contains the pre-push label state, so this action would set
|
||||
success on the new SHA (racing with the handle job removing the label)
|
||||
and auto-merge can slip through.
|
||||
regardless of labels. Must match the main slash-commands action's
|
||||
invalidate-on-push setting.
|
||||
required: false
|
||||
default: 'true'
|
||||
|
||||
outputs:
|
||||
state:
|
||||
description: The commit status state that was set (pending, success, failure)
|
||||
description: The commit status state that was set (pending or failure)
|
||||
value: ${{ steps.set.outputs.state }}
|
||||
|
||||
runs:
|
||||
|
|
@ -50,7 +43,6 @@ runs:
|
|||
id: set
|
||||
uses: actions/github-script@v8
|
||||
env:
|
||||
APPROVED_LABEL: ${{ inputs.approved-label }}
|
||||
HOLD_LABEL: ${{ inputs.hold-label }}
|
||||
STATUS_CONTEXT: ${{ inputs.status-context }}
|
||||
INVALIDATE_ON_PUSH: ${{ inputs.invalidate-on-push }}
|
||||
|
|
@ -64,32 +56,25 @@ runs:
|
|||
return;
|
||||
}
|
||||
const sha = pr.head.sha;
|
||||
const approvedLabel = process.env.APPROVED_LABEL;
|
||||
const holdLabel = process.env.HOLD_LABEL;
|
||||
const statusContext = process.env.STATUS_CONTEXT;
|
||||
const invalidateOnPush = process.env.INVALIDATE_ON_PUSH === 'true';
|
||||
|
||||
// On push, the event payload's labels reflect the PRE-push state.
|
||||
// If invalidation is on, the handle job is about to (or already did)
|
||||
// remove the approved label in parallel — but we can't see that here.
|
||||
// Force pending so auto-merge can't slip through on a stale success.
|
||||
// The subsequent unlabeled event will re-evaluate (and stay pending).
|
||||
// Never set success here. The only success writer is the main
|
||||
// action's /lgtm handler, which is auth-gated. Reading the approved
|
||||
// label would let anyone with triage+ perms bypass auth by labeling
|
||||
// manually. This sub-action's job is: pending on open, pending on
|
||||
// push (new SHA needs fresh approval), failure on hold.
|
||||
let state, description;
|
||||
if (invalidateOnPush && context.payload.action === 'synchronize') {
|
||||
state = 'pending';
|
||||
description = `New commits pushed — awaiting re-approval via /lgtm`;
|
||||
} else if (pr.labels.some(l => l.name === holdLabel)) {
|
||||
state = 'failure';
|
||||
description = `Blocked by ${holdLabel} — comment /unhold to clear`;
|
||||
} else {
|
||||
const labels = pr.labels.map(l => l.name);
|
||||
if (labels.includes(holdLabel)) {
|
||||
state = 'failure';
|
||||
description = `Blocked by ${holdLabel} — comment /unhold to clear`;
|
||||
} else if (labels.includes(approvedLabel)) {
|
||||
state = 'success';
|
||||
description = `Approved via /lgtm`;
|
||||
} else {
|
||||
state = 'pending';
|
||||
description = `Awaiting /lgtm from a maintainer or CODEOWNER`;
|
||||
}
|
||||
state = 'pending';
|
||||
description = `Awaiting /lgtm from a maintainer or CODEOWNER`;
|
||||
}
|
||||
|
||||
await github.rest.repos.createCommitStatus({
|
||||
|
|
|
|||
Loading…
Reference in a new issue