|
| 1 | +const asCount = (value) => { |
| 2 | + const parsed = Number(value); |
| 3 | + return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : 0; |
| 4 | +}; |
| 5 | + |
| 6 | +const normalizePullRequest = (pullRequest) => ({ |
| 7 | + headRefName: pullRequest?.headRefName ?? pullRequest?.head?.ref ?? "", |
| 8 | + baseRefName: pullRequest?.baseRefName ?? pullRequest?.base?.ref ?? "", |
| 9 | + mergedAt: pullRequest?.mergedAt ?? pullRequest?.merged_at ?? null, |
| 10 | + htmlUrl: pullRequest?.url ?? pullRequest?.html_url ?? "", |
| 11 | +}); |
| 12 | + |
| 13 | +export const isNormalStagingPromotion = (pullRequest) => { |
| 14 | + const normalized = normalizePullRequest(pullRequest); |
| 15 | + return normalized.baseRefName === "main" && normalized.headRefName === "staging" && Boolean(normalized.mergedAt); |
| 16 | +}; |
| 17 | + |
| 18 | +export const shouldOpenStagingDriftIssue = ({ driftCount, associatedPullRequests }) => { |
| 19 | + const count = asCount(driftCount); |
| 20 | + if (count === 0) { |
| 21 | + return { openIssue: false, reason: "no ancestry drift detected" }; |
| 22 | + } |
| 23 | + const pullRequests = Array.isArray(associatedPullRequests) ? associatedPullRequests : []; |
| 24 | + const stagingPromotion = pullRequests.map(normalizePullRequest).find(isNormalStagingPromotion); |
| 25 | + if (stagingPromotion) { |
| 26 | + return { |
| 27 | + openIssue: false, |
| 28 | + reason: `normal squash-merged staging->main promotion: ${stagingPromotion.htmlUrl || "associated PR"}`, |
| 29 | + }; |
| 30 | + } |
| 31 | + return { openIssue: true, reason: `${count} main commit(s) are not represented by a staging promotion PR` }; |
| 32 | +}; |
| 33 | + |
| 34 | +const writeGitHubOutput = async (entries) => { |
| 35 | + const outputPath = process.env.GITHUB_OUTPUT; |
| 36 | + const lines = Object.entries(entries).map(([key, value]) => `${key}=${value}`); |
| 37 | + if (!outputPath) { |
| 38 | + console.log(lines.join("\n")); |
| 39 | + return; |
| 40 | + } |
| 41 | + const { appendFileSync } = await import("node:fs"); |
| 42 | + appendFileSync(outputPath, `${lines.join("\n")}\n`); |
| 43 | +}; |
| 44 | + |
| 45 | +const fetchAssociatedPullRequests = async ({ token, repository, sha }) => { |
| 46 | + const [owner, repo] = repository.split("/"); |
| 47 | + if (!owner || !repo) throw new Error(`Invalid GITHUB_REPOSITORY: ${repository}`); |
| 48 | + const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/commits/${sha}/pulls`, { |
| 49 | + headers: { |
| 50 | + Accept: "application/vnd.github+json", |
| 51 | + Authorization: `Bearer ${token}`, |
| 52 | + "X-GitHub-Api-Version": "2022-11-28", |
| 53 | + }, |
| 54 | + }); |
| 55 | + if (!response.ok) { |
| 56 | + throw new Error(`GitHub API returned ${response.status} while reading associated PRs`); |
| 57 | + } |
| 58 | + return response.json(); |
| 59 | +}; |
| 60 | + |
| 61 | +export const runCli = async () => { |
| 62 | + const driftCount = process.env.DRIFT_COUNT ?? "0"; |
| 63 | + const token = process.env.GITHUB_TOKEN ?? ""; |
| 64 | + const repository = process.env.GITHUB_REPOSITORY ?? ""; |
| 65 | + const sha = process.env.GITHUB_SHA ?? ""; |
| 66 | + if (!token || !repository || !sha) { |
| 67 | + throw new Error("GITHUB_TOKEN, GITHUB_REPOSITORY, and GITHUB_SHA are required"); |
| 68 | + } |
| 69 | + |
| 70 | + const associatedPullRequests = await fetchAssociatedPullRequests({ token, repository, sha }); |
| 71 | + const result = shouldOpenStagingDriftIssue({ driftCount, associatedPullRequests }); |
| 72 | + await writeGitHubOutput({ |
| 73 | + open_issue: result.openIssue ? "true" : "false", |
| 74 | + reason: result.reason, |
| 75 | + }); |
| 76 | + console.log(`[staging-drift-policy] ${result.openIssue ? "open issue" : "skip issue"}: ${result.reason}`); |
| 77 | +}; |
| 78 | + |
| 79 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 80 | + runCli().catch((error) => { |
| 81 | + console.error(error); |
| 82 | + process.exit(1); |
| 83 | + }); |
| 84 | +} |
0 commit comments