-
Notifications
You must be signed in to change notification settings - Fork 947
64 lines (60 loc) · 2.09 KB
/
gh_pr_auto_updater.yml
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
name: '[gh] pull request auto updater'
on:
pull_request_target:
types:
- auto_merge_enabled
push:
jobs:
up_to_date:
if: github.repository == 'Tencent/Hippy'
runs-on: ubuntu-latest
steps:
- name: Updater
uses: actions/[email protected]
with:
# Because the Github App Token, Github Token, and Fine-grained PAT
# cannot obtain the permission to update the workflow files in the head branch,
# so use classic PAT to avoid failures caused by modification of workflow files.
# TODO: use Github App Token or Github Token if permission is granted in the future.
github-token: ${{ secrets.BOT_PAT }}
script: |
const { pulls, repos } = github.rest;
let pull_requests;
switch (context.eventName) {
case 'push': {
pull_requests = (await github.paginate(pulls.list, {
per_page: 100,
state: 'open',
base: '${{ github.ref_name }}',
...context.repo
})).filter(pull => pull.draft === false && pull.auto_merge);
break;
}
case 'pull_request_target': {
const { pull_request } = context.payload;
if (pull_request.draft === true) {
return;
}
pull_requests = [pull_request];
break;
}
default: {
throw new Error(`Unsupported event name: ${context.eventName}`);
break;
}
}
await Promise.all(
pull_requests.map(pull =>
repos.compareCommitsWithBasehead({
...context.repo,
basehead: `${pull.base.label}...${pull.head.label}`,
}).then(({ data: comparison }) => {
if (comparison.behind_by > 0) {
return pulls.updateBranch({
...context.repo,
pull_number: pull.number
}).catch(error => console.error(error));
}
})
)
);