Open History to see the last commit
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: Sync PR size label
128 uses: actions/github-script@v8
129 env:
130 PR_SIZE_LABELS_JSON: ${{ needs.prepare-config.outputs.labels_json }}
131 with:
132 script: |
133 const issueNumber = context.payload.pull_request.number;
134 const managedLabels = JSON.parse(process.env.PR_SIZE_LABELS_JSON ?? "[]");
135 const managedLabelNames = new Set(managedLabels.map((label) => label.name));
136 // Keep this aligned with the repo's test entrypoints and test-only support files.
137 const testFilePatterns = [
138 /(^|\/)__tests__(\/|$)/,
139 /(^|\/)tests?(\/|$)/,
140 /^apps\/server\/integration\//,
141 /\.(test|spec|browser|integration)\.[^.\/]+$/,
142 ];
143
144 const isTestFile = (filename) =>
145 testFilePatterns.some((pattern) => pattern.test(filename));
146
147 const resolveSizeLabel = (totalChangedLines) => {
148 if (totalChangedLines < 10) {
149 return "size:XS";
150 }
151
152 if (totalChangedLines < 30) {
153 return "size:S";
154 }
155
156 if (totalChangedLines < 100) {
157 return "size:M";
158 }
159
160 if (totalChangedLines < 500) {
161 return "size:L";
162 }
163
164 if (totalChangedLines < 1000) {
165 return "size:XL";
166 }
167
168 return "size:XXL";
169 };
170
171 const files = await github.paginate(
172 github.rest.pulls.listFiles,
173 {
174 owner: context.repo.owner,
175 repo: context.repo.repo,
176 pull_number: issueNumber,
177 per_page: 100,
178 },
179 (response) => response.data,
180 );
181
182 if (files.length >= 3000) {
183 core.warning(
184 "The GitHub pull request files API may truncate results at 3,000 files; PR size may be undercounted.",
185 );
186 }
187
188 let testChangedLines = 0;
189 let nonTestChangedLines = 0;
190
191 for (const file of files) {
192 const changedLinesForFile = (file.additions ?? 0) + (file.deletions ?? 0);
193
194 if (changedLinesForFile === 0) {
195 continue;
196 }
197
198 if (isTestFile(file.filename)) {
199 testChangedLines += changedLinesForFile;
200 continue;
201 }
202
203 nonTestChangedLines += changedLinesForFile;
204 }
205
206 const changedLines = nonTestChangedLines === 0 ? testChangedLines : nonTestChangedLines;
207 const nextLabelName = resolveSizeLabel(changedLines);
208
209 const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
210 owner: context.repo.owner,
211 repo: context.repo.repo,
212 issue_number: issueNumber,
213 per_page: 100,
214 });
215
216 for (const label of currentLabels) {
217 if (!managedLabelNames.has(label.name) || label.name === nextLabelName) {
218 continue;
219 }
220
221 try {
222 await github.rest.issues.removeLabel({
223 owner: context.repo.owner,
224 repo: context.repo.repo,
225 issue_number: issueNumber,
226 name: label.name,
227 });
228 } catch (removeError) {
229 if (removeError.status !== 404) {
230 throw removeError;
231 }
232 }
233 }
234
235 if (!currentLabels.some((label) => label.name === nextLabelName)) {
236 await github.rest.issues.addLabels({
237 owner: context.repo.owner,
238 repo: context.repo.repo,
239 issue_number: issueNumber,
240 labels: [nextLabelName],
241 });
242 }
243
244 const classification =
245 nonTestChangedLines === 0
246 ? testChangedLines > 0
247 ? "test-only PR"
248 : "no line changes"
249 : testChangedLines > 0
250 ? "test lines excluded"
251 : "all non-test changes";
252
253 core.info(
254 `PR #${issueNumber}: ${nonTestChangedLines} non-test lines, ${testChangedLines} test lines, ${changedLines} effective lines -> ${nextLabelName} (${classification})`,
255 );