|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import core from "@actions/core"; |
| 3 | +import { context, getOctokit } from "@actions/github"; |
| 4 | +import parseChangesetFile from "@changesets/parse"; |
| 5 | + |
| 6 | +if (require.main === module) { |
| 7 | + main().catch((err) => { |
| 8 | + console.error("Unexpected error:", err); |
| 9 | + process.exit(1); |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +async function main() { |
| 14 | + const majorChangesets = getMajorChangesets(); |
| 15 | + const breakingChangeLabel = core.getInput("label", { required: true }); |
| 16 | + const hasBreakingChangeLabel = await hasLabel(breakingChangeLabel); |
| 17 | + |
| 18 | + if (majorChangesets.length === 0) { |
| 19 | + if (hasBreakingChangeLabel) { |
| 20 | + core.error( |
| 21 | + `This PR is marked with the "${breakingChangeLabel}" but does not contain any major release changesets.` |
| 22 | + ); |
| 23 | + process.exit(1); |
| 24 | + } |
| 25 | + } else { |
| 26 | + if (!hasBreakingChangeLabel) { |
| 27 | + core.error( |
| 28 | + `This PR contains the following changesets that have major releases, but is not marked with the "${breakingChangeLabel}" label` |
| 29 | + ); |
| 30 | + majorChangesets.forEach((changeset) => { |
| 31 | + core.info( |
| 32 | + ` - "${changeset.file}": ${changeset.summary.split("\n")[0]}` |
| 33 | + ); |
| 34 | + }); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Returns an array of parsed changesets that contain at least one major release |
| 42 | + */ |
| 43 | +export function getMajorChangesets() { |
| 44 | + const files = JSON.parse( |
| 45 | + core.getInput("changes", { required: true }) |
| 46 | + ) as string[]; |
| 47 | + |
| 48 | + const changesets = files.map((file) => ({ |
| 49 | + file, |
| 50 | + ...parseChangesetFile(readFileSync(file, "utf-8")), |
| 51 | + })); |
| 52 | + return changesets.filter((parsed) => |
| 53 | + parsed.releases.some((r) => r.type === "major") |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Returns true of the current issue/pr has the given label |
| 59 | + */ |
| 60 | +export async function hasLabel(label: string) { |
| 61 | + const token = core.getInput("github_token", { required: true }); |
| 62 | + |
| 63 | + const octokit = getOctokit(token); |
| 64 | + const issue = await octokit.rest.issues.get({ |
| 65 | + ...context.repo, |
| 66 | + issue_number: context.issue.number, |
| 67 | + }); |
| 68 | + return !!issue.data.labels.find( |
| 69 | + (l) => (typeof l !== "string" && l.name === label) || l === label |
| 70 | + ); |
| 71 | +} |
0 commit comments