-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (130 loc) Β· 5.33 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
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
name: Auto Merge PR
on:
pull_request_review:
types: [submitted]
pull_request:
types: [labeled, unlabeled]
jobs:
auto-merge:
runs-on: ubuntu-latest
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 approve"));
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' && env.NO_AUTO_MERGE == 'false'
uses: actions/github-script@v6
with:
script: |
const token = process.env.ACCESS_TOKEN;
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
event: 'APPROVE',
body: 'κ³ μνλ€. ππ½',
headers: {
authorization: `token ${token}`
}
});
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
- name: Merge Pull Request on Review
if: |
github.event_name == 'pull_request_review' &&
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';
const prTitle = context.payload.pull_request.title;
const prNumber = context.payload.pull_request.number;
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
merge_method: 'squash',
commit_title: `${prTitle} (#${prNumber})`,
commit_message: 'Auto-merging PR',
sha: context.payload.pull_request.head.sha,
base: targetBranch
});
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
- name: Merge Pull Request on Label
if: |
github.event_name == 'pull_request' &&
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 prTitle = context.payload.pull_request.title;
const prNumber = context.payload.pull_request.number;
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: prNumber,
merge_method: 'squash',
commit_title: `${prTitle} (#${prNumber})`,
commit_message: 'Auto-merging PR',
sha: context.payload.pull_request.head.sha,
base: targetBranch
});
}
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
- name: Merge Pull Request on Unlabel
if: |
github.event_name == 'pull_request' &&
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';
const prTitle = context.payload.pull_request.title;
const prNumber = context.payload.pull_request.number;
if (approved) {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
merge_method: 'squash',
commit_title: `${prTitle} (#${prNumber})`,
commit_message: 'Auto-merging PR',
sha: context.payload.pull_request.head.sha,
base: targetBranch
});
}
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}