|
| 1 | +const { config } = require('dotenv'); |
| 2 | +const { |
| 3 | + getInfo, |
| 4 | + getInfoFromPullRequest, |
| 5 | +} = require('@changesets/get-github-info'); |
| 6 | + |
| 7 | +config(); |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + getDependencyReleaseLine: async () => { |
| 11 | + return ''; |
| 12 | + }, |
| 13 | + getReleaseLine: async (changeset, type, options) => { |
| 14 | + if (!options || !options.repo) { |
| 15 | + throw new Error( |
| 16 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]' |
| 17 | + ); |
| 18 | + } |
| 19 | + |
| 20 | + let prFromSummary; |
| 21 | + let commitFromSummary; |
| 22 | + let usersFromSummary = []; |
| 23 | + |
| 24 | + const replacedChangelog = changeset.summary |
| 25 | + .replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => { |
| 26 | + let num = Number(pr); |
| 27 | + if (!isNaN(num)) prFromSummary = num; |
| 28 | + return ''; |
| 29 | + }) |
| 30 | + .replace(/^\s*commit:\s*([^\s]+)/im, (_, commit) => { |
| 31 | + commitFromSummary = commit; |
| 32 | + return ''; |
| 33 | + }) |
| 34 | + .replace(/^\s*(?:author|user):\s*@?([^\s]+)/gim, (_, user) => { |
| 35 | + usersFromSummary.push(user); |
| 36 | + return ''; |
| 37 | + }) |
| 38 | + .trim(); |
| 39 | + |
| 40 | + const [firstLine, ...futureLines] = replacedChangelog |
| 41 | + .split('\n') |
| 42 | + .map((l) => l.trimRight()); |
| 43 | + |
| 44 | + const links = await (async () => { |
| 45 | + if (prFromSummary !== undefined) { |
| 46 | + let { links } = await getInfoFromPullRequest({ |
| 47 | + repo: options.repo, |
| 48 | + pull: prFromSummary, |
| 49 | + }); |
| 50 | + if (commitFromSummary) { |
| 51 | + links = { |
| 52 | + ...links, |
| 53 | + commit: `[\`${commitFromSummary}\`](https://github.com/${options.repo}/commit/${commitFromSummary})`, |
| 54 | + }; |
| 55 | + } |
| 56 | + return links; |
| 57 | + } |
| 58 | + const commitToFetchFrom = commitFromSummary || changeset.commit; |
| 59 | + if (commitToFetchFrom) { |
| 60 | + let { links } = await getInfo({ |
| 61 | + repo: options.repo, |
| 62 | + commit: commitToFetchFrom, |
| 63 | + }); |
| 64 | + return links; |
| 65 | + } |
| 66 | + return { |
| 67 | + commit: null, |
| 68 | + pull: null, |
| 69 | + user: null, |
| 70 | + }; |
| 71 | + })(); |
| 72 | + |
| 73 | + const users = usersFromSummary.length |
| 74 | + ? usersFromSummary |
| 75 | + .map( |
| 76 | + (userFromSummary) => |
| 77 | + `[@${userFromSummary}](https://github.com/${userFromSummary})` |
| 78 | + ) |
| 79 | + .join(', ') |
| 80 | + : links.user; |
| 81 | + |
| 82 | + const pull = links.pull === null ? '' : ` ${links.pull}`; |
| 83 | + const commit = !!pull || links.commit === null ? '' : ` ${links.commit}`; |
| 84 | + |
| 85 | + const prefix = [pull, commit, users === null ? '' : ` by ${users}`].join( |
| 86 | + '' |
| 87 | + ); |
| 88 | + |
| 89 | + let lines = `${firstLine}\n${futureLines.map((l) => ` ${l}`).join('\n')}`; |
| 90 | + |
| 91 | + if (firstLine[0] === '-') { |
| 92 | + lines = `\n ${firstLine}\n${futureLines |
| 93 | + .map((l) => ` ${l}`) |
| 94 | + .join('\n')}`; |
| 95 | + } |
| 96 | + |
| 97 | + return `\n\n-${prefix ? `${prefix} –` : ''} ${lines}`; |
| 98 | + }, |
| 99 | +}; |
0 commit comments