|
| 1 | +import { getInfo, getInfoFromPullRequest } from "@changesets/get-github-info"; |
| 2 | + |
| 3 | +/** @typedef {import("@changesets/types").ChangelogFunctions} ChangelogFunctions */ |
| 4 | + |
| 5 | +/** |
| 6 | + * @returns {{ GITHUB_SERVER_URL: string }} value |
| 7 | + */ |
| 8 | +function readEnv() { |
| 9 | + const GITHUB_SERVER_URL = process.env.GITHUB_SERVER_URL || "https://github.com"; |
| 10 | + return { GITHUB_SERVER_URL }; |
| 11 | +} |
| 12 | + |
| 13 | +/** @type {ChangelogFunctions} */ |
| 14 | +const changelogFunctions = { |
| 15 | + getDependencyReleaseLine: async (changesets, dependenciesUpdated, options) => { |
| 16 | + if (!options.repo) { |
| 17 | + throw new Error( |
| 18 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]', |
| 19 | + ); |
| 20 | + } |
| 21 | + if (dependenciesUpdated.length === 0) return ""; |
| 22 | + |
| 23 | + const changesetLink = `- Updated dependencies [${( |
| 24 | + await Promise.all( |
| 25 | + changesets.map(async (cs) => { |
| 26 | + if (cs.commit) { |
| 27 | + const { links } = await getInfo({ |
| 28 | + repo: options.repo, |
| 29 | + commit: cs.commit, |
| 30 | + }); |
| 31 | + return links.commit; |
| 32 | + } |
| 33 | + }), |
| 34 | + ) |
| 35 | + ) |
| 36 | + .filter(Boolean) |
| 37 | + .join(", ")}]:`; |
| 38 | + |
| 39 | + const updatedDependenciesList = dependenciesUpdated.map( |
| 40 | + (dependency) => ` - ${dependency.name}@${dependency.newVersion}`, |
| 41 | + ); |
| 42 | + |
| 43 | + return [changesetLink, ...updatedDependenciesList].join("\n"); |
| 44 | + }, |
| 45 | + getReleaseLine: async (changeset, type, options) => { |
| 46 | + const { GITHUB_SERVER_URL } = readEnv(); |
| 47 | + if (!options || !options.repo) { |
| 48 | + throw new Error( |
| 49 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]', |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** @type {number | undefined} */ |
| 54 | + let prFromSummary; |
| 55 | + /** @type {string | undefined} */ |
| 56 | + let commitFromSummary; |
| 57 | + /** @type {string[]} */ |
| 58 | + const usersFromSummary = []; |
| 59 | + |
| 60 | + const replacedChangelog = changeset.summary |
| 61 | + .replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => { |
| 62 | + const num = Number(pr); |
| 63 | + if (!Number.isNaN(num)) prFromSummary = num; |
| 64 | + return ""; |
| 65 | + }) |
| 66 | + .replace(/^\s*commit:\s*([^\s]+)/im, (_, commit) => { |
| 67 | + commitFromSummary = commit; |
| 68 | + return ""; |
| 69 | + }) |
| 70 | + .replaceAll(/^\s*(?:author|user):\s*@?([^\s]+)/gim, (_, user) => { |
| 71 | + usersFromSummary.push(user); |
| 72 | + return ""; |
| 73 | + }) |
| 74 | + .trim(); |
| 75 | + |
| 76 | + const [firstLine, ...futureLines] = replacedChangelog.split("\n").map((l) => l.trimEnd()); |
| 77 | + |
| 78 | + const links = await (async () => { |
| 79 | + if (prFromSummary !== undefined) { |
| 80 | + let { links } = await getInfoFromPullRequest({ |
| 81 | + repo: options.repo, |
| 82 | + pull: prFromSummary, |
| 83 | + }); |
| 84 | + if (commitFromSummary) { |
| 85 | + const shortCommitId = commitFromSummary.slice(0, 7); |
| 86 | + links = { |
| 87 | + ...links, |
| 88 | + commit: `[\`${shortCommitId}\`](${GITHUB_SERVER_URL}/${options.repo}/commit/${commitFromSummary})`, |
| 89 | + }; |
| 90 | + } |
| 91 | + return links; |
| 92 | + } |
| 93 | + const commitToFetchFrom = commitFromSummary || changeset.commit; |
| 94 | + if (commitToFetchFrom) { |
| 95 | + const { links } = await getInfo({ |
| 96 | + repo: options.repo, |
| 97 | + commit: commitToFetchFrom, |
| 98 | + }); |
| 99 | + return links; |
| 100 | + } |
| 101 | + return { |
| 102 | + commit: null, |
| 103 | + pull: null, |
| 104 | + user: null, |
| 105 | + }; |
| 106 | + })(); |
| 107 | + |
| 108 | + const users = usersFromSummary.length |
| 109 | + ? usersFromSummary |
| 110 | + .map( |
| 111 | + (userFromSummary) => `[@${userFromSummary}](${GITHUB_SERVER_URL}/${userFromSummary})`, |
| 112 | + ) |
| 113 | + .join(", ") |
| 114 | + : links.user; |
| 115 | + |
| 116 | + let suffix = ""; |
| 117 | + if (links.pull || links.commit || users) { |
| 118 | + suffix = `(${users ? `by ${users} ` : ""}in ${links.pull || links.commit})`; |
| 119 | + } |
| 120 | + |
| 121 | + return `\n\n- ${firstLine} ${suffix}\n${futureLines.map((l) => ` ${l}`).join("\n")}`; |
| 122 | + }, |
| 123 | +}; |
| 124 | + |
| 125 | +export default changelogFunctions; |
0 commit comments