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