-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (72 loc) · 3.21 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
const {Probot} = require('probot')
const backport = require('./backport')
Probot.run(backportApp)
async function backportApp (app) {
async function handler (context) {
const issue = context.payload.issue || context.payload.pull_request
const comment = context.payload.comment
if (!issue.html_url.endsWith(`pull/${issue.number}`)) return
const bodyAndBranches = await matchComments(context, comment.body)
const {targetBranches} = bodyAndBranches
if (!targetBranches.length) return
let {body} = bodyAndBranches
await updateComment(context, body)
for (const {arg1: targetBranch, arg2} of targetBranches) {
try {
await backport(context, targetBranch, arg2 === '--force')
body = body.replace(`🕑 /backport ${targetBranch}`, `🎉 /backport ${targetBranch}`)
await updateComment(context, body)
} catch (err) {
context.log.warn(`Backport to ${targetBranch} failed`, err)
body = [
body.replace(`🕑 /backport ${targetBranch}`, `💥 /backport ${targetBranch}`),
'',
`The backport to ${targetBranch} failed.`,
`Please do this backport manually.`,
...(err.pr ? ['```',
`git fetch origin ${targetBranch}`,
`git checkout -b backport/${issue.number}/${targetBranch} ${targetBranch}`,
`git cherry-pick ${err.pr.base.sha}..${err.pr.head.sha}`,
`git push -u origin backport/${issue.number}/${targetBranch}`,
'```'
] : []),
'```js',
err.stack,
'```'
].join('\n')
await updateComment(context, body)
}
}
}
app.on('pull_request_review_comment.created', handler)
app.on('pull_request_review_comment.edited', handler)
app.on('issue_comment.created', handler)
app.on('issue_comment.edited', handler)
}
async function updateComment (context, body) {
const comment = context.payload.comment
const resource = comment.pull_request_review_id ? context.github.pulls : context.github.issues;
return resource.updateComment(context.issue({comment_id: comment.id, body}))
}
const staticBackportRegexp = /^ *\/backport ([a-zA-Z0-9\/\-._]+) *([a-zA-Z0-9\/\-._]+)?$/img
const globalBackportRegexp = /^ *\/backport *$/img
async function matchComments (context, commentBody) {
let body = commentBody
const targetBranches = [...commentBody.matchAll(staticBackportRegexp)]
.map(([command, arg1, arg2]) => ({command, arg1, arg2}))
const globalBranches = [...commentBody.matchAll(globalBackportRegexp)]
.map(([command]) => ({command}))
if (globalBranches.length) {
const pullRequest = await backport.getPullRequest(context)
const backportBranches = (pullRequest.body.match(/Backports?: (.*)/)?.[1]?.split(/[`, ]/).filter(Boolean) || [])
.map((b) => ({command: `/backport ${b}`, arg1: b}))
if (backportBranches.length) {
targetBranches.push(...backportBranches)
body = body.replace(/^( *\/backport)$/im, backportBranches.map((b) => `🕑 ${b.command}`).join('\n'))
} else {
body = body.replace(/^( *\/backport)$/im, `💥 /backport [no branches found in pr body]`)
}
}
body = body.replace(/^ *\/backport(.*)$/img, `🕑 /backport$1`)
return {body, targetBranches}
}