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