-
Notifications
You must be signed in to change notification settings - Fork 4
193 lines (169 loc) · 8.44 KB
/
auto-assign-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
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
name: Auto Assign, Review, and Merge
on:
pull_request:
types:
- opened
- labeled
- unlabeled
- review_requested
- review_request_removed
pull_request_review:
types:
- submitted
jobs:
auto-assign:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "16"
- name: Assign PR creator as Assignee
uses: actions/github-script@v6
with:
script: |
const prNumber = context.payload.pull_request.number;
const currentAssignees = context.payload.pull_request.assignees.map(a => a.login);
// PR 작성자가 이미 담당자로 지정되어 있지 않은 경우에만 할당
if (!currentAssignees.includes(context.actor)) {
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
assignees: [context.actor]
});
}
auto-reviewers:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
needs: auto-assign
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "16"
- name: Add reviewers based on labels
uses: actions/github-script@v6
with:
script: |
const prNumber = context.payload.pull_request.number;
const prAuthor = context.payload.pull_request.user.login;
const assignees = context.payload.pull_request.assignees.map(a => a.login);
// 현재 리뷰어 목록 가져오기
const currentReviewers = context.payload.pull_request.requested_reviewers
? context.payload.pull_request.requested_reviewers.map(r => r.login)
: [];
const BE_reviewers = ['summersummerwhy', 'ezcolin2', 'Tolerblanc'];
const FE_reviewers = ['yewonJin', 'djk01281'];
const doc_reviewers = ['summersummerwhy', 'ezcolin2', 'Tolerblanc', 'yewonJin', 'djk01281'];
// Function to filter out assignees, PR author, and current reviewers
const filterReviewers = (reviewers) => {
return reviewers.filter(r =>
!assignees.includes(r) &&
r !== prAuthor &&
!currentReviewers.includes(r)
);
};
// Check the labels on the PR and assign appropriate reviewers
const labels = context.payload.pull_request.labels.map(label => label.name);
let reviewersToAdd = [];
if (labels.includes('🐧🚀😶🌫️ BE')) {
reviewersToAdd.push(...filterReviewers(BE_reviewers));
}
if (labels.includes('🐳🐣 FE')) {
reviewersToAdd.push(...filterReviewers(FE_reviewers));
}
if (labels.includes('📚 Documentation')) {
reviewersToAdd.push(...filterReviewers(doc_reviewers));
}
// Remove duplicates and limit the number of reviewers
reviewersToAdd = [...new Set(reviewersToAdd)];
const maxReviewers = 4; // 최대 리뷰어 수 제한
reviewersToAdd = reviewersToAdd.slice(0, maxReviewers);
// Request reviewers only if there are new reviewers to add
if (reviewersToAdd.length > 0) {
try {
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
reviewers: reviewersToAdd
});
console.log(`Added reviewers: ${reviewersToAdd.join(', ')}`);
} catch (error) {
console.error('Failed to add reviewers:', error);
// 실패해도 워크플로우는 계속 진행
}
} else {
console.log('No new reviewers to add');
}
auto-merge:
if: github.event_name == 'pull_request_review' && github.event.review.state == 'approved'
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Merge and Close PR if Approved
uses: actions/github-script@v6
with:
script: |
const prNumber = context.payload.pull_request.number || context.payload.review.pull_request_number;
try {
// PR 정보 가져오기
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
if (pr.data.merged) {
console.log('PR이 이미 머지되었습니다.');
return;
}
// 머지 가능 상태 확인
if (!pr.data.mergeable) {
console.log('PR에 충돌이 있습니다. 수동 확인이 필요합니다.');
core.setFailed('PR에 충돌이 있어 자동 머지를 진행할 수 없습니다.');
return;
}
// 모든 리뷰 확인
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
// 각 리뷰어의 최신 리뷰 상태만 확인
const latestReviews = new Map();
reviews.data.forEach(review => {
latestReviews.set(review.user.login, review.state);
});
const hasRejection = Array.from(latestReviews.values()).includes('CHANGES_REQUESTED');
const approvalCount = Array.from(latestReviews.values()).filter(state => state === 'APPROVED').length;
if (!hasRejection && approvalCount >= 1) {
console.log('PR 머지를 시도합니다...');
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
// 브랜치 삭제
const branchName = pr.data.head.ref;
if (branchName !== 'main' && branchName !== 'master') {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${branchName}`
});
}
} else {
console.log('머지 조건이 충족되지 않았습니다.');
}
} catch (error) {
console.error('Error:', error);
core.setFailed(error.message);
}