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

pr-size.yml

202 lines6.4 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 changed lines (additions + deletions).",
28 },
29 {
30 name: "size:S",
31 color: "5ebd3e",
32 description: "10-29 changed lines (additions + deletions).",
33 },
34 {
35 name: "size:M",
36 color: "fbca04",
37 description: "30-99 changed lines (additions + deletions).",
38 },
39 {
40 name: "size:L",
41 color: "fe7d37",
42 description: "100-499 changed lines (additions + deletions).",
43 },
44 {
45 name: "size:XL",
46 color: "d93f0b",
47 description: "500-999 changed lines (additions + deletions).",
48 },
49 {
50 name: "size:XXL",
51 color: "b60205",
52 description: "1,000+ changed lines (additions + deletions).",
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 additions = context.payload.pull_request.additions ?? 0;
135 const deletions = context.payload.pull_request.deletions ?? 0;
136 const changedLines = additions + deletions;
137 const managedLabels = JSON.parse(process.env.PR_SIZE_LABELS_JSON ?? "[]");
138​
139 const managedLabelNames = new Set(managedLabels.map((label) => label.name));
140​
141 const resolveSizeLabel = (totalChangedLines) => {
142 if (totalChangedLines < 10) {
143 return "size:XS";
144 }
145​
146 if (totalChangedLines < 30) {
147 return "size:S";
148 }
149​
150 if (totalChangedLines < 100) {
151 return "size:M";
152 }
153​
154 if (totalChangedLines < 500) {
155 return "size:L";
156 }
157​
158 if (totalChangedLines < 1000) {
159 return "size:XL";
160 }
161​
162 return "size:XXL";
163 };
164​
165 const nextLabelName = resolveSizeLabel(changedLines);
166​
167 const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
168 owner: context.repo.owner,
169 repo: context.repo.repo,
170 issue_number: issueNumber,
171 per_page: 100,
172 });
173​
174 for (const label of currentLabels) {
175 if (!managedLabelNames.has(label.name) || label.name === nextLabelName) {
176 continue;
177 }
178​
179 try {
180 await github.rest.issues.removeLabel({
181 owner: context.repo.owner,
182 repo: context.repo.repo,
183 issue_number: issueNumber,
184 name: label.name,
185 });
186 } catch (removeError) {
187 if (removeError.status !== 404) {
188 throw removeError;
189 }
190 }
191 }
192​
193 if (!currentLabels.some((label) => label.name === nextLabelName)) {
194 await github.rest.issues.addLabels({
195 owner: context.repo.owner,
196 repo: context.repo.repo,
197 issue_number: issueNumber,
198 labels: [nextLabelName],
199 });
200 }
201​
202 core.info(`PR #${issueNumber}: ${changedLines} changed lines -> ${nextLabelName}`);