-
Notifications
You must be signed in to change notification settings - Fork 742
305 lines (266 loc) · 11.1 KB
/
Copy pathpr-merge-guidance.yml
File metadata and controls
305 lines (266 loc) · 11.1 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
name: PR Merge Guidance
on:
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review, converted_to_draft]
push:
branches:
- main
- develop
workflow_dispatch:
inputs:
pr_number:
description: Pull request number
required: true
type: number
dry_run:
description: Log the comment instead of writing it
required: false
default: true
type: boolean
permissions:
contents: read
issues: write
pull-requests: write
concurrency:
group: pr-merge-guidance-${{ github.event.pull_request.number || github.event.inputs.pr_number || github.ref_name }}
cancel-in-progress: false
jobs:
comment:
if: ${{ github.event_name == 'workflow_dispatch' || vars.PR_MERGE_GUIDANCE_ENABLED == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Post PR merge guidance
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3
env:
PR_NUMBER: ${{ github.event.pull_request.number || github.event.inputs.pr_number || '' }}
BASE_BRANCH: ${{ github.event_name == 'push' && github.ref_name || '' }}
DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true' || 'false' }}
MIN_CREATED_AT: ${{ vars.PR_MERGE_GUIDANCE_MIN_CREATED_AT || '2026-04-01T00:00:00Z' }}
COMMENT_MARKER: "### PR merge guidance"
CONFLICT_LABEL: "needs: rebase"
SIGNING_LABEL: "needs: signing"
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const marker = process.env.COMMENT_MARKER;
const conflictLabel = process.env.CONFLICT_LABEL;
const signingLabel = process.env.SIGNING_LABEL;
const requestedPullNumber = Number(process.env.PR_NUMBER);
const baseBranch = process.env.BASE_BRANCH;
const dryRun = process.env.DRY_RUN === "true";
const minCreatedAt = new Date(process.env.MIN_CREATED_AT);
if (requestedPullNumber) {
await processPullRequest(requestedPullNumber);
} else if (baseBranch) {
const openPullRequests = await github.paginate(github.rest.pulls.list, {
owner,
repo,
state: "open",
base: baseBranch,
per_page: 100,
});
for (const openPullRequest of openPullRequests) {
try {
await processPullRequest(openPullRequest.number);
} catch (error) {
const errorDetails = error?.stack || error?.message || String(error);
core.warning(`PR #${openPullRequest.number}: failed to process merge guidance: ${errorDetails}`);
}
}
} else {
core.setFailed("Pull request number was not provided.");
return;
}
async function processPullRequest(pull_number) {
const { data: pr } = await github.rest.pulls.get({
owner,
repo,
pull_number,
});
if (pr.state !== "open") {
core.info(`Skipping PR #${pull_number}: state is ${pr.state}.`);
return;
}
if (new Date(pr.created_at) < minCreatedAt) {
if (dryRun) {
core.info(`Would skip PR #${pull_number}: created at ${pr.created_at}, before cutoff ${process.env.MIN_CREATED_AT}.`);
}
return;
}
const existingComment = await findExistingComment(pull_number);
if (pr.draft) {
if (dryRun) {
core.info(`Would delete existing guidance comment and remove labels for draft PR #${pull_number}: ${Boolean(existingComment)}`);
return;
}
await removeLabel(pull_number, conflictLabel);
await removeLabel(pull_number, signingLabel);
await deleteExistingComment(existingComment);
return;
}
const prState = await getPullRequestState(pull_number);
const commits = await github.paginate(github.rest.pulls.listCommits, {
owner,
repo,
pull_number,
per_page: 100,
});
const unverifiedCommits = commits.filter((commit) => {
return !commit.commit?.verification?.verified;
});
const items = [];
const author = prState.author?.login || pr.user?.login;
const authorMention = author ? `@${author} ` : "";
const baseRef = prState.baseRefName || pr.base.ref;
if (prState.mergeable === "CONFLICTING") {
items.push(
`- This branch has merge conflicts with \`${baseRef}\`. Please rebase your branch on the latest \`${baseRef}\`, resolve the conflicts locally, and force-push the updated branch.`
);
}
if (unverifiedCommits.length > 0) {
const exampleShas = unverifiedCommits
.slice(0, 5)
.map((commit) => `\`${commit.sha.slice(0, 7)}\``)
.join(", ");
const extraCount = unverifiedCommits.length > 5 ? ` and ${unverifiedCommits.length - 5} more` : "";
items.push(
`- ${unverifiedCommits.length} commit${unverifiedCommits.length === 1 ? " does" : "s do"} not have a verified signature (${exampleShas}${extraCount}). Please sign the commits and force-push the updated branch.`
);
}
const hasConflict = prState.mergeable === "CONFLICTING";
const mergeableKnown = prState.mergeable !== "UNKNOWN";
const hasUnsigned = unverifiedCommits.length > 0;
if (dryRun) {
core.info(
`Would set labels for PR #${pull_number}: "${conflictLabel}"=${mergeableKnown ? hasConflict : "skipped (mergeable unknown)"}, "${signingLabel}"=${hasUnsigned}.`
);
} else {
if (mergeableKnown) {
await (hasConflict ? addLabel(pull_number, conflictLabel) : removeLabel(pull_number, conflictLabel));
}
await (hasUnsigned ? addLabel(pull_number, signingLabel) : removeLabel(pull_number, signingLabel));
}
if (items.length === 0) {
if (!mergeableKnown) {
core.info(`PR #${pull_number}: no signing issues and mergeable is UNKNOWN; leaving existing guidance comment untouched.`);
return;
}
if (dryRun) {
core.info(`Would delete existing guidance comment for PR #${pull_number}: ${Boolean(existingComment)}`);
return;
}
await deleteExistingComment(existingComment);
return;
}
const body = [
marker,
"",
`${authorMention}thanks for the PR. GitHub is currently blocking merge for one or more repository requirements:`,
"",
...items,
"",
"Relevant guide:",
`- Signed commits: https://github.com/${owner}/${repo}/blob/develop/CONTRIBUTING.md#commit-signing`,
`- Contribution guide: https://github.com/${owner}/${repo}/blob/develop/CONTRIBUTING.md`,
].join("\n");
if (existingComment) {
if (existingComment.body !== body) {
if (dryRun) {
core.info(`Would update guidance comment for PR #${pull_number}:\n${body}`);
return;
}
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body,
});
}
} else {
if (dryRun) {
core.info(`Would create guidance comment for PR #${pull_number}:\n${body}`);
return;
}
await github.rest.issues.createComment({
owner,
repo,
issue_number: pull_number,
body,
});
}
}
async function getPullRequestState(pull_number) {
const query = `
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
author {
login
}
baseRefName
mergeable
}
}
}
`;
let result = null;
for (let attempt = 0; attempt < 3; attempt += 1) {
result = await github.graphql(query, {
owner,
repo,
number: pull_number,
});
if (result.repository.pullRequest.mergeable !== "UNKNOWN") {
break;
}
await new Promise((resolve) => setTimeout(resolve, 2000));
}
if (result.repository.pullRequest.mergeable === "UNKNOWN") {
core.warning(`PR #${pull_number}: mergeable state is still UNKNOWN after retries; conflict check skipped.`);
}
return result.repository.pullRequest;
}
async function findExistingComment(pull_number) {
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: pull_number,
per_page: 100,
});
return comments.find((comment) => {
return comment.user?.type === "Bot" && comment.body?.startsWith(marker);
});
}
async function deleteExistingComment(comment) {
if (!comment) {
return;
}
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
async function addLabel(pull_number, name) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: pull_number,
labels: [name],
});
}
async function removeLabel(pull_number, name) {
try {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: pull_number,
name,
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
}