Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 76 additions & 31 deletions .github/scripts/native-queue-policy.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const BUILD_CHECKS = {
const SHA = /^[a-f0-9]{40}$/;
const BLOCKED_LABEL = /^(hold|needs-human|release-in-progress|do-not-merge(?:\/.*)?)$/i;
const MAX_QUEUE_PAGES = 10;
const POLICY_CHECK_NAME = 'native-queue-policy';
const POLICY_CHECK_SOURCE = ['native-queue-policy.yml', ['pull_request_target']];

const closedNoop = (pr) => `PR #${pr.number} is closed; nothing to evaluate`;

Expand Down Expand Up @@ -111,43 +113,49 @@ function evidenceForHead(runs, head, checks) {
});
}

// Every Actions workflow shares one App id, so a same-named check run proves
// nothing: bind it to the expected workflow file, event, head, suite and job.
async function assertActionsProvenance(github, repo, number, head, run, source, workflows) {
const url = new URL(run.details_url);
const prefix = `/${repo.owner}/${repo.repo}/actions/runs/`;
const id = url.pathname.startsWith(prefix) && url.pathname.slice(prefix.length).split('/')[0];
if (url.hostname !== 'github.com' || !/^[0-9]+$/.test(id)) {
throw new Error(`Invalid Actions provenance for ${run.name}`);
}
const runID = Number(id);
if (!workflows.has(runID)) {
const { data: workflow } = await github.rest.actions.getWorkflowRun({ ...repo, run_id: runID });
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
...repo, run_id: runID, per_page: 100, filter: 'latest',
});
workflows.set(runID, { workflow, jobs });
}
const { workflow, jobs } = workflows.get(runID);
const [file, events] = source;
if (workflow.path !== `.github/workflows/${file}` || !events.includes(workflow.event) ||
workflow.head_sha !== head || workflow.id !== runID) {
throw new Error(`Unexpected workflow/event/head for ${run.name} on #${number}`);
}
// details_url is only a locator. The API-owned suite and job identities
// must independently bind this exact check run to the expected workflow.
const checkURL = `https://api.github.com/repos/${repo.owner}/${repo.repo}/check-runs/${run.id}`;
if (!Number.isSafeInteger(run.check_suite?.id) ||
run.check_suite.id !== workflow.check_suite_id ||
!jobs.some(job => job.run_id === runID && job.name === run.name &&
job.head_sha === head && job.check_run_url === checkURL &&
job.status === 'completed' && job.conclusion === run.conclusion)) {
throw new Error(`Check suite/job identity mismatch for ${run.name} on #${number}`);
}
}

async function assertEvidence(github, repo, number, head, checks) {
const runs = await github.paginate(github.rest.checks.listForRef, {
...repo, ref: head, per_page: 100, filter: 'latest',
});
const workflows = new Map();
for (const { run, source } of evidenceForHead(runs, head, checks)) {
if (!source) continue; // Probot DCO is deliberately not impersonated.
const url = new URL(run.details_url);
const prefix = `/${repo.owner}/${repo.repo}/actions/runs/`;
const id = url.pathname.startsWith(prefix) && url.pathname.slice(prefix.length).split('/')[0];
if (url.hostname !== 'github.com' || !/^[0-9]+$/.test(id)) {
throw new Error(`Invalid Actions provenance for ${run.name}`);
}
const runID = Number(id);
if (!workflows.has(runID)) {
const { data: workflow } = await github.rest.actions.getWorkflowRun({ ...repo, run_id: runID });
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
...repo, run_id: runID, per_page: 100, filter: 'latest',
});
workflows.set(runID, { workflow, jobs });
}
const { workflow, jobs } = workflows.get(runID);
const [file, events] = source;
if (workflow.path !== `.github/workflows/${file}` || !events.includes(workflow.event) ||
workflow.head_sha !== head || workflow.id !== runID) {
throw new Error(`Unexpected workflow/event/head for ${run.name} on #${number}`);
}
// details_url is only a locator. The API-owned suite and job identities
// must independently bind this exact check run to the expected workflow.
const checkURL = `https://api.github.com/repos/${repo.owner}/${repo.repo}/check-runs/${run.id}`;
if (!Number.isSafeInteger(run.check_suite?.id) ||
run.check_suite.id !== workflow.check_suite_id ||
!jobs.some(job => job.run_id === runID && job.name === run.name &&
job.head_sha === head && job.check_run_url === checkURL &&
job.status === 'completed' && job.conclusion === run.conclusion)) {
throw new Error(`Check suite/job identity mismatch for ${run.name} on #${number}`);
}
await assertActionsProvenance(github, repo, number, head, run, source, workflows);
}
}

