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