Repository navigation
Expand file tree
/
Copy pathattempt-backport.js
More file actions
225 lines (185 loc) · 5.99 KB
/
Copy pathattempt-backport.js
File metadata and controls
225 lines (185 loc) · 5.99 KB
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
'use strict'
const child_process = require('child_process')
const debug = require('debug')('attempt-backport')
const request = require('request')
const node_repo = require('../lib/node-repo')
const updatePrWithLabels = node_repo.updatePrWithLabels
// const removeLabelFromPR = node_repo.removeLabelFromPR
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)
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()
}
// 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 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 = child_process.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) updatePrWithLabels(options, [`dont-land-on-v${version}.x`])
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 = child_process.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
}
gitResetBefore()
})
}
function gitResetBefore () {
options.logger.debug(`resetting origin/v${version}.x-staging...`)
wrapCP('git', ['reset', `origin/v${version}.x-staging`, '--hard'], 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`], gitReset)
}
function gitReset () {
options.logger.debug(`resetting origin/v${version}.x-staging...`)
wrapCP('git', ['reset', `origin/v${version}.x-staging`, '--hard'], 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'], { stdio: 'pipe' }, function done () {
// Success!
if (isLTS) {
updatePrWithLabels(options, [`lts-watch-v${version}.x`])
}// else {
// TODO(Fishrock123): Re-enable this, but do a check first
// to make sure the label was set by the bot only.
// removeLabelFromPR(options, `dont-land-on-v${version}.x`)
// }
setImmediate(() => {
options.logger.debug(`backport to v${version} successful`)
inProgress = false
cb()
})
})
req.pipe(cp.stdin)
}
}