Skip to content
Closed
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
124 changes: 110 additions & 14 deletions .github/scripts/native-queue-policy.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ const BLOCKED_LABEL = /^(hold|needs-human|release-in-progress|do-not-merge(?:\/.
const MAX_QUEUE_PAGES = 10;
const POLICY_CHECK_NAME = 'native-queue-policy';
const POLICY_CHECK_SOURCE = ['native-queue-policy.yml', ['pull_request_target']];
// Decision B on #17030: a v5→v6 forward-merge opened by v6-topup.yml carries
// commits that already passed v5's DCO gate, and probot DCO cannot be repaired
// PR-side. Only that exact shape may substitute prow's `dco` status.
const FORWARD_MERGE = {
head: 'sync/v5-to-v6', base: 'v6', source: 'v5',
author: { login: 'github-actions[bot]', id: 41898282, type: 'Bot' },
};
const PROW = { login: 'kubestellar-prow[bot]', id: 208851060, context: 'dco' };
// REST pull-request commit listing stops at 250; a longer list is unverifiable.
const MAX_PR_COMMITS = 250;
const PROBOT_DCO_PATH = 'DCO path: probot DCO (App 1861)';

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

Expand Down Expand Up @@ -148,15 +159,83 @@ async function assertActionsProvenance(github, repo, number, head, run, source,
}
}

async function assertEvidence(github, repo, number, head, checks) {
async function reachableFrom(github, repo, branch, commit) {
const { data } = await github.rest.repos.compareCommitsWithBasehead({
...repo, basehead: `${branch}...${commit}`,
});
return ['behind', 'identical'].includes(data.status);
}

// Returns null when the PR is not the top-up shape at all, otherwise whether
// the narrow prow path applies and why. Any doubt keeps probot DCO required.
async function forwardMergeDCOPath(github, repo, pr, head) {
const author = FORWARD_MERGE.author;
if (pr?.head?.ref !== FORWARD_MERGE.head || pr.base?.ref !== FORWARD_MERGE.base ||
pr.head.repo?.full_name !== `${repo.owner}/${repo.repo}` ||
pr.user?.login !== author.login || pr.user.id !== author.id || pr.user.type !== author.type) {
return null;
}
const commits = await github.paginate(github.rest.pulls.listCommits, {
...repo, pull_number: pr.number, per_page: 100,
});
const fallback = why => ({ prow: false, path: `${PROBOT_DCO_PATH}; ${FORWARD_MERGE.head} fallback: ${why}` });
if (commits.length === 0 || commits.length >= MAX_PR_COMMITS) {
return fallback(`commit list unverifiable (${commits.length} commits)`);
}
for (const commit of commits) {
if (!SHA.test(commit.sha)) return fallback('malformed commit list');
// The tip is v6-topup.yml's own `--no-ff` merge of origin/v5 onto v6, so it
// is never on v5 itself: its parents must be v6 and v5 history exactly.
if (commit.sha === head) {
const [ours, theirs] = (commit.parents || []).map(p => p.sha);
if (commit.parents?.length !== 2 || !SHA.test(ours) || !SHA.test(theirs) ||
!await reachableFrom(github, repo, FORWARD_MERGE.base, ours) ||
!await reachableFrom(github, repo, FORWARD_MERGE.source, theirs)) {
return fallback(`head ${head} is not a merge of ${FORWARD_MERGE.source} into ${FORWARD_MERGE.base}`);
}
} else if (!await reachableFrom(github, repo, FORWARD_MERGE.source, commit.sha)) {
return fallback(`${commit.sha} is not reachable from ${FORWARD_MERGE.source}`);
}
}
if (!commits.some(c => c.sha === head)) return fallback(`head ${head} is not in the commit list`);
return {
prow: true,
path: `DCO path: prow ${PROW.context} for ${FORWARD_MERGE.source}-reachable ${FORWARD_MERGE.head} forward-merge (#17030)`,
};
}

async function assertProwDCO(github, repo, head) {
const statuses = await github.paginate(github.rest.repos.listCommitStatusesForRef, {
...repo, ref: head, per_page: 100,
});
// Any workflow token can post a `dco` status; only prow's own is evidence.
const status = statuses
.filter(s => s.context === PROW.context && s.creator?.login === PROW.login && s.creator.id === PROW.id)
.sort((a, b) => b.id - a.id)[0];
if (!status) throw new PendingEvidence(`Missing prow ${PROW.context} on ${head}`);
if (status.state === 'pending') throw new PendingEvidence(`prow ${PROW.context} is pending`);
if (status.state !== 'success') throw new Error(`prow ${PROW.context}: ${status.state}`);
}

async function assertEvidence(github, repo, number, head, checks, pr) {
const dco = Object.hasOwn(checks, 'DCO') ? await forwardMergeDCOPath(github, repo, pr, head) : null;
const required = { ...checks };
if (dco?.prow) delete required.DCO;
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.
await assertActionsProvenance(github, repo, number, head, run, source, workflows);
try {
for (const { run, source } of evidenceForHead(runs, head, required)) {
if (!source) continue; // Probot DCO is deliberately not impersonated.
await assertActionsProvenance(github, repo, number, head, run, source, workflows);
}
if (dco?.prow) await assertProwDCO(github, repo, head);
} catch (error) {
if (dco && !(error instanceof ProviderUnavailable)) error.message += ` [${dco.path}]`;
throw error;
}
return dco?.path || PROBOT_DCO_PATH;
}

async function assertReviews(github, repo, number, head) {
Expand Down Expand Up @@ -237,10 +316,12 @@ function isCosmeticLabelFlip({ eventName, payload }) {
// 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.
// eligibility and the POLICY_CHECKS evidence. 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), or a policy provider is missing,
// still running or reporting UNKNOWN — 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;
Expand All @@ -259,7 +340,21 @@ async function replayLastVerdict({ github, context, currentCheckRunId }) {
// 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}`;
// The prior success is only valid while the providers it validated are
// still green: one re-run red on this head must not be papered over. A
// provider that is missing or still running is not a verdict either way;
// fall through so the caller runs the full polling wait instead of failing.
// A DCO enumeration UNKNOWN also falls through: the full wait re-raises it
// once and renders the runbook job summary, so operators see the same
// surface whichever event fired.
let path;
try {
path = await assertEvidence(github, repo, pr.number, pr.head.sha, POLICY_CHECKS, pr);
} catch (error) {
if (error instanceof PendingEvidence || error instanceof ProviderUnavailable) return null;
throw error;
}
return `PR #${pr.number}: replayed ${POLICY_CHECK_NAME} run ${prior.id} (success) on ${pr.head.sha}; ${path}`;
}

async function evaluateOnce({ github, context }) {
Expand All @@ -270,17 +365,18 @@ async function evaluateOnce({ github, context }) {
// Post-merge label/comment edits re-fire this event; a closed PR has nothing to gate.
if (pr.state === 'closed') return closedNoop(pr);
assertEligible(pr, event.head.sha, event.base.ref);
await assertEvidence(github, repo, pr.number, pr.head.sha, POLICY_CHECKS);
const path = await assertEvidence(github, repo, pr.number, pr.head.sha, POLICY_CHECKS, pr);
const { data: current } = await github.rest.pulls.get({ ...repo, pull_number: event.number });
if (current.state === 'closed') return closedNoop(current);
assertEligible(current, event.head.sha, event.base.ref);
return `PR #${pr.number}: five existing policy providers succeeded on ${pr.head.sha}`;
return `PR #${pr.number}: five existing policy providers succeeded on ${pr.head.sha}; ${path}`;
}
if (context.eventName !== 'merge_group' || context.payload.action !== 'checks_requested') {
throw new Error('Unsupported policy event');
}
const group = context.payload.merge_group;
const members = await readQueue(github, repo, group);
const paths = [];
for (const entry of members) {
const { number, headRefOid: head } = entry.pullRequest;
const { data: pr } = await github.rest.pulls.get({ ...repo, pull_number: number });
Expand All @@ -292,7 +388,7 @@ async function evaluateOnce({ github, context }) {
throw new Error(`#${number}: current PR head is not contained in the tested group`);
}
await assertReviews(github, repo, number, head);
await assertEvidence(github, repo, number, head, { ...POLICY_CHECKS, ...BUILD_CHECKS });
paths.push(`#${number} ${await assertEvidence(github, repo, number, head, { ...POLICY_CHECKS, ...BUILD_CHECKS }, pr)}`);
}
// Re-read both membership and mutable gates after all evidence reads. This
// bounds TOCTOU, but GitHub/Hive reconciliation is not an atomic transaction.
Expand All @@ -309,14 +405,14 @@ async function evaluateOnce({ github, context }) {
...repo, ref: group.head_ref.slice('refs/'.length),
});
if (ref.object?.sha !== group.head_sha) throw new Error('Synthetic group ref moved');
return `Group ${group.head_sha}: validated all ${members.length} member heads`;
return `Group ${group.head_sha}: validated all ${members.length} member heads (${paths.join('; ')})`;
}

module.exports = {
ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS,
PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate,
isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict, BLOCKED_LABEL,
ProviderUnavailable,
ProviderUnavailable, FORWARD_MERGE, PROW,
DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK,
DCO_PROVIDER_UNAVAILABLE_TEXT, DCO_PROVIDER_RUNBOOK,
};
135 changes: 133 additions & 2 deletions .github/scripts/native-queue-policy.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS,
PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate,
isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict,
ProviderUnavailable,
ProviderUnavailable, FORWARD_MERGE, PROW,
DCO_ENUMERATION_SUMMARY_PREFIX,
DCO_ENUMERATION_SUMMARY_FALLBACK,
DCO_PROVIDER_UNAVAILABLE_TEXT,
Expand Down Expand Up @@ -206,6 +206,99 @@ test('UNKNOWN reporting requires trusted exact-head completed DCO and the specif
error => !(error instanceof ProviderUnavailable) && error.message === 'DCO: failure');
});

// #17030: v6-topup.yml's PR whose probot DCO rejects a commit already on v5.
function forwardMergeFixture() {
const f = fixture('pull_request_target');
Object.assign(f.state.prs[0], {
head: { sha: sha(11), ref: 'sync/v5-to-v6', repo: { full_name: 'acme/widget' } },
base: { ref: 'v6' }, user: { ...FORWARD_MERGE.author },
});
f.context.payload.pull_request = clone(f.state.prs[0]);
f.state.runs.get(sha(11)).find(r => r.name === 'DCO').conclusion = 'action_required';
// sha(201) is the v5 commit probot rejects; sha(11) is the top-up merge of
// v5 (sha(201)) onto v6 (sha(300)).
f.state.commits = [
{ sha: sha(201), parents: [{ sha: sha(200) }] },
{ sha: sha(11), parents: [{ sha: sha(300) }, { sha: sha(201) }] },
];
f.state.reachable = new Set([`v5...${sha(201)}`, `v6...${sha(300)}`]);
f.state.statuses = [
{ id: 1, context: 'dco', state: 'success', creator: { login: PROW.login, id: PROW.id } },
];
f.github.rest.pulls.listCommits = 'commits';
f.github.rest.repos.listCommitStatusesForRef = 'statuses';
f.github.rest.repos.compareCommitsWithBasehead = async ({ basehead }) => {
f.state.seen.push(basehead);
return { data: { status: f.state.reachable.has(basehead) ? 'behind' : 'diverged' } };
};
const basePaginate = f.github.paginate;
f.github.paginate = async (method, args) => {
if (method === 'commits') return clone(f.state.commits);
if (method === 'statuses') return clone(f.state.statuses);
return basePaginate(method, args);
};
return f;
}

test('v5-reachable sync/v5-to-v6 forward-merge passes on prow dco without probot DCO', async () => {
const f = forwardMergeFixture();
assert.match(await evaluate(f),
/five existing policy providers succeeded.*DCO path: prow dco for v5-reachable sync\/v5-to-v6 forward-merge/);
assert.deepEqual(f.state.seen, [`v5...${sha(201)}`, `v6...${sha(300)}`, `v5...${sha(201)}`]);
});

test('one commit unreachable from v5 keeps the forward-merge on probot DCO', async () => {
const unreachable = forwardMergeFixture();
unreachable.state.reachable.delete(`v5...${sha(201)}`);
await assert.rejects(evaluate(unreachable), /DCO: action_required.*probot DCO.*not reachable from v5/);
const extra = forwardMergeFixture();
extra.state.commits.splice(1, 0, { sha: sha(202), parents: [{ sha: sha(201) }] });
await assert.rejects(evaluate(extra), new RegExp(`DCO: action_required.*${sha(202)} is not reachable from v5`));
for (const parents of [[{ sha: sha(201) }], [{ sha: sha(301) }, { sha: sha(201) }], [{ sha: sha(300) }, { sha: sha(202) }]]) {
const notMerge = forwardMergeFixture();
notMerge.state.commits[1].parents = parents;
await assert.rejects(evaluate(notMerge), /DCO: action_required.*is not a merge of v5 into v6/);
}
const truncated = forwardMergeFixture();
truncated.state.commits = Array.from({ length: 250 }, (_, i) => ({ sha: sha(1000 + i), parents: [] }));
await assert.rejects(evaluate(truncated), /DCO: action_required.*commit list unverifiable/);
// Probot DCO succeeding still admits through the unchanged path.
unreachable.state.runs.get(sha(11)).find(r => r.name === 'DCO').conclusion = 'success';
assert.match(await evaluate(unreachable), /DCO path: probot DCO \(App 1861\); sync\/v5-to-v6 fallback/);
});

test('another head branch, base, fork or author keeps requiring probot DCO', async () => {
for (const change of [
pr => { pr.head.ref = 'sync/v5-to-v6-extra'; },
pr => { pr.head.repo.full_name = 'fork/widget'; },
pr => { pr.user = { login: 'clubanderson', id: 1, type: 'User' }; },
pr => { pr.user.id = 1; },
pr => { pr.user.login = 'hivecommons-hive[bot]'; },
]) {
const f = forwardMergeFixture();
change(f.state.prs[0]);
await assert.rejects(evaluate(f), error => error.message === 'DCO: action_required');
assert.equal(f.state.seen.length, 0);
}
const v5 = forwardMergeFixture();
v5.state.prs[0].base.ref = 'v5';
v5.context.payload.pull_request.base.ref = 'v5';
await assert.rejects(evaluate(v5), error => error.message === 'DCO: action_required');
});

test('forward-merge path fails without a successful prow-created dco status', async () => {
const failed = forwardMergeFixture();
failed.state.statuses.push({ id: 2, context: 'dco', state: 'failure', creator: { login: PROW.login, id: PROW.id } });
await assert.rejects(evaluate(failed), /prow dco: failure.*DCO path: prow dco/);
// Any workflow token can post a `dco` status; it never stands in for prow.
const forged = forwardMergeFixture();
forged.state.statuses = [{ id: 3, context: 'dco', state: 'success', creator: { login: 'github-actions[bot]', id: 41898282 } }];
await assert.rejects(evaluate(forged), error => error instanceof PendingEvidence && /Missing prow dco/.test(error.message));
const pending = forwardMergeFixture();
pending.state.statuses[0].state = 'pending';
await assert.rejects(evaluate(pending), error => error instanceof PendingEvidence);
});

test('draft, closed, held and moved-head/base PRs cannot pass', () => {
for (const change of [
{ draft: true }, { state: 'closed' }, { head: { sha: sha(99) } }, { base: { ref: 'v6' } },
Expand Down Expand Up @@ -409,11 +502,16 @@ function replayFixture(priorRuns, labels = [], workflowOverride = {}) {
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: {
const baseGetWorkflowRun = f.github.rest.actions.getWorkflowRun;
const basePaginate = f.github.paginate;
f.github.rest.actions.getWorkflowRun = async ({ run_id: id }) => (id < 3000 ? baseGetWorkflowRun({ 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) => {
// Policy provider evidence comes from the base fixture (all green).
if (method === 'jobs' && args.run_id < 3000) return basePaginate(method, args);
if (method === 'checks' && !args.check_name) return basePaginate(method, args);
if (method === 'jobs') {
const run = runs.find(r => 3000 + r.id === args.run_id);
if (!run) return [];
Expand All @@ -440,6 +538,39 @@ test('cosmetic flip replays the latest completed success on the same head', asyn
assert.equal(f.state.listed[0].ref, sha(11));
});

test('cosmetic flip does not replay a success when a policy provider re-ran red on the head', async () => {
for (const name of Object.keys(POLICY_CHECKS)) {
const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }]);
f.state.runs.get(sha(11)).find(r => r.name === name).conclusion = 'failure';
await assert.rejects(replayLastVerdict(f), new RegExp(`${name}: failure`));
}
});

test('cosmetic flip falls through to the full wait while a policy provider is re-running or missing', async () => {
// PendingEvidence is not a verdict: returning null sends the caller to the
// polling wait instead of failing the check with no retry.
for (const name of Object.keys(POLICY_CHECKS)) {
for (const change of [{ status: 'queued', conclusion: null }, { status: 'in_progress', conclusion: null }]) {
const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }]);
Object.assign(f.state.runs.get(sha(11)).find(r => r.name === name), change);
assert.equal(await replayLastVerdict(f), null);
}
const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }]);
const runs = f.state.runs.get(sha(11));
runs.splice(runs.findIndex(r => r.name === name), 1);
assert.equal(await replayLastVerdict(f), null);
}
});

test('cosmetic flip falls through to the full wait when the DCO provider reports UNKNOWN', async () => {
// The full wait re-raises ProviderUnavailable once and renders the runbook
// job summary; failing here would skip that operator-facing surface.
const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }]);
Object.assign(f.state.runs.get(sha(11)).find(r => r.name === 'DCO'),
{ conclusion: 'failure', output: { summary: unavailableSummary } });
assert.equal(await replayLastVerdict(f), null);
});

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.
Expand Down
Loading
Loading