|
| 1 | +// Minimal replacement for @svitejs/changesets-changelog-github-compact (deprecated). |
| 2 | +// Formats changelog lines as "- summary (#pr)" / "- summary (commit-link)". |
| 3 | + |
| 4 | +function repoOf(options) { |
| 5 | + if (!options || !options.repo) { |
| 6 | + throw new Error( |
| 7 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["./utils/changesets-changelog.cjs", { "repo": "org/repo" }]' |
| 8 | + ); |
| 9 | + } |
| 10 | + return options.repo; |
| 11 | +} |
| 12 | + |
| 13 | +// ponytail: unauthenticated GitHub API calls are rate-limited to 60/hr; set GITHUB_TOKEN |
| 14 | +// in CI if changesets ever needs more commits' worth of PR lookups per release. |
| 15 | +async function commitPullLink(repo, commit) { |
| 16 | + const res = await fetch(`https://api.github.com/repos/${repo}/commits/${commit}/pulls`, { |
| 17 | + headers: { |
| 18 | + Accept: "application/vnd.github+json", |
| 19 | + ...(process.env.GITHUB_TOKEN ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } : {}) |
| 20 | + } |
| 21 | + }); |
| 22 | + if (!res.ok) return null; |
| 23 | + const [pr] = await res.json(); |
| 24 | + return pr ? `[#${pr.number}](${pr.html_url})` : null; |
| 25 | +} |
| 26 | + |
| 27 | +async function getReleaseLine(changeset, _type, options) { |
| 28 | + const repo = repoOf(options); |
| 29 | + const summary = changeset.summary.trim(); |
| 30 | + |
| 31 | + const pullLink = changeset.commit ? await commitPullLink(repo, changeset.commit) : null; |
| 32 | + const commitLink = changeset.commit |
| 33 | + ? `[\`${changeset.commit.slice(0, 7)}\`](https://github.com/${repo}/commit/${changeset.commit})` |
| 34 | + : null; |
| 35 | + const suffix = pullLink ? ` (${pullLink})` : commitLink ? ` (${commitLink})` : ""; |
| 36 | + |
| 37 | + return `\n- ${summary}${suffix}`; |
| 38 | +} |
| 39 | + |
| 40 | +async function getDependencyReleaseLine(changesets, dependenciesUpdated, options) { |
| 41 | + const repo = repoOf(options); |
| 42 | + if (dependenciesUpdated.length === 0) return ""; |
| 43 | + |
| 44 | + const commitLinks = changesets |
| 45 | + .filter((cs) => cs.commit) |
| 46 | + .map((cs) => `[\`${cs.commit.slice(0, 7)}\`](https://github.com/${repo}/commit/${cs.commit})`); |
| 47 | + |
| 48 | + const heading = `- Updated dependencies [${commitLinks.join(", ")}]:`; |
| 49 | + const deps = dependenciesUpdated.map((dep) => ` - ${dep.name}@${dep.newVersion}`); |
| 50 | + return [heading, ...deps].join("\n"); |
| 51 | +} |
| 52 | + |
| 53 | +module.exports = { getReleaseLine, getDependencyReleaseLine }; |
0 commit comments