-
Notifications
You must be signed in to change notification settings - Fork 4
77 lines (70 loc) Β· 2.75 KB
/
auto_merge_approved_pr.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
65
66
67
68
69
70
71
72
73
74
75
76
77
name: "Auto Merge Approved PRs"
on:
pull_request_review:
types: [submitted]
jobs:
auto_merge:
runs-on: ubuntu-latest
steps:
- name: "Check Approvals"
id: check
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const pull_number = context.payload.pull_request.number;
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number,
});
const approvals = reviews.data.filter(review => review.state === 'APPROVED');
const approvalCount = approvals.length;
core.info(`PR #${pull_number} has ${approvalCount} approval(s).`);
return approvalCount >= 2;
- name: "Merge PR"
if: ${{ steps.check.outputs.result == 'true' }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pull_number = context.payload.pull_request.number;
// PR μ 보 λ° λ³ν© κ°λ₯ μν νμΈ
let pr;
for (let i = 0; i < 5; i++) { // μ΅λ 5ν μ¬μλ
const { data } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number,
});
pr = data;
if (pr.state !== 'open') {
core.info(`PR #${pull_number} is not open (state: ${pr.state}).`);
return;
}
if (pr.mergeable === true) {
break; // λ³ν© κ°λ₯νλ©΄ 루ν μ’
λ£
} else if (pr.mergeable === false) {
core.info(`PR #${pull_number} cannot be merged due to conflicts or other issues.`);
return;
} else {
// mergeableμ΄ nullμΈ κ²½μ° (κ³μ° μ€), μ μ λκΈ° ν μ¬μλ
await new Promise(resolve => setTimeout(resolve, 2000)); // 2μ΄ λκΈ°
}
}
if (pr.mergeable !== true) {
core.info(`PR #${pull_number} mergeable status is unknown after retries.`);
return;
}
// PR λ³ν© μλ
try {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number,
});
core.info(`PR #${pull_number} has been merged successfully.`);
} catch (error) {
core.info(`Failed to merge PR #${pull_number}: ${error.message}`);
}