-
Notifications
You must be signed in to change notification settings - Fork 0
116 lines (103 loc) ยท 4.65 KB
/
auto-merge.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
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
name: Auto Merge PR
on:
pull_request_review:
types: [submitted]
pull_request:
types: [labeled, unlabeled]
jobs:
auto-merge:
runs-on: ubuntu-latest
if: github.event.action == 'submitted' || github.event.action == 'labeled' || github.event.action == 'unlabeled'
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Check Labels
id: check-labels
uses: actions/github-script@v6
with:
script: |
const labels = context.payload.pull_request.labels.map(label => label.name);
core.setOutput("no_auto_merge", labels.includes("no auto merge"));
core.setOutput("auto_merge", labels.includes("auto merge"));
core.setOutput("main_merge", labels.includes("main merge"));
- name: Set Environment Variables
run: |
echo "NO_AUTO_MERGE=${{ steps.check-labels.outputs.no_auto_merge }}" >> $GITHUB_ENV
echo "AUTO_MERGE=${{ steps.check-labels.outputs.auto_merge }}" >> $GITHUB_ENV
echo "MAIN_MERGE=${{ steps.check-labels.outputs.main_merge }}" >> $GITHUB_ENV
- name: Auto Approve if Auto Merge Label Exists
if: env.AUTO_MERGE == 'true'
uses: hmarr/auto-approve-action@v2.0.0
with:
github-token: ${{ secrets.PUBLIC_ACCOUNT_TOKEN }}
- name: Merge Pull Request on Review
if: github.event.action == 'submitted' && github.event.review.state == 'approved' && env.NO_AUTO_MERGE == 'false'
uses: actions/github-script@v6
with:
script: |
const targetBranch = process.env.MAIN_MERGE === 'true' ? 'main' : 'dev';
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
merge_method: 'squash',
commit_title: `Merge pull request #${context.payload.pull_request.number} into ${targetBranch}`,
commit_message: 'Auto-merging PR',
sha: context.payload.pull_request.head.sha,
base: targetBranch
});
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Merge Pull Request on Label
if: github.event.action == 'labeled' && env.NO_AUTO_MERGE == 'false'
uses: actions/github-script@v6
with:
script: |
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
const approved = reviews.some(review => review.state === 'APPROVED');
const targetBranch = process.env.MAIN_MERGE === 'true' ? 'main' : 'dev';
if (approved) {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
merge_method: 'squash',
commit_title: `Merge pull request #${context.payload.pull_request.number} into ${targetBranch}`,
commit_message: 'Auto-merging PR',
sha: context.payload.pull_request.head.sha,
base: targetBranch
});
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Merge Pull Request on Unlabel
if: github.event.action == 'unlabeled' && github.event.label.name == 'no auto merge'
uses: actions/github-script@v6
with:
script: |
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
const approved = reviews.some(review => review.state === 'APPROVED');
const labels = context.payload.pull_request.labels.map(label => label.name);
const targetBranch = labels.includes("main merge") ? 'main' : 'dev';
if (approved) {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
merge_method: 'squash',
commit_title: `Merge pull request #${context.payload.pull_request.number} into ${targetBranch}`,
commit_message: 'Auto-merging PR',
sha: context.payload.pull_request.head.sha,
base: targetBranch
});
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}