/
gen-changelog.js
282 lines (245 loc) · 7.1 KB
/
gen-changelog.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// @noflow
'use strict';
const util = require('util');
const https = require('https');
const { exec } = require('./utils');
const packageJSON = require('../package.json');
const graphqlRequest = util.promisify(graphqlRequestImpl);
const labelsConfig = {
'PR: breaking change 💥': {
section: 'Breaking Change 💥',
},
'PR: feature 🚀': {
section: 'New Feature 🚀',
},
'PR: bug fix 🐞': {
section: 'Bug Fix 🐞',
},
'PR: docs 📝': {
section: 'Docs 📝',
fold: true,
},
'PR: polish 💅': {
section: 'Polish 💅',
fold: true,
},
'PR: internal 🏠': {
section: 'Internal 🏠',
fold: true,
},
'PR: dependency 📦': {
section: 'Dependency 📦',
fold: true,
},
};
const GH_TOKEN = process.env['GH_TOKEN'];
if (!GH_TOKEN) {
console.error('Must provide GH_TOKEN as enviroment variable!');
process.exit(1);
}
if (!packageJSON.repository || typeof packageJSON.repository.url !== 'string') {
console.error('package.json is missing repository.url string!');
process.exit(1);
}
const match = /https:\/\/github.com\/([^/]+)\/([^/]+).git/.exec(
packageJSON.repository.url,
);
if (match == null) {
console.error('Can not extract organisation and repo name from repo URL!');
process.exit(1);
}
const [, githubOrg, githubRepo] = match;
getChangeLog()
.then(changelog => process.stdout.write(changelog))
.catch(error => console.error(error));
function getChangeLog() {
const { version } = packageJSON;
let tag = null;
let commitsList = exec(`git rev-list --reverse v${version}..`);
if (commitsList === '') {
const parentPackageJSON = exec('git cat-file blob HEAD~1:package.json');
const parentVersion = JSON.parse(parentPackageJSON).version;
commitsList = exec(`git rev-list --reverse v${parentVersion}..HEAD~1`);
tag = `v${version}`;
}
const date = exec('git log -1 --format=%cd --date=short');
return getCommitsInfo(commitsList.split('\n')).then(commitsInfo =>
genChangeLog(tag, date, commitsInfo),
);
}
function genChangeLog(tag, date, commitsInfo) {
const allPRs = commitsInfoToPRs(commitsInfo);
const byLabel = {};
const commitersByLogin = {};
for (const pr of allPRs) {
if (!labelsConfig[pr.label]) {
throw new Error('Unknown label: ' + pr.label + pr.number);
}
byLabel[pr.label] = byLabel[pr.label] || [];
byLabel[pr.label].push(pr);
commitersByLogin[pr.author.login] = pr.author;
}
let changelog = `## ${tag || 'Unreleased'} (${date})\n`;
for (const [label, config] of Object.entries(labelsConfig)) {
const prs = byLabel[label];
if (prs) {
const shouldFold = config.fold && prs.length > 1;
changelog += `\n#### ${config.section}\n`;
if (shouldFold) {
changelog += '<details>\n';
changelog += `<summary> ${prs.length} PRs were merged </summary>\n\n`;
}
for (const pr of prs) {
const { number, url, author } = pr;
changelog += `* [#${number}](${url}) ${pr.title} ([@${author.login}](${author.url}))\n`;
}
if (shouldFold) {
changelog += '</details>\n';
}
}
}
const commiters = Object.values(commitersByLogin).sort((a, b) =>
(a.name || a.login).localeCompare(b.name || b.login),
);
changelog += `\n#### Committers: ${commiters.length}\n`;
for (const commiter of commiters) {
changelog += `* ${commiter.name}([@${commiter.login}](${commiter.url}))\n`;
}
return changelog;
}
function graphqlRequestImpl(query, variables, cb) {
const resultCB = typeof variables === 'function' ? variables : cb;
const req = https.request('https://api.github.com/graphql', {
method: 'POST',
headers: {
Authorization: 'bearer ' + GH_TOKEN,
'Content-Type': 'application/json',
'User-Agent': 'gen-changelog',
},
});
req.on('response', res => {
let responseBody = '';
res.setEncoding('utf8');
res.on('data', d => (responseBody += d));
res.on('error', error => resultCB(error));
res.on('end', () => {
if (res.statusCode !== 200) {
return resultCB(
new Error(
`GitHub responded with ${res.statusCode}: ${res.statusMessage}\n` +
responseBody,
),
);
}
let json;
try {
json = JSON.parse(responseBody);
} catch (error) {
return resultCB(error);
}
if (json.errors) {
return resultCB(
new Error('Errors: ' + JSON.stringify(json.errors, null, 2)),
);
}
resultCB(undefined, json.data);
});
});
req.on('error', error => cb(error));
req.write(JSON.stringify({ query, variables }));
req.end();
}
async function batchCommitInfo(commits) {
let commitsSubQuery = '';
for (const oid of commits) {
commitsSubQuery += `
commit_${oid}: object(oid: "${oid}") {
... on Commit {
oid
message
associatedPullRequests(first: 10) {
nodes {
number
title
url
author {
login
url
... on User {
name
}
}
repository {
nameWithOwner
}
labels(first: 10) {
nodes {
name
}
}
}
}
}
}
`;
}
const response = await graphqlRequest(`
{
repository(owner: "${githubOrg}", name: "${githubRepo}") {
${commitsSubQuery}
}
}
`);
const commitsInfo = [];
for (const oid of commits) {
commitsInfo.push(response.repository['commit_' + oid]);
}
return commitsInfo;
}
function commitsInfoToPRs(commits) {
const prs = {};
for (const commit of commits) {
const associatedPRs = commit.associatedPullRequests.nodes.filter(
pr => pr.repository.nameWithOwner === `${githubOrg}/${githubRepo}`,
);
if (associatedPRs.length === 0) {
throw new Error(
`Commit ${commit.oid} has no associated PR: ${commit.message}`,
);
}
if (associatedPRs.length > 1) {
throw new Error(
`Commit ${commit.oid} is associated with multiple PRs: ${commit.message}`,
);
}
const pr = associatedPRs[0];
const labels = pr.labels.nodes
.map(label => label.name)
.filter(label => label.startsWith('PR: '));
if (labels.length === 0) {
throw new Error(`PR #${pr.number} missing label`);
}
if (labels.length > 1) {
throw new Error(
`PR #${pr.number} has conflicting labels: ` + labels.join('\n'),
);
}
prs[pr.number] = {
number: pr.number,
title: pr.title,
url: pr.url,
author: pr.author,
label: labels[0],
};
}
return Object.values(prs);
}
async function getCommitsInfo(commits) {
// Split commits into batches of 50 to prevent timeouts
const commitInfoPromises = [];
for (let i = 0; i < commits.length; i += 50) {
const batch = commits.slice(i, i + 50);
commitInfoPromises.push(batchCommitInfo(batch));
}
return (await Promise.all(commitInfoPromises)).flat();
}