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
|
|
@ -11,6 +11,7 @@ module.exports = async ({ github, context, core }) => {
|
|||
const codeownersPath = process.env.CODEOWNERS_PATH;
|
||||
const invalidateOnPush = process.env.INVALIDATE_ON_PUSH === 'true';
|
||||
const submitReview = process.env.SUBMIT_REVIEW === 'true';
|
||||
const statusContext = process.env.STATUS_CONTEXT;
|
||||
const reviewMarker = '<!-- slash-commands-lgtm -->';
|
||||
// `github` is authed with bot-token (defaults to GITHUB_TOKEN) so comments,
|
||||
// reactions, labels, and reviews show as github-actions[bot]. The PAT is
|
||||
|
|
@ -207,6 +208,28 @@ module.exports = async ({ github, context, core }) => {
|
|||
}
|
||||
}
|
||||
}
|
||||
// GITHUB_TOKEN-triggered label events do NOT create workflow runs, so the
|
||||
// status sub-action cannot react to our addLabel/removeLabel calls. Set the
|
||||
// commit status directly here. This is also the ONLY path that may set
|
||||
// state=success — the status sub-action deliberately never does, so that a
|
||||
// manually-added approved label (triage+ perms suffice) cannot bypass auth.
|
||||
async function setCommitStatus(sha, approved, held) {
|
||||
let state, description;
|
||||
if (held) {
|
||||
state = 'failure';
|
||||
description = `Blocked by ${holdLabel} — comment /unhold to clear`;
|
||||
} else if (approved) {
|
||||
state = 'success';
|
||||
description = `Approved via /lgtm`;
|
||||
} else {
|
||||
state = 'pending';
|
||||
description = `Awaiting /lgtm from a maintainer or CODEOWNER`;
|
||||
}
|
||||
await github.rest.repos.createCommitStatus({
|
||||
owner, repo, sha, state, description, context: statusContext,
|
||||
});
|
||||
core.info(`Set commit status ${state} on ${sha.slice(0,7)}: ${description}`);
|
||||
}
|
||||
|
||||
// --- Dispatch ---------------------------------------------------
|
||||
const ev = context.eventName;
|
||||
|
|
@ -244,6 +267,10 @@ module.exports = async ({ github, context, core }) => {
|
|||
const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber });
|
||||
if (pr.state !== 'open') return setResult('noop', actor);
|
||||
|
||||
const preLabels = new Set(pr.labels.map(l => l.name));
|
||||
const hadApproved = preLabels.has(approvedLabel);
|
||||
const hadHold = preLabels.has(holdLabel);
|
||||
|
||||
// Self-approval guard (only for /lgtm, not /hold)
|
||||
if (cmd === 'lgtm' && !cancel && actor.toLowerCase() === pr.user.login.toLowerCase()) {
|
||||
await react(commentId, '-1');
|
||||
|
|
@ -305,21 +332,25 @@ module.exports = async ({ github, context, core }) => {
|
|||
await addLabel(prNumber, approvedLabel);
|
||||
if (submitReview) await submitApproval(prNumber, actor);
|
||||
await react(commentId, '+1');
|
||||
await setCommitStatus(pr.head.sha, true, hadHold);
|
||||
return setResult('lgtm-added', actor);
|
||||
}
|
||||
if (cmd === 'lgtm' && cancel) {
|
||||
await removeLabel(prNumber, approvedLabel);
|
||||
await dismissBotApprovals(prNumber, `@${actor} cancelled approval via /lgtm cancel.`);
|
||||
await react(commentId, '+1');
|
||||
await setCommitStatus(pr.head.sha, false, hadHold);
|
||||
return setResult('lgtm-removed', actor);
|
||||
}
|
||||
if ((cmd === 'hold' && !cancel)) {
|
||||
await addLabel(prNumber, holdLabel);
|
||||
await react(commentId, '+1');
|
||||
await setCommitStatus(pr.head.sha, hadApproved, true);
|
||||
return setResult('hold-added', actor);
|
||||
}
|
||||
// /hold cancel or /unhold
|
||||
await removeLabel(prNumber, holdLabel);
|
||||
await react(commentId, '+1');
|
||||
await setCommitStatus(pr.head.sha, hadApproved, false);
|
||||
return setResult('hold-removed', actor);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue