|
| 1 | +const authToken = process.env.CODE_FREEZE_TOKEN |
| 2 | + |
| 3 | +if (!authToken) { |
| 4 | + throw new Error(`missing CODE_FREEZE_TOKEN env`) |
| 5 | +} |
| 6 | + |
| 7 | +const codeFreezeRule = { |
| 8 | + context: 'Potentially publish release', |
| 9 | + app_id: 15368, |
| 10 | +} |
| 11 | + |
| 12 | +async function updateRules(newRules) { |
| 13 | + const res = await fetch( |
| 14 | + `https://api.github.com/repos/vercel/next.js/branches/canary/protection`, |
| 15 | + { |
| 16 | + method: 'PUT', |
| 17 | + headers: { |
| 18 | + Accept: 'application/vnd.github+json', |
| 19 | + Authorization: `Bearer ${authToken}`, |
| 20 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 21 | + }, |
| 22 | + body: JSON.stringify(newRules), |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + if (!res.ok) { |
| 27 | + throw new Error( |
| 28 | + `Failed to check for rule ${res.status} ${await res.text()}` |
| 29 | + ) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async function getCurrentRules() { |
| 34 | + const res = await fetch( |
| 35 | + `https://api.github.com/repos/vercel/next.js/branches/canary/protection`, |
| 36 | + { |
| 37 | + headers: { |
| 38 | + Accept: 'application/vnd.github+json', |
| 39 | + Authorization: `Bearer ${authToken}`, |
| 40 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 41 | + }, |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + if (!res.ok) { |
| 46 | + throw new Error( |
| 47 | + `Failed to check for rule ${res.status} ${await res.text()}` |
| 48 | + ) |
| 49 | + } |
| 50 | + const data = await res.json() |
| 51 | + |
| 52 | + return { |
| 53 | + required_status_checks: { |
| 54 | + strict: data.required_status_checks.strict, |
| 55 | + // checks: data.required_status_checks.checks, |
| 56 | + contexts: data.required_status_checks.contexts, |
| 57 | + }, |
| 58 | + enforce_admins: data.enforce_admins.enabled, |
| 59 | + required_pull_request_reviews: { |
| 60 | + dismiss_stale_reviews: |
| 61 | + data.required_pull_request_reviews.dismiss_stale_reviews, |
| 62 | + require_code_owner_reviews: |
| 63 | + data.required_pull_request_reviews.require_code_owner_reviews, |
| 64 | + require_last_push_approval: |
| 65 | + data.required_pull_request_reviews.require_last_push_approval, |
| 66 | + required_approving_review_count: |
| 67 | + data.required_pull_request_reviews.required_approving_review_count, |
| 68 | + }, |
| 69 | + restrictions: data.restrictions || { |
| 70 | + users: [], |
| 71 | + teams: [], |
| 72 | + apps: [], |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +async function main() { |
| 78 | + const typeIdx = process.argv.indexOf('--type') |
| 79 | + const type = process.argv[typeIdx + 1] |
| 80 | + |
| 81 | + if (type !== 'enable' && type !== 'disable') { |
| 82 | + throw new Error(`--type should be enable or disable`) |
| 83 | + } |
| 84 | + const isEnable = type === 'enable' |
| 85 | + const currentRules = await getCurrentRules() |
| 86 | + const hasRule = currentRules.required_status_checks.contexts?.some((ctx) => { |
| 87 | + return ctx === codeFreezeRule.context |
| 88 | + }) |
| 89 | + |
| 90 | + console.log(currentRules) |
| 91 | + |
| 92 | + if (isEnable) { |
| 93 | + if (hasRule) { |
| 94 | + console.log(`Already enabled`) |
| 95 | + return |
| 96 | + } |
| 97 | + currentRules.required_status_checks.contexts.push(codeFreezeRule.context) |
| 98 | + await updateRules(currentRules) |
| 99 | + console.log('Enabled code freeze') |
| 100 | + } else { |
| 101 | + if (!hasRule) { |
| 102 | + console.log(`Already disabled`) |
| 103 | + return |
| 104 | + } |
| 105 | + currentRules.required_status_checks.contexts = |
| 106 | + currentRules.required_status_checks.contexts.filter( |
| 107 | + (ctx) => ctx !== codeFreezeRule.context |
| 108 | + ) |
| 109 | + await updateRules(currentRules) |
| 110 | + console.log('Disabled code freeze') |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +main().catch((err) => { |
| 115 | + console.error(err) |
| 116 | + process.exit(1) |
| 117 | +}) |
0 commit comments