forked from linuxdeepin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
421 lines (396 loc) · 16.5 KB
/
chatOps.yml
File metadata and controls
421 lines (396 loc) · 16.5 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
name: chatOps
on:
workflow_call:
env:
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
jobs:
chatopt:
if: contains(github.event.comment.html_url, '/pull/')
runs-on: [ubuntu-latest]
steps:
# assign to someone
- name: assign
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/assign')
env:
COMMENT_BODY: ${{ github.event.comment.body }}
with:
script: |
const { COMMENT_BODY } = process.env
const groups = COMMENT_BODY.match(/\/assign \@(\S+)/)
groups && await github.rest.issues.addAssignees({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
assignees: groups[1]
})
# request someone to review
- name: review
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/review')
env:
COMMENT_BODY: ${{ github.event.comment.body }}
with:
script: |
const { COMMENT_BODY } = process.env
const command = COMMENT_BODY.match(/\/review (\@\w+ )+/)
if ( !command || !command[0] ){
return
}
const users = command[0].match(/\@\w+/g)
if( !users ){
return
}
await github.rest.pulls.requestReviewers({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
reviewers: users.map(a=>a.slice(1))
});
# !deprecated add approve label
- name: "approve"
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/+1')
with:
script: |
if(context.payload.sender.id == context.payload.issue.user.id){
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Permission denied'
})
return
}
await github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['approve']
})
# retrigger checks
- name: check
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/check')
env:
COMMENT_BODY: ${{ github.event.comment.body }}
with:
script: |
const { COMMENT_BODY } = process.env
const checkNameGroup = COMMENT_BODY.match(/\/check (\S+)/)
const pull = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
})
const listResp = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: pull.data.head.sha,
});
for(const run of listResp.data.check_runs){
if(checkNameGroup) {
const input = checkNameGroup[1].startsWith("/") ? checkNameGroup[1] : '/' + checkNameGroup[1]
const name ='/' + run.name.replace(/ \/ /g,'/')
if(name.includes(input)){
console.log(name, "re-check")
await github.request(`POST /repos/${context.repo.owner}/${context.repo.repo}/actions/jobs/${run.id}/rerun`);
} else {
console.log(name, "skip")
}
}else{
await github.request(`POST /repos/${context.repo.owner}/${context.repo.repo}/actions/jobs/${run.id}/rerun`);
}
}
# auto merge pull request
- name: install depends for load scripts
if: |
startsWith(github.event.comment.body, '/merge') || startsWith(github.event.comment.body, '/check') ||
startsWith(github.event.comment.body, '/forcemerge')
run: |
npm install @octokit/rest@19.0.13
npm install @octokit/auth-app@6.1.1
- name: Get token using github-script
if: |
startsWith(github.event.comment.body, '/merge') || startsWith(github.event.comment.body, '/check') ||
startsWith(github.event.comment.body, '/forcemerge')
id: get-token
uses: actions/github-script@v6
with:
script: |
global["fetch"] = fetch
const { Octokit } = require("@octokit/rest");
const { createAppAuth } = require("@octokit/auth-app");
const appOctokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: process.env.APP_ID,
privateKey: process.env.APP_PRIVATE_KEY,
}
});
const app_installation = await appOctokit.rest.apps.getRepoInstallation({
owner: context.payload.organization.login,
repo: context.payload.repository.name
});
const { token } = await appOctokit.auth({
type: "installation",
installationId: app_installation.data.id
});
core.setOutput('app_token', token)
# retrigger obs build webhook
- name: check obs
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/check')
id: check-obs
env:
COMMENT_BODY: ${{ github.event.comment.body }}
HOOK_ID: 389767322
with:
github-token: ${{ steps.get-token.outputs.app_token }}
script: |
const BOT_NAME = "Deepin Obs Bot"
const COMMENT_HEAD = "**" + BOT_NAME + "**\n"
let BODY = "Starting find obs webhook event and retrigger!"
if ( context.issue.number != undefined ) {
const response = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
})
const reg = new RegExp("\\*+" + BOT_NAME + "\\*+")
BotComment= response.data.find(comment => comment.body.match(reg))
if (BotComment) {
await github.rest.issues.deleteComment({
comment_id: BotComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
})
}
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: COMMENT_HEAD + BODY
})
} else {
console.log("context.issue.number is not a valid issue number: " + context.issue.number)
}
const { COMMENT_BODY } = process.env
const checkNameGroup = COMMENT_BODY.match(/\/check (\S+)/)
// handle obs workflow resend
obs_webhook_need_resend = true
if (checkNameGroup && !COMMENT_BODY.includes("obs")) {
obs_webhook_need_resend = false
}
// console.log("HOOK_ID: ", process.env.HOOK_ID)
core.setOutput('retriggered', "true")
if (obs_webhook_need_resend) {
// deliveries obs webhook
let maxWebhooks = 1000
let retriggered = false
for await (const WebhookDeliveriesResp of github.paginate.iterator(
github.rest.orgs.listWebhookDeliveries,
{
org: context.repo.owner,
hook_id: process.env.HOOK_ID,
per_page: 100,
},
(response, done) => {
if (retriggered) {
done();
}
return response.data;
}
)) {
console.log("github repository_id: ", ${{ github.repository_id }})
core.setOutput('retriggered', "false")
for (var i = 0; i < WebhookDeliveriesResp.data.length; i++) {
let wh = WebhookDeliveriesResp.data[i]
console.log("webhook event: ", wh.event, ", webhook repository_id: ", wh.repository_id)
if (wh.event == 'pull_request' && wh.repository_id == ${{ github.repository_id }} && (wh.action == 'opened' || wh.action == 'synchronize')) {
// Get delivery payload and filter
const resp = await github.rest.orgs.getWebhookDelivery({
org: context.repo.owner,
hook_id: process.env.HOOK_ID,
delivery_id: wh.id,
});
//console.log("delivery payload: " + JSON.stringifyesp.data))
console.log("payload number: " + resp.data.request.payload.number)
console.log("pr number: " + context.issue.number)
if (resp.data.request.payload.number == context.issue.number) {
const resp = await github.rest.orgs.redeliverWebhookDelivery({
org: context.repo.owner,
hook_id: process.env.HOOK_ID,
delivery_id: wh.id,
});
console.log("redeliver webhook resp: ", resp)
retriggered = true
core.setOutput('retriggered', "true")
return
}
}
}
}
}
- name: check obs comment
uses: actions/github-script@v6
if: |
startsWith(github.event.comment.body, '/check obs') && steps.check-obs.outputs.retriggered == 'true'
with:
github-token: ${{ steps.get-token.outputs.app_token }}
script: |
console.log("redeliveried failed, context.issue.number: ", context.issue.number)
const BOT_NAME = "Deepin Obs Bot"
const COMMENT_HEAD = "**" + BOT_NAME + "**\n"
let COMMENT_BODY = "Deepin redeliveried failed, need update or recreate pull request!!!"
if ( context.issue.number != undefined ) {
const response = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
})
const reg = new RegExp("\\*+" + BOT_NAME + "\\*+")
BotComment= response.data.find(comment => comment.body.match(reg))
if (BotComment) {
await github.rest.issues.updateComment({
comment_id: BotComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: COMMENT_HEAD + COMMENT_BODY
})
} else {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: COMMENT_HEAD + COMMENT_BODY
})
}
}
- name: disable merge
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/merge') && github.repository_owner != 'linuxdeepin'
with:
github-token: ${{ steps.get-token.outputs.app_token }}
script: |
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Merge" command is disabled`
})
- name: merge
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/merge') && github.repository_owner == 'linuxdeepin'
with:
github-token: ${{ steps.get-token.outputs.app_token }}
script: |
if(context.payload.sender.id != context.payload.issue.user.id){
const permission = await github.request(`GET /repos/${context.repo.owner}/${context.repo.repo}/collaborators/${context.payload.sender.login}/permission`, {
owner: context.repo.owner,
repo: context.repo.repo,
username: context.payload.sender.login,
})
if(!["admin","maintain","write"].includes(permission.data.permission)){
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Permission denied'
})
return
}
}
const pull = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
})
if( pull.data.mergeable_state != "clean" ){
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `This pr cannot be merged! (status: ${pull.data.mergeable_state})`
})
return
}
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
merge_method: 'rebase'
});
- name: forcemerge
uses: actions/github-script@v6
if: startsWith(github.event.comment.body, '/forcemerge') && github.repository_owner == 'linuxdeepin'
with:
github-token: ${{ steps.get-token.outputs.app_token }}
script: |
if(context.payload.sender.id != context.payload.issue.user.id){
const permission = await github.request(`GET /repos/${context.repo.owner}/${context.repo.repo}/collaborators/${context.payload.sender.login}/permission`, {
owner: context.repo.owner,
repo: context.repo.repo,
username: context.payload.sender.login,
})
if(!["admin","maintain","write"].includes(permission.data.permission)){
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Permission denied'
})
return
}
}
const pull = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
})
if( pull.data.mergeable_state != "clean" ){
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `This pr force merged! (status: ${pull.data.mergeable_state})`
})
}
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
merge_method: 'rebase'
});
pr-info:
runs-on: ubuntu-latest
outputs:
ref: ${{ steps.pr.outputs.ref }}
steps:
- name: Get integration app token
id: pr
if: startsWith(github.event.comment.body, '/integrate')
uses: actions/github-script@v6
with:
script: |
const response = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
if (response.data.state != "closed") {
core.setOutput('ref', response.data.head.sha)
} else {
core.setOutput('ref', response.data.merge_commit_sha)
}
integration:
needs:
- pr-info
name: Try pr integration
if: startsWith(github.event.comment.body, '/integrate')
uses: linuxdeepin/.github/.github/workflows/auto-integration-pr.yml@master
secrets: inherit
with:
integrationrepo: ${{ github.repository }}
tagsha: ${{ needs.pr-info.outputs.ref }}