-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
291 lines (246 loc) · 7.05 KB
/
index.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
283
284
285
286
287
288
289
290
291
/** Copyright (c) 2017 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// NOTE: prevailing assumption is a release's target_commitish is always a SHA.
const generateChangelog = require('./generate-changelog');
const generateMigrationGuide = require('./generate-migration-guide');
const releasesForCommit = require('./releases-for-commit');
const prForCommit = require('./pr-for-commit');
module.exports = robot => {
robot.on('release', handler);
robot.on('pull_request.edited', update);
robot.on('pull_request.labeled', update);
robot.on('pull_request.unlabeled', update);
async function handler(context) {
const release = context.payload.release;
// Don't generate release notes for prereleases
if (release.prerelease) {
return;
}
const releasesBySha = await fetchAllReleases(context);
const {commits} = await getChangeInfo(
context,
release.target_commitish,
releasesBySha,
[release.target_commitish],
);
const body = await notesForCommits(context, commits);
updateRelease(context, release, body);
}
async function update(context) {
const pr = context.payload.pull_request;
if (!pr.merged) {
return;
}
const repo = context.payload.repository.full_name;
const releases = await releasesForCommit(repo, pr.merge_commit_sha);
const {releasesBySha, tagShas} = await getReleaseInfo(context, releases);
const {commits, release} = await getChangeInfo(
context,
pr.merge_commit_sha,
releasesBySha,
tagShas,
);
const body = await notesForCommits(context, commits);
updateRelease(context, release, body);
}
};
async function updateRelease(context, release, body) {
const {github} = context;
github.repos.editRelease(
context.repo({
id: release.id,
tag_name: release.tag_name,
body,
}),
);
}
// fetches all releases
// also finds releases corresponding to provided tags
async function getReleaseInfo(context, childTags) {
const tagShas = [];
const releasesBySha = await fetchAllReleases(context, release => {
if (childTags.has(release.tag_name)) {
// put in reverse order
// later releases come first,
// but we want to iterate beginning oldest releases first
tagShas.unshift(release.target_commitish);
// tagSha.push(release.target_commitish);
}
});
return {releasesBySha, tagShas};
}
async function fetchAllReleases(context, handler = () => {}) {
const {github} = context;
const releasesBySha = new Map();
const req = github.repos.getReleases(
context.repo({
per_page: 100,
}),
);
await fetchPages(github, req, results => {
results.data.forEach(release => {
if (release.prerelease) {
// Ignore prereleases, so changelog reflects changes since last non-prerelease
return;
}
releasesBySha.set(release.target_commitish, release);
handler(release);
});
});
return releasesBySha;
}
async function getChangeInfo(
context,
commitShaInReleaseOrChildReleaseCommitSha,
releaseCommits,
pertinentReleaseCommits,
) {
const loadedCommits = new Map();
for (const sha of pertinentReleaseCommits) {
if (loadedCommits.has(sha)) {
// skip this commit if visited already
continue;
}
const done = await fetchRelevantCommits(
context,
loadedCommits,
sha,
releaseCommits,
commitShaInReleaseOrChildReleaseCommitSha,
);
if (done) {
return {
commits: buildCommits(
loadedCommits,
done.releaseSha,
done.parentReleaseSha,
),
release: releaseCommits.get(done.releaseSha),
};
}
}
}
async function notesForCommits(context, commits) {
const {github} = context;
const repo = context.payload.repository.full_name;
const prs = await Promise.all(commits.map(sha => prForCommit(repo, sha)));
const issues = await Promise.all(
prs.filter(Boolean).map(number =>
github.issues.get(
context.repo({
number,
}),
),
),
);
let changes = issues.map(issue => ({
labels: issue.data.labels,
title: issue.data.title,
number: issue.data.number,
url: issue.data.html_url,
}));
const config = await context.config('release-notes.yml', {
labels: [
'security',
'breaking',
'interface',
'bugfix',
'dependencies',
'performance',
],
ignore: ['release'],
});
changes = changes.filter(change => {
return !change.labels.some(label => config.ignore.includes(label.name));
});
const prNums = changes.reduce((acc, change) => {
return acc.add(change.number);
}, new Set());
const changelog = generateChangelog(changes, config.labels);
const guide = await generateMigrationGuide(context, prNums);
return [
'## Changelog',
changelog,
...(guide ? ['## Migration Guide', guide] : []),
].join('\n');
}
function buildCommits(commitsBySha, releaseSha, prevReleaseSha) {
const commits = [];
let nextSha = releaseSha;
while (nextSha !== prevReleaseSha) {
const commit = commitsBySha.get(nextSha);
commits.push(commit.sha);
if (commit.parents.length > 1) {
throw new Error(`commit has ${commit.sha} multiple parents`);
}
nextSha = commit.parents.length === 0 ? void 0 : commit.parents[0].sha;
}
return commits;
}
async function fetchRelevantCommits(
context,
commitCache,
sha,
releaseCommits,
commitShaInReleaseOrChildReleaseCommitSha,
) {
if (commitCache.has(sha)) {
return; // return early
}
const {github} = context;
let nearestDescendantReleaseSha = sha;
let req = github.repos.getCommits(
context.repo({
sha,
per_page: 100,
}),
);
let releaseSha;
let parentReleaseSha;
// todo: verify no two releases target same commit hash
await fetchPages(github, req, commits => {
for (let commit of commits.data) {
if (commitCache.has(commit.sha)) {
// break out of loop if we've already encountered this commit.
// hence as we have already visited all parents
return true;
}
commitCache.set(commit.sha, commit);
if (commit.sha === commitShaInReleaseOrChildReleaseCommitSha) {
releaseSha = nearestDescendantReleaseSha;
} else {
if (releaseCommits.has(commit.sha)) {
nearestDescendantReleaseSha = commit.sha;
if (releaseSha) {
if (commit.sha !== releaseSha) {
// set parent release
parentReleaseSha = commit.sha;
}
// exit now
return true;
}
}
}
}
});
if (!releaseSha) {
throw new Error('Unexpected state');
}
return {
releaseSha,
parentReleaseSha,
};
}
async function fetchPages(github, pageReq, pageHandler) {
while (pageReq) {
const page = await pageReq;
const stopEarly = await pageHandler(page);
pageReq =
!stopEarly && github.hasNextPage(page)
? github.getNextPage(page)
: void 0;
}
}