From 1e6f5a236ec84cb50c5bd2b8b6ef13b696de6b69 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Sat, 5 Dec 2026 07:07:57 +0800 Subject: [PATCH] feat(scripts): guard sync-policies against outdated PrGovernance pins Co-authored-by: Antigravity --- scripts/sync-policies.mjs | 33 +++++++++++++++++++++++++++++++++ scripts/test-sync-policies.cjs | 18 ++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/scripts/sync-policies.mjs b/scripts/sync-policies.mjs index 073dcc1..c59234d 100644 --- a/scripts/sync-policies.mjs +++ b/scripts/sync-policies.mjs @@ -14,11 +14,25 @@ import { fileURLToPath } from 'node:url'; const HERE = dirname(fileURLToPath(import.meta.url)); const POLICY = '.github/governance/policy.json'; +const RISK_GATE_WORKFLOW = '.github/workflows/risk-gate.yml'; const BRANCH = 'chore/policy-tier'; +const GOVERNANCE_REPO = 'CodeGovernanceLab/PrGovernance'; +export const MIN_POLICY_PIN_SHA = 'b540fc2eed7fa0a80c1a70b419581df2a49b0eec'; // The fields this script owns. Anything else in a policy file is left alone. export const FIELDS = ['blocking_severity', 'block_unresolved_threads', 'require_clearing_label_on_r2', 'require_owner_approval_on_r2']; +export function extractRiskGatePin(workflowContent) { + const match = String(workflowContent || '').match(new RegExp(`${GOVERNANCE_REPO}[^@\\s]*@([^\\s'"]+)`)); + return match ? match[1] : null; +} + +export function isPinAtLeast(pinnedSha, minSha, compareStatus = '') { + if (!pinnedSha || !/^[a-f0-9]{40}$/.test(pinnedSha)) return false; + if (pinnedSha === minSha) return true; + return compareStatus === 'ahead' || compareStatus === 'identical'; +} + export function applyTier(policy, tier) { const next = { ...policy }; for (const field of FIELDS) { @@ -73,6 +87,7 @@ function openPull(repo, base, title, body) { function main(argv) { const dryRun = argv.includes('--dry-run'); + const skipPinCheck = argv.includes('--skip-pin-check'); const orgIndex = argv.indexOf('--org'); const onlyOrg = orgIndex === -1 ? null : argv[orgIndex + 1]; const manifest = JSON.parse(readFileSync(join(HERE, 'policy-tiers.json'), 'utf8')); @@ -100,6 +115,24 @@ function main(argv) { skipped += 1; continue; } + if (!skipPinCheck) { + let pinned = null; + let status = ''; + try { + const wf = retry(() => api(`repos/${repo}/contents/${RISK_GATE_WORKFLOW}?ref=${base}`)); + pinned = extractRiskGatePin(Buffer.from(wf.content, 'base64').toString('utf8')); + if (pinned && /^[a-f0-9]{40}$/.test(pinned) && pinned !== MIN_POLICY_PIN_SHA) { + status = retry(() => api(`repos/${GOVERNANCE_REPO}/compare/${MIN_POLICY_PIN_SHA}...${pinned}`)).status || ''; + } + } catch { + status = ''; + } + if (!isPinAtLeast(pinned, MIN_POLICY_PIN_SHA, status)) { + console.log(`${repo}: skipped (pin ${pinned ? pinned.slice(0, 8) : 'missing'} predates ${MIN_POLICY_PIN_SHA.slice(0, 8)}; run sync-pins first)`); + skipped += 1; + continue; + } + } const next = applyTier(policy, tier); if (dryRun) { console.log(`${repo}: would set ${JSON.stringify(tier)} (${tierName})`); diff --git a/scripts/test-sync-policies.cjs b/scripts/test-sync-policies.cjs index c272c2b..657fe74 100644 --- a/scripts/test-sync-policies.cjs +++ b/scripts/test-sync-policies.cjs @@ -54,3 +54,21 @@ test('the manifest gives every repository a documented tier', () => { } assert.ok(Object.keys(manifest.repositories).length >= 40); }); + +test('extractRiskGatePin and isPinAtLeast guard against outdated PrGovernance pins', async () => { + const { extractRiskGatePin, isPinAtLeast, MIN_POLICY_PIN_SHA } = await load(); + assert.equal( + extractRiskGatePin(`uses: CodeGovernanceLab/PrGovernance/pr-governance@${MIN_POLICY_PIN_SHA} # v1`), + MIN_POLICY_PIN_SHA, + ); + assert.equal(extractRiskGatePin('uses: actions/checkout@abc'), null); + + const otherSha = 'c'.repeat(40); + assert.equal(isPinAtLeast(MIN_POLICY_PIN_SHA, MIN_POLICY_PIN_SHA, 'behind'), true); + assert.equal(isPinAtLeast(otherSha, MIN_POLICY_PIN_SHA, 'identical'), true); + assert.equal(isPinAtLeast(otherSha, MIN_POLICY_PIN_SHA, 'ahead'), true); + assert.equal(isPinAtLeast(otherSha, MIN_POLICY_PIN_SHA, 'behind'), false); + assert.equal(isPinAtLeast(otherSha, MIN_POLICY_PIN_SHA, 'diverged'), false); + assert.equal(isPinAtLeast('v1', MIN_POLICY_PIN_SHA, 'ahead'), false); + assert.equal(isPinAtLeast(null, MIN_POLICY_PIN_SHA, ''), false); +});