This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
301 lines (273 loc) · 9.05 KB
/
server.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
292
293
294
295
296
297
298
299
300
301
if (process.env.GIT_EMAIL) {
require('child_process').execSync(`git config user.email ${process.env.GIT_EMAIL}`)
}
const { App } = require('@octokit/app')
const Octokit = require('@octokit/rest')
const express = require('express');
const basicAuth = require('express-basic-auth')
const app = express();
const prettier = require('prettier')
require('longjohn');
// https://github.com/bemusic/bemuse/blob/master/.prettierrc
const prettierConfig = {
"singleQuote": true,
"semi": false,
"proseWrap": "always",
"trailingComma": "es5",
"jsxSingleQuote": true
}
const authenticated = basicAuth({
users: { admin: process.env.ADMIN_PASSWORD },
challenge: true,
realm: 'bemuse-release-train',
})
app.use(authenticated)
app.use(express.static('public'))
const owner = 'bemusic'
const repo = 'bemuse'
app.get('/changelog', async function(req, res, next) {
try {
const gh = getGitHubClient()
const log = console.log
// Fetch the pull requests
const pullsResponse = await gh.pulls.list({
owner,
repo,
per_page: 100,
sort: 'created',
direction: 'asc',
})
// Fetch existing changelog
const changelogResponse = await gh.repos.getContents({
owner,
repo,
path: 'CHANGELOG.md'
})
const existingChangelog = Buffer.from(changelogResponse.data.content, 'base64').toString()
const pullsToPrepare = pullsResponse.data.filter(p => p.labels.map(l => l.name).includes('c:ready'))
const markdown = updateChangelog(existingChangelog, pullsToPrepare)
const htmlResponse = await gh.markdown.render({
text: markdown,
context: `${owner}/${repo}`
})
res.send(htmlResponse.data)
} catch (e) {
next(e)
}
})
const indent = require('indent-string')
const _ = require('lodash')
function updateChangelog(existingChangelog, pulls, version = 'UNRELEASED') {
const userListRegExp = /((?:\[@\w+\]: https.+\n)+)/
const userList = existingChangelog.match(userListRegExp)
const existingUsers = new Set()
userList[1].replace(/@(\w+)/g, (a, id) => {
existingUsers.add(id.toLowerCase())
})
const pullMap = new Map()
const newUsers = new Map()
const registerUser = u => {
if (!existingUsers.has(u.toLowerCase())) newUsers.set(u.toLowerCase(), u)
return `[@${u}]`
}
const bullets = pulls
.map(p => ({
match: p.body.match(/### Changelog\s*\n([^]+)/),
pull: p,
}))
.filter(x => x.match)
.map(x => {
pullMap.set(x.pull.number, x.pull)
const text = x.match[1].trim().replace(/\[@([^\]\s]+)\]/, (a, id) => {
return registerUser(id)
})
return {
text: `- ${indent(text, 2).substr(2)} [#${x.pull.number}], by ${registerUser(x.pull.user.login)}`,
category: (x.pull.labels.find(l => l.name.startsWith('category:')) || { name: 'category:Others' }).name.replace(/category:/, ''),
}
})
const bulletPoints = _.chain(bullets)
.groupBy(b => b.category)
.map((v, k) => {
return {
text: `### ${k}\n\n` + v.map(b => b.text).join('\n'),
order: (['New stuff'].indexOf(k) + 1) || 999999,
}
})
.sortBy(b => b.order)
.value()
.map(c => c.text)
.join('\n\n')
const pullRefs = [...pullMap].map(([number, pull]) => `[#${number}]: ${pull.html_url}`).join('\n')
const newUserRefs = [...newUsers].map(([k, u]) => `[@${k}]: https://github.com/${u}`).join('\n')
const newMarkdown = `## ${version}\n\n${bulletPoints}\n\n${pullRefs}`
let markdown = existingChangelog
.replace(userListRegExp, a => {
return a + newUserRefs + '\n'
})
.replace(/## /, a => {
return newMarkdown + '\n\n' + a
})
return prettier.format(markdown, { ...prettierConfig, parser: 'markdown' })
}
app.post('/prepare/:version', async function(req, res, next) {
try {
const gh = getGitHubClient()
const log = console.log
// Fetch the pull requests
const pullsResponse = await gh.pulls.list({
owner,
repo,
per_page: 100,
sort: 'created',
direction: 'asc',
})
log(`Pull requests fetched: ${pullsResponse.data.length}`)
// Existing pull request for release
const existingPull = pullsResponse.data.find(p => p.head.ref === 'release-train/proposed')
const pullsToPrepare = pullsResponse.data.filter(p => p.labels.map(l => l.name).includes('c:ready'))
log(`Pull requests to prepare: ${pullsToPrepare.length}`)
// Create a preparation branch
const masterResponse = await gh.git.getRef({
owner,
repo,
ref: 'heads/master'
})
const masterSha = masterResponse.data.object.sha
log(`master branch is at ${masterSha}`)
if (!masterSha) {
throw new Error('Expected masterSha to exist')
}
await gh.git.createRef({
owner, repo,
ref: 'refs/heads/release-train/prepare',
sha: masterSha
})
// Merge each PR to this branch
let headSha = masterSha
const mergedPulls = []
for (const pull of pullsToPrepare) {
log(`Preparing pull request #${pull.number}`)
try {
const mergeResponse = await gh.repos.merge({
owner,
repo,
base: 'release-train/prepare',
head: pull.head.ref,
commit_message: `Merge pull request #${pull.number} from ${pull.head.ref}`,
})
headSha = mergeResponse.data.sha
if (!headSha) {
throw new Error('Expected headSha to exist')
}
log(`Merged pull request #${pull.number} from ${pull.head.ref} -> ${headSha}`)
mergedPulls.push(pull)
} catch (e) {
log(`Failed to merge pull request #${pull.number}: ${e}`)
}
}
// Update changelog
const changelogResponse = await gh.repos.getContents({
owner,
repo,
path: 'CHANGELOG.md',
ref: headSha,
})
const existingChangelog = Buffer.from(changelogResponse.data.content, 'base64').toString()
const version = req.params.version.replace(/^(\d)/, 'v$1')
const preVersion = version + '-pre.' + new Date().toJSON().replace(/\D/g, '').substr(0, 12)
const newChangelog = updateChangelog(existingChangelog, mergedPulls, preVersion)
const changelogUpdateResponse = await gh.repos.createOrUpdateFile({
owner,
repo,
path: 'CHANGELOG.md',
message: 'Prepare changelog for ' + preVersion,
content: Buffer.from(newChangelog).toString('base64'),
branch: 'release-train/prepare',
sha: changelogResponse.data.sha,
})
headSha = changelogUpdateResponse.data.commit.sha
log(`Updated changelog -> ${headSha}`)
// Update package.json version
const packageJSONResponse = await gh.repos.getContents({
owner,
repo,
path: 'package.json',
ref: headSha,
})
const packageJSONUpdateResponse = await gh.repos.createOrUpdateFile({
owner,
repo,
path: 'CHANGELOG.md',
message: 'Prepare changelog for ' + preVersion,
content: Buffer.from(newChangelog).toString('base64'),
branch: 'release-train/prepare',
sha: changelogResponse.data.sha,
})
headSha = packageJSONUpdateResponse.data.commit.sha
log(`Updated package.json -> ${headSha}`)
// Update the proposed branch
const forcePushRef = async (ref, sha) => {
try {
log(`Updating ref ${ref} -> ${sha}`)
await gh.git.updateRef({ owner, repo, ref, sha, force: true })
} catch (e) {
if (e.status === 422) {
log(`422 - Creating ref ${ref} -> ${sha}`)
await gh.git.createRef({ owner, repo, ref: 'refs/' + ref, sha })
} else {
throw e
}
}
}
log(`Updating proposed branch`)
await forcePushRef('heads/release-train/proposed', headSha)
log(`Deleting preparation branch`)
await gh.git.deleteRef({
owner, repo,
ref: 'heads/release-train/prepare',
})
const pullBody = `This is an automatically-generated pull request which will merge these pull requests:\n\n` +
mergedPulls.map(p => {
return `- #${p.number} @ ${p.head.sha} “${p.title}” by @${p.user.login}`
}).join('\n')
const pullTitle = `Prepare release of Bemuse ${version}`
if (existingPull) {
await gh.pulls.update({
owner,
repo,
pull_number: existingPull.number,
body: pullBody,
title: pullTitle,
})
} else {
await gh.pulls.create({
owner,
repo,
title: pullTitle,
head: 'release-train/proposed',
base: 'master',
body: pullBody,
})
}
res.send('OK!')
} catch (e) {
next(e)
}
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});
function getGitHubClient() {
const app = new App({ id: process.env.GH_APP_ID, privateKey: Buffer.from(process.env.GH_APP_PRIVATE_KEY_BASE64, 'base64').toString() })
const octokit = new Octokit({
async auth () {
const installationAccessToken = await app.getInstallationAccessToken({
installationId: process.env.GH_APP_INSTALLATION_ID
});
return `token ${installationAccessToken}`;
}
})
return octokit
}