Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pr-size.yml

292 lines9.9 KB

1name: PR Size
2​
3on:
4 pull_request_target:
5 types: [opened, reopened, synchronize, ready_for_review, converted_to_draft]
6​
7permissions:
8 contents: read
9​
10jobs:
11 prepare-config:
12 name: Prepare PR size config
13 runs-on: ubuntu-24.04
14 outputs:
15 labels_json: ${{ steps.config.outputs.labels_json }}
16 steps:
17 - id: config
18 name: Build PR size label config
19 uses: actions/github-script@v8
20 with:
21 result-encoding: string
22 script: |
23 const managedLabels = [
24 {
25 name: "size:XS",
26 color: "0e8a16",
27 description: "0-9 effective changed lines (test files excluded in mixed PRs).",
28 },
29 {
30 name: "size:S",
31 color: "5ebd3e",
32 description: "10-29 effective changed lines (test files excluded in mixed PRs).",
33 },
34 {
35 name: "size:M",
36 color: "fbca04",
37 description: "30-99 effective changed lines (test files excluded in mixed PRs).",
38 },
39 {
40 name: "size:L",
41 color: "fe7d37",
42 description: "100-499 effective changed lines (test files excluded in mixed PRs).",
43 },
44 {
45 name: "size:XL",
46 color: "d93f0b",
47 description: "500-999 effective changed lines (test files excluded in mixed PRs).",
48 },
49 {
50 name: "size:XXL",
51 color: "b60205",
52 description: "1,000+ effective changed lines (test files excluded in mixed PRs).",
53 },
54 ];
55​
56 core.setOutput("labels_json", JSON.stringify(managedLabels));
57 sync-label-definitions:
58 name: Sync PR size label definitions
59 needs: prepare-config
60 if: github.event_name != 'pull_request_target'
61 runs-on: ubuntu-24.04
62 permissions:
63 contents: read
64 issues: write
65 steps:
66 - name: Ensure PR size labels exist
67 uses: actions/github-script@v8
68 env:
69 PR_SIZE_LABELS_JSON: ${{ needs.prepare-config.outputs.labels_json }}
70 with:
71 script: |
72 const managedLabels = JSON.parse(process.env.PR_SIZE_LABELS_JSON ?? "[]");
73​
74 for (const label of managedLabels) {
75 try {
76 const { data: existing } = await github.rest.issues.getLabel({
77 owner: context.repo.owner,
78 repo: context.repo.repo,
79 name: label.name,
80 });
81​
82 if (
83 existing.color !== label.color ||
84 (existing.description ?? "") !== label.description
85 ) {
86 await github.rest.issues.updateLabel({
87 owner: context.repo.owner,
88 repo: context.repo.repo,
89 name: label.name,
90 color: label.color,
91 description: label.description,
92 });
93 }
94 } catch (error) {
95 if (error.status !== 404) {
96 throw error;
97 }
98​
99 try {
100 await github.rest.issues.createLabel({
101 owner: context.repo.owner,
102 repo: context.repo.repo,
103 name: label.name,
104 color: label.color,
105 description: label.description,
106 });
107 } catch (createError) {
108 if (createError.status !== 422) {
109 throw createError;
110 }
111 }
112 }
113 }
114 label:
115 name: Label PR size
116 needs: prepare-config
117 if: github.event_name == 'pull_request_target'
118 runs-on: ubuntu-24.04
119 permissions:
120 contents: read
121 issues: read
122 pull-requests: write
123 concurrency:
124 group: pr-size-${{ github.event.pull_request.number }}
125 cancel-in-progress: true
126 steps:
127 - name: Checkout base repository
128 uses: actions/checkout@v4
129 with:
130 fetch-depth: 0
131 - name: Sync PR size label
132 uses: actions/github-script@v8
133 env:
134 PR_SIZE_LABELS_JSON: ${{ needs.prepare-config.outputs.labels_json }}
135 with:
136 script: |
137 const { execFileSync } = require("node:child_process");
138​
139 const issueNumber = context.payload.pull_request.number;
140 const baseSha = context.payload.pull_request.base.sha;
141 const headSha = context.payload.pull_request.head.sha;
142 const headTrackingRef = `refs/remotes/pr-size/${issueNumber}`;
143 const managedLabels = JSON.parse(process.env.PR_SIZE_LABELS_JSON ?? "[]");
144 const managedLabelNames = new Set(managedLabels.map((label) => label.name));
145 // Keep this aligned with the repo's test entrypoints and test-only support files.
146 const testExcludePathspecs = [
147 ":(glob,exclude)**/__tests__/**",
148 ":(glob,exclude)**/test/**",
149 ":(glob,exclude)**/tests/**",
150 ":(glob,exclude)apps/server/integration/**",
151 ":(glob,exclude)**/*.test.*",
152 ":(glob,exclude)**/*.spec.*",
153 ":(glob,exclude)**/*.browser.*",
154 ":(glob,exclude)**/*.integration.*",
155 ];
156​
157 const sumNumstat = (text) =>
158 text
159 .split("\n")
160 .filter(Boolean)
161 .reduce((total, line) => {
162 const [insertionsRaw = "0", deletionsRaw = "0"] = line.split("\t");
163 const additions =
164 insertionsRaw === "-" ? 0 : Number.parseInt(insertionsRaw, 10) || 0;
165 const deletions =
166 deletionsRaw === "-" ? 0 : Number.parseInt(deletionsRaw, 10) || 0;
167​
168 return total + additions + deletions;
169 }, 0);
170​
171 const resolveSizeLabel = (totalChangedLines) => {
172 if (totalChangedLines < 10) {
173 return "size:XS";
174 }
175​
176 if (totalChangedLines < 30) {
177 return "size:S";
178 }
179​
180 if (totalChangedLines < 100) {
181 return "size:M";
182 }
183​
184 if (totalChangedLines < 500) {
185 return "size:L";
186 }
187​
188 if (totalChangedLines < 1000) {
189 return "size:XL";
190 }
191​
192 return "size:XXL";
193 };
194​
195 execFileSync("git", ["fetch", "--no-tags", "origin", baseSha], {
196 stdio: "inherit",
197 });
198​
199 execFileSync(
200 "git",
201 ["fetch", "--no-tags", "origin", `+refs/pull/${issueNumber}/head:${headTrackingRef}`],
202 {
203 stdio: "inherit",
204 },
205 );
206​
207 const resolvedHeadSha = execFileSync("git", ["rev-parse", headTrackingRef], {
208 encoding: "utf8",
209 }).trim();
210​
211 if (resolvedHeadSha !== headSha) {
212 core.warning(
213 `Fetched head SHA ${resolvedHeadSha} does not match pull request head SHA ${headSha}; using fetched ref for sizing.`,
214 );
215 }
216​
217 execFileSync("git", ["cat-file", "-e", `${baseSha}^{commit}`], {
218 stdio: "inherit",
219 });
220​
221 const diffArgs = [
222 "diff",
223 "--numstat",
224 "--ignore-all-space",
225 "--ignore-blank-lines",
226 `${baseSha}...${resolvedHeadSha}`,
227 ];
228​
229 const totalChangedLines = sumNumstat(
230 execFileSync(
231 "git",
232 diffArgs,
233 { encoding: "utf8" },
234 ),
235 );
236 const nonTestChangedLines = sumNumstat(
237 execFileSync("git", [...diffArgs, "--", ".", ...testExcludePathspecs], {
238 encoding: "utf8",
239 }),
240 );
241 const testChangedLines = Math.max(0, totalChangedLines - nonTestChangedLines);
242​
243 const changedLines = nonTestChangedLines === 0 ? testChangedLines : nonTestChangedLines;
244 const nextLabelName = resolveSizeLabel(changedLines);
245​
246 const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
247 owner: context.repo.owner,
248 repo: context.repo.repo,
249 issue_number: issueNumber,
250 per_page: 100,
251 });
252​
253 for (const label of currentLabels) {
254 if (!managedLabelNames.has(label.name) || label.name === nextLabelName) {
255 continue;
256 }
257​
258 try {
259 await github.rest.issues.removeLabel({
260 owner: context.repo.owner,
261 repo: context.repo.repo,
262 issue_number: issueNumber,
263 name: label.name,
264 });
265 } catch (removeError) {
266 if (removeError.status !== 404) {
267 throw removeError;
268 }
269 }
270 }
271​
272 if (!currentLabels.some((label) => label.name === nextLabelName)) {
273 await github.rest.issues.addLabels({
274 owner: context.repo.owner,
275 repo: context.repo.repo,
276 issue_number: issueNumber,
277 labels: [nextLabelName],
278 });
279 }
280​
281 const classification =
282 nonTestChangedLines === 0
283 ? testChangedLines > 0
284 ? "test-only PR"
285 : "no line changes"
286 : testChangedLines > 0
287 ? "test lines excluded"
288 : "all non-test changes";
289​
290 core.info(
291 `PR #${issueNumber}: ${nonTestChangedLines} non-test lines, ${testChangedLines} test lines, ${changedLines} effective lines -> ${nextLabelName} (${classification})`,
292 );