Expand Down Expand Up @@ -217,6 +225,43 @@ async function evaluate(args) {
}
}

// A labeled/unlabeled event only matters when the label is one assertEligible
// reads (BLOCKED_LABEL); every other flip cannot change the verdict.
function isCosmeticLabelFlip({ eventName, payload }) {
return eventName === 'pull_request_target' &&
['labeled', 'unlabeled'].includes(payload.action) &&
typeof payload.label?.name === 'string' &&
!BLOCKED_LABEL.test(payload.label.name);
}

// A cosmetic flip skips the evidence wait but must not mint a fresh passing
// required check: replay the latest completed `success` run of this check on
// the head (rejecting one not from this workflow) and still read live PR
// eligibility. Returns null when there is nothing to replay — no completed
// run, or the latest one did not succeed (a failed, cancelled or timed-out
// wait) — so the caller falls through to the full evidence wait instead of
// pinning that earlier verdict onto every later stage-label flip.
async function replayLastVerdict({ github, context, currentCheckRunId }) {
const repo = context.repo;
const event = context.payload.pull_request;
const { data: pr } = await github.rest.pulls.get({ ...repo, pull_number: event.number });
if (pr.state === 'closed') return closedNoop(pr);
assertEligible(pr, event.head.sha, event.base.ref);
const runs = await github.paginate(github.rest.checks.listForRef, {
...repo, ref: pr.head.sha, check_name: POLICY_CHECK_NAME, per_page: 100, filter: 'all',
});
const prior = runs
.filter(r => r.name === POLICY_CHECK_NAME && r.status === 'completed' &&
r.head_sha === pr.head.sha && r.app?.id === ACTIONS_APP_ID && r.id !== currentCheckRunId)
.sort((a, b) => b.id - a.id)[0];
if (!prior) return null;
// A same-named job from any other workflow shares the Actions App id; only
// a run of this workflow file on this event may be replayed.
await assertActionsProvenance(github, repo, pr.number, pr.head.sha, prior, POLICY_CHECK_SOURCE, new Map());
if (prior.conclusion !== 'success') return null;
return `PR #${pr.number}: replayed ${POLICY_CHECK_NAME} run ${prior.id} (success) on ${pr.head.sha}`;
}

async function evaluateOnce({ github, context }) {
const repo = context.repo;
if (context.eventName === 'pull_request_target') {
Expand Down Expand Up @@ -270,7 +315,7 @@ async function evaluateOnce({ github, context }) {
module.exports = {
ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS,
PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate,
isTransientAPIError,
isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict, BLOCKED_LABEL,
ProviderUnavailable,
DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK,
DCO_PROVIDER_UNAVAILABLE_TEXT, DCO_PROVIDER_RUNBOOK,
Expand Down
106 changes: 105 additions & 1 deletion .github/scripts/native-queue-policy.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('node:assert/strict');
const {
ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS,
PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate,
isTransientAPIError,
isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict,
ProviderUnavailable,
DCO_ENUMERATION_SUMMARY_PREFIX,
DCO_ENUMERATION_SUMMARY_FALLBACK,
Expand Down Expand Up @@ -387,3 +387,107 @@ test('API failure is never interpreted as an empty successful member list', asyn
f.github.graphql = async () => { throw new Error('403 queue read forbidden'); };
await assert.rejects(evaluate(f), /403/);
});

test('isCosmeticLabelFlip is true only for unread labels on label events', () => {
const ctx = (eventName, action, label) => ({ eventName, payload: { action, ...(label && { label: { name: label } }) } });
assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled', 'size/L')), true);
assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'unlabeled', 'size/L')), true);
assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled', 'hold')), false);
assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled', 'do-not-merge/foo')), false);
assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'synchronize', 'size/L')), false);
assert.equal(isCosmeticLabelFlip(ctx('merge_group', 'checks_requested')), false);
assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled')), false);
});

