-
Notifications
You must be signed in to change notification settings - Fork 0
216 lines (190 loc) · 6.99 KB
/
org-required-checks.yml
File metadata and controls
216 lines (190 loc) · 6.99 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
name: org-required-checks
on:
pull_request:
types:
- opened
- reopened
- synchronize
- edited
- ready_for_review
deployment_status:
permissions:
checks: read
contents: read
pull-requests: read
statuses: read
jobs:
verify:
name: Check
if: |
github.event_name == 'pull_request' ||
(github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success')
runs-on: ubuntu-latest
steps:
- name: Wait for merge gates
uses: actions/github-script@v8
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
let pr = context.payload.pull_request;
let pull_number;
let headSha;
if (pr) {
pull_number = pr.number;
headSha = pr.head.sha;
} else {
const deploymentStatus = context.payload.deployment_status;
const deploymentSha = deploymentStatus?.deployment?.sha;
if (!deploymentSha) {
core.info("No pull request or deployment SHA context. Passing check.");
return;
}
headSha = deploymentSha;
const associatedPrs = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner,
repo,
commit_sha: headSha,
});
pr =
associatedPrs.data.find((candidate) => candidate.state === "open") ?? null;
if (!pr) {
core.info(`No open pull request associated with SHA ${headSha}. Passing check.`);
return;
}
pull_number = pr.number;
}
const files = await github.paginate(github.rest.pulls.listFiles, {
owner,
repo,
pull_number,
per_page: 100,
});
const ciIgnoredPatterns = [
/^docs\//,
/^README\.md$/,
/^AGENTS\.md$/,
/^\.codex\//,
/^\.opencode\//,
];
const hasCiRelevantChange = files.some((file) => {
return !ciIgnoredPatterns.some((pattern) => pattern.test(file.filename));
});
const isReleaseVersionBumpPr =
/^chore\/bump-version-/.test(pr.head.ref) &&
(pr.user?.login === "github-actions[bot]" || pr.user?.login === "app/github-actions");
const requiredContexts = ["Analyze (JavaScript/TypeScript)", "Vercel"];
if (hasCiRelevantChange && !isReleaseVersionBumpPr) {
requiredContexts.push("checks", "Preview validation (seed + runtime QA)");
} else if (hasCiRelevantChange) {
requiredContexts.push("Preview validation (seed + runtime QA)");
}
core.info(
`Required contexts: ${requiredContexts.join(", ")} (ciRelevant=${hasCiRelevantChange}, releaseBump=${isReleaseVersionBumpPr})`,
);
const deadline = Date.now() + 20 * 60 * 1000;
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const normalizeStatusContext = (state) => {
if (state === "success") {
return "success";
}
if (state === "failure" || state === "error") {
return "failure";
}
return "pending";
};
const normalizeCheckRun = (checkRun) => {
if (checkRun.status !== "completed") {
return "pending";
}
if (checkRun.conclusion === "success") {
return "success";
}
if (checkRun.conclusion === "neutral") {
return "success";
}
return "failure";
};
while (Date.now() < deadline) {
const [checkRunsResp, statusesResp] = await Promise.all([
github.rest.checks.listForRef({
owner,
repo,
ref: headSha,
per_page: 100,
}),
github.rest.repos.listCommitStatusesForRef({
owner,
repo,
ref: headSha,
per_page: 100,
}),
]);
const checkRunsByName = new Map();
for (const checkRun of checkRunsResp.data.check_runs) {
if (!checkRunsByName.has(checkRun.name)) {
checkRunsByName.set(checkRun.name, checkRun);
}
}
const statusesByContext = new Map();
for (const status of statusesResp.data) {
if (!statusesByContext.has(status.context)) {
statusesByContext.set(status.context, status);
}
}
let hasPending = false;
for (const contextName of requiredContexts) {
const checkRun = checkRunsByName.get(contextName);
if (checkRun) {
const normalized = normalizeCheckRun(checkRun);
if (normalized === "failure") {
core.setFailed(
`${contextName} failed with conclusion '${checkRun.conclusion ?? "unknown"}'.`,
);
return;
}
if (normalized === "pending") {
hasPending = true;
}
continue;
}
const status = statusesByContext.get(contextName);
if (status) {
const normalized = normalizeStatusContext(status.state);
if (normalized === "failure") {
core.setFailed(`${contextName} failed with state '${status.state}'.`);
return;
}
if (normalized === "pending") {
hasPending = true;
}
continue;
}
hasPending = true;
}
if (!hasPending) {
core.info("All required contexts passed.");
return;
}
await sleep(15000);
}
core.setFailed(
`Timed out waiting for required contexts: ${requiredContexts.join(", ")}`,
);
ValidatePrTitle:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
steps:
- name: Validate PR title
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
if [ -z "${PR_TITLE:-}" ]; then
echo "PR title is empty."
exit 1
fi
if [ ${#PR_TITLE} -gt 120 ]; then
echo "PR title must be 120 characters or fewer."
exit 1
fi
echo "PR title validation passed."