Skip to content

Commit b5a58cd

Browse files
authored
ci: add custom changelog formatter (#90)
* ci: add custom changelog formatter * add changeset to ensure custom generator runs
1 parent e7d0294 commit b5a58cd

File tree

5 files changed

+124
-15
lines changed

5 files changed

+124
-15
lines changed

.changeset/angry-emus-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
customize changelog format

.changeset/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3-
"changelog": ["@changesets/changelog-github", { "repo": "sveltejs/vite-plugin-svelte" }],
3+
"changelog": ["scripts/changelog-github-custom.js", { "repo": "sveltejs/vite-plugin-svelte" }],
44
"commit": false,
55
"linked": [],
66
"access": "public",
77
"baseBranch": "main",
88
"updateInternalDependencies": "patch",
9-
"ignore": ["playground**"]
9+
"ignore": ["playground**","e2e-test**"]
1010
}
1111

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"prepare": "husky install"
2424
},
2525
"devDependencies": {
26-
"@changesets/changelog-github": "^0.4.0",
2726
"@changesets/cli": "^2.16.0",
27+
"@changesets/get-github-info": "^0.5.0",
2828
"@types/fs-extra": "^9.0.12",
2929
"@types/jest": "^26.0.24",
3030
"@types/node": "^16.3.0",
@@ -33,6 +33,7 @@
3333
"@typescript-eslint/parser": "^4.28.2",
3434
"chalk": "^4.1.1",
3535
"cross-env": "^7.0.3",
36+
"dotenv": "^10.0.0",
3637
"enquirer": "^2.3.6",
3738
"esbuild": "^0.12.15",
3839
"eslint": "^7.30.0",

pnpm-lock.yaml

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/changelog-github-custom.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)