function replayFixture(priorRuns, labels = [], workflowOverride = {}) {
const f = fixture('pull_request_target');
f.state.prs[0].labels = labels;
const head = sha(11);
f.state.listed = [];
const runs = clone(priorRuns).map(r => ({
name: 'native-queue-policy', head_sha: head, app: { id: ACTIONS_APP_ID },
check_suite: { id: 5000 + r.id },
details_url: `https://github.com/acme/widget/actions/runs/${3000 + r.id}/job/1`, ...r,
}));
f.github.rest.actions.getWorkflowRun = async ({ run_id: id }) => ({ data: {
id, check_suite_id: id + 2000, path: '.github/workflows/native-queue-policy.yml',
event: 'pull_request_target', head_sha: head, ...workflowOverride,
} });
f.github.paginate = async (method, args) => {
if (method === 'jobs') {
const run = runs.find(r => 3000 + r.id === args.run_id);
if (!run) return [];
return [{
id: 200000 + run.id, run_id: args.run_id, name: run.name, head_sha: head,
check_run_url: `https://api.github.com/repos/acme/widget/check-runs/${run.id}`,
status: 'completed', conclusion: run.conclusion,
}];
}
assert.equal(method, 'checks');
f.state.listed.push(args);
return clone(runs);
};
return f;
}

test('cosmetic flip replays the latest completed success on the same head', async () => {
const f = replayFixture([
{ id: 5, status: 'completed', conclusion: 'failure' },
{ id: 7, status: 'completed', conclusion: 'success' },
{ id: 9, status: 'in_progress', conclusion: null },
]);
assert.match(await replayLastVerdict(f), /run 7 \(success\)/);
assert.equal(f.state.listed[0].ref, sha(11));
});

test('cosmetic flip has nothing to replay without a prior success and falls through', async () => {
// A failed, cancelled (timed-out) or absent prior run must not be pinned
// onto every later stage-label flip: null sends the caller to the full wait.
assert.equal(await replayLastVerdict(replayFixture([
{ id: 5, status: 'completed', conclusion: 'success' },
{ id: 7, status: 'completed', conclusion: 'failure' },
])), null);
assert.equal(await replayLastVerdict(replayFixture([
{ id: 5, status: 'completed', conclusion: 'success' },
{ id: 7, status: 'completed', conclusion: 'cancelled' },
])), null);
assert.equal(await replayLastVerdict(replayFixture([
{ id: 9, status: 'in_progress', conclusion: null },
])), null);
assert.equal(await replayLastVerdict(replayFixture([])), null);
const own = replayFixture([{ id: 8, status: 'completed', conclusion: 'success' }]);
assert.equal(await replayLastVerdict({ ...own, currentCheckRunId: 8 }), null);
});

test('cosmetic flip still honours a live hold even after a prior success', async () => {
const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }], [{ name: 'hold' }]);
await assert.rejects(replayLastVerdict(f), /held/);
});

test('replay ignores runs with a different name, head_sha or app id', async () => {
for (const change of [
{ name: 'other-check' }, { head_sha: sha(99) }, { app: { id: DCO_APP_ID } }, { app: undefined },
]) {
assert.equal(
await replayLastVerdict(replayFixture([{ id: 7, status: 'completed', conclusion: 'success', ...change }])),
null);
}
});

test('replay rejects a same-named success run from another workflow', async () => {
const run = { id: 7, status: 'completed', conclusion: 'success' };
for (const override of [
{ path: '.github/workflows/forged.yml' },
{ event: 'pull_request' },
{ head_sha: sha(99) },
{ check_suite_id: 1 },
]) {
await assert.rejects(replayLastVerdict(replayFixture([run], [], override)), /Unexpected workflow|identity mismatch/);
}
await assert.rejects(replayLastVerdict(replayFixture([
{ ...run, details_url: 'https://example.com/acme/widget/actions/runs/3007/job/1' },
])), /Invalid Actions provenance/);
await assert.rejects(replayLastVerdict(replayFixture([
{ ...run, details_url: 'https://github.com/acme/widget/actions/runs/3008/job/1' },
])), /Unexpected workflow|identity mismatch/);
});
19 changes: 19 additions & 0 deletions .github/scripts/test-native-queue-workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ def test_owned_gate_retriggers_never_cancel_the_evidence_wait(self):
self.assertIn("github.event.pull_request.number", concurrency["group"])
self.assertIs(concurrency["cancel-in-progress"], False)

