-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathattempt-backport.js
241 lines (196 loc) · 6.15 KB
/
attempt-backport.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
'use strict'
const { spawn } = require('child_process')
const debug = require('debug')('attempt-backport')
const request = require('request')
const { fetchExistingThenUpdatePr, removeLabelFromPR, getBotPrLabels } = require('../lib/node-repo')
const enabledRepos = ['node']
const nodeVersions = [
{ version: 7 },
{ version: 6, lts: true },
{ version: 4, lts: true }
]
const queue = []
let inProgress = false
module.exports = function (app) {
if (!global._node_repo_dir) return
app.on('pull_request.opened', handlePrUpdate)
// Pull Request updates
app.on('pull_request.synchronize', handlePrUpdate)
// to trigger polling manually
app.get('/attempt-backport/pr/:owner/:repo/:id', (req, res) => {
const owner = req.params.owner
const repo = req.params.repo
const prId = parseInt(req.params.id, 10)
const options = { owner, repo, prId, logger: req.log }
if (~enabledRepos.indexOf(repo)) {
for (const node of nodeVersions) {
queueAttemptBackport(options, node.version, !!node.lts)
}
}
if (!inProgress) processNextBackport()
res.end()
})
}
function handlePrUpdate (event, owner, repo) {
if (!~enabledRepos.indexOf(repo)) return
if (event.pull_request.base.ref !== 'master') return
const prId = event.number
const options = { owner, repo, prId, logger: event.logger }
debug(`/${owner}/${repo}/pull/${prId} sync`)
for (const node of nodeVersions) {
queueAttemptBackport(options, node.version, !!node.lts)
}
if (!inProgress) processNextBackport()
}
function processNextBackport () {
const item = queue.shift()
if (!item) return
if (typeof item !== 'function') {
debug(`item was not a function! - queue size: ${queue.length}`)
return
} else if (inProgress) {
debug(`was still in progress! - queue size: ${queue.length}`)
return
}
item()
}
function queueAttemptBackport (options, version, isLTS) {
queue.push(function () {
options.logger.debug(`processing a new backport to v${version}`)
attemptBackport(options, version, isLTS, processNextBackport)
})
}
function attemptBackport (options, version, isLTS, cb) {
// Start
gitAmAbort()
function wrapCP (cmd, args, opts, callback) {
let exited = false
if (arguments.length === 3) {
callback = opts
opts = {}
}
opts.cwd = global._node_repo_dir
const cp = spawn(cmd, args, opts)
const argsString = [cmd, ...args].join(' ')
cp.on('error', function (err) {
debug(`child_process err: ${err}`)
if (!exited) onError()
})
cp.on('exit', function (code) {
exited = true
if (!cb) {
debug(`error before exit, code: ${code}, on '${argsString}'`)
return
} else if (code > 0) {
debug(`exit code > 0: ${code}, on '${argsString}'`)
onError()
return
}
callback()
})
// Useful when debugging.
cp.stdout.on('data', (data) => {
options.logger.debug(data.toString())
})
cp.stderr.on('data', (data) => {
options.logger.debug(data.toString())
})
return cp
}
function onError () {
if (!cb) return
const _cb = cb
setImmediate(() => {
options.logger.debug(`backport to ${version} failed`)
if (!isLTS) {
options.logger.debug(`Should have added (but temporary disabled): dont-land-on-v${version}.x`)
} else {
getBotPrLabels(options, (err, ourLabels) => {
if (err) {
options.logger.error(err, 'Error fetching existing bot labels')
return
}
const label = `lts-watch-v${version}.x`
if (!ourLabels.includes(label)) return
removeLabelFromPR(options, label)
})
}
setImmediate(() => {
inProgress = false
_cb()
})
})
cb = null
}
function gitAmAbort () {
// TODO(Fishrock123): this should probably just merge into wrapCP
let exited = false
options.logger.debug('aborting any previous backport attempt...')
const cp = spawn('git', ['am', '--abort'], { cwd: global._node_repo_dir })
const argsString = 'git am --abort'
cp.on('error', function (err) {
debug(`child_process err: ${err}`)
if (!exited) onError()
})
cp.on('exit', function (code) {
exited = true
if (!cb) {
debug(`error before exit, code: ${code}, on '${argsString}'`)
return
}
gitRemoteUpdate()
})
}
function gitRemoteUpdate () {
options.logger.debug('updating git remotes...')
wrapCP('git', ['remote', 'update', '-p'], gitCheckout)
}
function gitCheckout () {
options.logger.debug(`checking out origin/v${version}.x-staging...`)
wrapCP('git', ['checkout', `origin/v${version}.x-staging`], gitCleanFXD)
}
function gitCleanFXD () {
wrapCP('git', ['clean', '-fdx'], fetchDiff)
}
function fetchDiff () {
options.logger.debug(`fetching diff from pr ${options.prId}...`)
const url = `https://patch-diff.githubusercontent.com/raw/${options.owner}/${options.repo}/pull/${options.prId}.patch`
const req = request(url)
req.on('error', function (err) {
debug(`request err: ${err}`)
return onError()
})
req.on('response', function (response) {
if (response.statusCode !== 200) {
debug(`request non-200 status: ${response.statusCode}`)
return onError()
}
})
gitAttemptBackport(req)
}
function gitAttemptBackport (req) {
options.logger.debug(`attempting a backport to v${version}...`)
const cp = wrapCP('git', ['am', '-3'], { stdio: 'pipe' }, function done () {
// Success!
if (isLTS) {
fetchExistingThenUpdatePr(options, [`lts-watch-v${version}.x`])
} else {
getBotPrLabels(options, (err, ourLabels) => {
if (err) {
options.logger.error(err, 'Error fetching existing bot labels')
return
}
const label = `dont-land-on-v${version}.x`
if (!ourLabels.includes(label)) return
removeLabelFromPR(options, label)
})
}
setImmediate(() => {
options.logger.debug(`backport to v${version} successful`)
inProgress = false
cb()
})
})
req.pipe(cp.stdin)
}
}