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
33 changes: 33 additions & 0 deletions scripts/sync-policies.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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})`);
Expand Down
18 changes: 18 additions & 0 deletions scripts/test-sync-policies.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});