def test_cosmetic_label_flips_skip_the_evidence_wait(self):
# Only labels matching the policy's BLOCKED_LABEL are read; any other
# labeled/unlabeled flip must skip the 22.5 min wait (#16945) while
# the job still concludes successfully.
steps = workflow("native-queue-policy.yml")["jobs"]["policy"]["steps"]
gate = next(s for s in steps
if s.get("name") == "Replay last verdict for cosmetic label flips")
self.assertEqual(gate["id"], "label-flip")
self.assertIn("isCosmeticLabelFlip", gate["with"]["script"])
self.assertIn("replayLastVerdict", gate["with"]["script"])
# Nothing to replay (no prior success, or a transient API error) must
# fall through to the full wait rather than fail or fabricate a pass.
self.assertIn("replayed === null", gate["with"]["script"])
self.assertIn("isTransientAPIError(error)", gate["with"]["script"])
self.assertIn("cosmetic = false", gate["with"]["script"])
wait = next(s for s in steps if s.get("name", "").startswith("Validate exact-head evidence"))
self.assertIn("steps.label-flip.outputs.cosmetic != 'true'", wait["if"])
self.assertLess(steps.index(gate), steps.index(wait))

def test_dependabot_runs_the_same_trusted_pr_verifier(self):
verifier = workflow("pr-verifier.yml")
events = verifier.get("on", verifier.get(True))
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/native-queue-policy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,38 @@ jobs:
persist-credentials: false
- name: Test policy behavior
run: node --test .github/scripts/native-queue-policy.test.cjs .github/scripts/diagnose-dco-provider.test.cjs
- name: Replay last verdict for cosmetic label flips
id: label-flip
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const { isCosmeticLabelFlip, replayLastVerdict, isTransientAPIError } = require('./.github/scripts/native-queue-policy.cjs');
let cosmetic = isCosmeticLabelFlip(context);
if (cosmetic) {
core.info('Label "' + context.payload.label.name + '" is not read by the policy; replaying the last completed verdict instead of waiting for evidence.');
try {
const replayed = await replayLastVerdict({github, context});
if (replayed === null) {
// No prior success on this head (never evaluated, or the last
// wait failed/timed out): run the authoritative wait instead of
// pinning that verdict onto every later cosmetic flip.
core.info('No successful native-queue-policy run on this head to replay; running the full evidence wait.');
cosmetic = false;
} else {
core.info(replayed);
}
} catch (error) {
if (isTransientAPIError(error)) {
core.info('GitHub API ' + error.status + ' while replaying; running the full evidence wait (which retries) instead.');
cosmetic = false;
} else {
core.setFailed(error.message);
}
}
}
core.setOutput('cosmetic', String(cosmetic));
- name: Validate exact-head evidence (bounded wait for PR providers)
if: steps.label-flip.outputs.cosmetic != 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
Expand Down
1 change: 1 addition & 0 deletions changelog.d/fixed-16952.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `native-queue-policy` no longer spends its ~22-minute evidence wait on label flips it never reads (`size/*`, `dco-signoff`, `hive/claimed-by-*`, `copilot`, stage labels); instead it replays the latest successful `native-queue-policy` run on the same head rather than fabricating a pass, still applies the live hold/draft/head-move check, and falls through to the full exact-head evidence wait when there is no prior success to replay (the head was never evaluated, or the last wait failed or timed out) or GitHub returns a 5xx, so a stale red verdict is never pinned onto later cosmetic flips. Flips of `hold`, `needs-human`, `release-in-progress` and `do-not-merge` always run the full wait ([#16945](https://github.com/hivecommons/hive/issues/16945)).
10 changes: 9 additions & 1 deletion src/docs/native-merge-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ metadata, not a fabricated last-member PR event.
Label, edit, or unlabel events re-trigger this workflow and serialize behind
any in-flight run (the concurrency group is per-PR and never cancels
in-progress); the queued run re-reads mutable PR state after the prior run
finishes, preserving fail-closed semantics.
finishes, preserving fail-closed semantics. Labels the policy never reads
(`size/*`, `dco-signoff`, stage labels, ...) skip the wait: the job replays
the latest completed `native-queue-policy` `success` on the same head and
still checks the live PR for holds, draft and head moves. When there is no
prior success to replay (never evaluated, or the last wait failed or timed
out) or GitHub returns a 5xx, the job falls through to the full wait so a
stale red verdict is never pinned onto later cosmetic flips. `hold`,
`needs-human`, `release-in-progress` and `do-not-merge*` flips always get
the full wait.
- On a group it executes the event's trusted `base_sha` validator. It reads
the live queue prefix ending at the event's exact synthetic head,
verifies contiguous membership and the base/head commit chain, and checks
Expand Down
Loading