|
| 1 | +// based on https://github.com/atlassian/changesets/blob/main/packages/changelog-github/src/index.ts |
| 2 | +// modifications to improve readability: |
| 3 | +// - removed thanks notes. We're thanking contributors in the PRs or acknowledge their work in different ways |
| 4 | +// - moved issue links to end of first line |
| 5 | + |
| 6 | +import { config } from 'dotenv'; |
| 7 | +import { getInfo, getInfoFromPullRequest } from '@changesets/get-github-info'; |
| 8 | + |
| 9 | +config(); |
| 10 | + |
| 11 | +const changelogFunctions = { |
| 12 | + getDependencyReleaseLine: async (changesets, dependenciesUpdated, options) => { |
| 13 | + if (!options.repo) { |
| 14 | + throw new Error( |
| 15 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]' |
| 16 | + ); |
| 17 | + } |
| 18 | + if (dependenciesUpdated.length === 0) return ''; |
| 19 | + |
| 20 | + const changesetLink = `- Updated dependencies [${( |
| 21 | + await Promise.all( |
| 22 | + changesets.map(async (cs) => { |
| 23 | + if (cs.commit) { |
| 24 | + let { links } = await getInfo({ |
| 25 | + repo: options.repo, |
| 26 | + commit: cs.commit |
| 27 | + }); |
| 28 | + return links.commit; |
| 29 | + } |
| 30 | + }) |
| 31 | + ) |
| 32 | + ) |
| 33 | + .filter((_) => _) |
| 34 | + .join(', ')}]:`; |
| 35 | + |
| 36 | + const updatedDepenenciesList = dependenciesUpdated.map( |
| 37 | + (dependency) => ` - ${dependency.name}@${dependency.newVersion}` |
| 38 | + ); |
| 39 | + |
| 40 | + return [changesetLink, ...updatedDepenenciesList].join('\n'); |
| 41 | + }, |
| 42 | + getReleaseLine: async (changeset, type, options) => { |
| 43 | + if (!options || !options.repo) { |
| 44 | + throw new Error( |
| 45 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + let prFromSummary; |
| 50 | + let commitFromSummary; |
| 51 | + let usersFromSummary; |
| 52 | + |
| 53 | + const replacedChangelog = changeset.summary |
| 54 | + .replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => { |
| 55 | + let num = Number(pr); |
| 56 | + if (!isNaN(num)) prFromSummary = num; |
| 57 | + return ''; |
| 58 | + }) |
| 59 | + .replace(/^\s*commit:\s*([^\s]+)/im, (_, commit) => { |
| 60 | + commitFromSummary = commit; |
| 61 | + return ''; |
| 62 | + }) |
| 63 | + .replace(/^\s*(?:author|user):\s*@?([^\s]+)/gim, (_, user) => { |
| 64 | + usersFromSummary.push(user); |
| 65 | + return ''; |
| 66 | + }) |
| 67 | + .trim(); |
| 68 | + |
| 69 | + const [firstLine, ...futureLines] = replacedChangelog.split('\n').map((l) => l.trimRight()); |
| 70 | + |
| 71 | + const links = await (async () => { |
| 72 | + if (prFromSummary !== undefined) { |
| 73 | + let { links } = await getInfoFromPullRequest({ |
| 74 | + repo: options.repo, |
| 75 | + pull: prFromSummary |
| 76 | + }); |
| 77 | + if (commitFromSummary) { |
| 78 | + links = { |
| 79 | + ...links, |
| 80 | + commit: `[\`${commitFromSummary}\`](https://github.com/${options.repo}/commit/${commitFromSummary})` |
| 81 | + }; |
| 82 | + } |
| 83 | + return links; |
| 84 | + } |
| 85 | + const commitToFetchFrom = commitFromSummary || changeset.commit; |
| 86 | + if (commitToFetchFrom) { |
| 87 | + let { links } = await getInfo({ |
| 88 | + repo: options.repo, |
| 89 | + commit: commitToFetchFrom |
| 90 | + }); |
| 91 | + return links; |
| 92 | + } |
| 93 | + return { |
| 94 | + commit: null, |
| 95 | + pull: null, |
| 96 | + user: null |
| 97 | + }; |
| 98 | + })(); |
| 99 | + |
| 100 | + const suffix = [ |
| 101 | + links.pull === null ? '' : ` (${links.pull})`, |
| 102 | + links.commit === null ? '' : ` (${links.commit})` |
| 103 | + ].join(''); |
| 104 | + |
| 105 | + return `\n\n- ${firstLine}${suffix}\n${futureLines.map((l) => ` ${l}`).join('\n')}`; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +export default changelogFunctions; |
0 commit comments