forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreleaseChangelog.mjs
213 lines (191 loc) · 6.27 KB
/
releaseChangelog.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* eslint-disable no-restricted-syntax */
import childProcess from 'child_process';
import { promisify } from 'util';
import { Octokit } from '@octokit/rest';
import chalk from 'chalk';
import yargs from 'yargs';
const exec = promisify(childProcess.exec);
/**
* @param {string} commitMessage
* @returns {string} The tags in lowercases, ordered ascending and comma separated
*/
function parseTags(commitMessage) {
const tagMatch = commitMessage.match(/^(\[[\w-]+\])+/);
if (tagMatch === null) {
return '';
}
const [tagsWithBracketDelimiter] = tagMatch;
return tagsWithBracketDelimiter
.match(/([\w-]+)/g)
.map((tag) => {
return tag.toLocaleLowerCase();
})
.sort((a, b) => {
return a.localeCompare(b);
})
.join(',');
}
/**
* @param {Octokit.ReposCompareCommitsResponseCommitsItem} commitsItem
*/
function filterCommit(commitsItem) {
// TODO: Use labels
// Filter dependency updates
return !commitsItem.commit.message.startsWith('Bump');
}
async function findLatestTaggedVersion() {
const { stdout } = await exec(
[
'git',
'describe',
// Earlier tags used lightweight tags + commit.
// We switched to annotated tags later.
'--tags',
'--abbrev=0',
// only include "version-tags"
'--match "v*"',
].join(' '),
);
return stdout.trim();
}
async function main(argv) {
const { githubToken, lastRelease: lastReleaseInput, release, repo } = argv;
if (!githubToken) {
throw new TypeError(
'Unable to authenticate. Make sure you either call the script with `--githubToken $token` or set `process.env.GITHUB_TOKEN`. The token needs `public_repo` permissions.',
);
}
const octokit = new Octokit({
auth: githubToken,
request: {
fetch,
},
});
const latestTaggedVersion = await findLatestTaggedVersion();
const lastRelease = lastReleaseInput !== undefined ? lastReleaseInput : latestTaggedVersion;
if (lastRelease !== latestTaggedVersion) {
console.warn(
`Creating changelog for ${lastRelease}..${release} when latest tagged version is '${latestTaggedVersion}'.`,
);
}
/**
* @type {AsyncIterableIterator<Octokit.Response<Octokit.ReposCompareCommitsResponse>>}
*/
const timeline = octokit.paginate.iterator(
octokit.repos.compareCommits.endpoint.merge({
owner: 'mui',
repo,
base: lastRelease,
head: release,
}),
);
/**
* @type {Octokit.ReposCompareCommitsResponseCommitsItem[]}
*/
const commitsItems = [];
for await (const response of timeline) {
const { data: compareCommits } = response;
commitsItems.push(...compareCommits.commits.filter(filterCommit));
}
let warnedOnce = false;
const getAuthor = (commit) => {
if (!commit.author) {
if (!warnedOnce) {
console.warn(
`The author of the commit: ${commit.commit.tree.url} cannot be retrieved. Please add the github username manually.`,
);
}
warnedOnce = true;
return chalk.red("TODO INSERT AUTHOR'S USERNAME");
}
return commit.author?.login;
};
const authors = Array.from(
new Set(
commitsItems.map((commitsItem) => {
return getAuthor(commitsItem);
}),
),
);
const contributorHandles = authors
.sort((a, b) => a.localeCompare(b))
.map((author) => `@${author}`)
.join(', ');
// We don't know when a particular commit was made from the API.
// Only that the commits are ordered by date ASC
const commitsItemsByDateDesc = commitsItems.slice().reverse();
// Sort by tags ASC, date desc
// Will only consider exact matches of tags so `[Slider]` will not be grouped with `[Slider][Modal]`
commitsItems.sort((a, b) => {
const aTags = parseTags(a.commit.message);
const bTags = parseTags(b.commit.message);
if (aTags === bTags) {
return commitsItemsByDateDesc.indexOf(a) - commitsItemsByDateDesc.indexOf(b);
}
return aTags.localeCompare(bTags);
});
const changes = commitsItems.map((commitsItem) => {
// Helps changelog author keeping track of order when grouping commits under headings.
// ​ is a zero-width-space that ensures that the content of the listitem is formatted properly
const dateSortMarker = `​<!-- ${(commitsItems.length - commitsItems.indexOf(commitsItem))
.toString()
// Padding them with a zero means we can just feed a list into online sorting tools like https://www.online-utility.org/text/sort.jsp
// i.e. we can sort the lines alphanumerically
.padStart(Math.floor(Math.log10(commitsItemsByDateDesc.length)) + 1, '0')} -->`;
const shortMessage = commitsItem.commit.message.split('\n')[0];
return `- ${dateSortMarker}${shortMessage} @${getAuthor(commitsItem)}`;
});
const nowFormatted = new Date().toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
});
const changelog = `
## TODO RELEASE NAME
<!-- generated comparing ${lastRelease}..${release} -->
_${nowFormatted}_
A big thanks to the ${
authors.length
} contributors who made this release possible. Here are some highlights ✨:
TODO INSERT HIGHLIGHTS
${changes.join('\n')}
All contributors of this release in alphabetical order: ${contributorHandles}
`;
// eslint-disable-next-line no-console -- output of this script
console.log(changelog);
}
yargs(process.argv.slice(2))
.command({
command: '$0',
description: 'Creates a changelog',
builder: (command) => {
return command
.option('lastRelease', {
describe:
'The release to compare against e.g. `v5.0.0-alpha.23`. Default: The latest tag on the current branch.',
type: 'string',
})
.option('githubToken', {
default: process.env.GITHUB_TOKEN,
describe:
'The personal access token to use for authenticating with GitHub. Needs public_repo permissions.',
type: 'string',
})
.option('release', {
// #default-branch-switch
default: 'next',
describe: 'Ref which we want to release',
type: 'string',
})
.option('repo', {
default: 'material-ui',
describe: 'Repository to generate a changelog for',
type: 'string',
});
},
handler: main,
})
.help()
.strict(true)
.version(false)
.parse();