Skip to content

Commit f5f175e

Browse files
authored
ci: adapt PR size job to add sizes label (#96)
* ci: adapt PR size job to add sizes label * ci: adapt PR size job to add sizes label * ci: adapt PR size job to add sizes label * ci: adapt PR size job to add sizes label
1 parent 727c028 commit f5f175e

File tree

1 file changed

+119
-8
lines changed

1 file changed

+119
-8
lines changed

.github/workflows/pr-line-check.yml

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ on:
1818
required: false
1919
type: string
2020
default: '(\.lock$)'
21+
xs_max_size:
22+
description: 'Maximum lines for XS size'
23+
required: false
24+
type: number
25+
default: 10
26+
s_max_size:
27+
description: 'Maximum lines for S size'
28+
required: false
29+
type: number
30+
default: 100
31+
m_max_size:
32+
description: 'Maximum lines for M size'
33+
required: false
34+
type: number
35+
default: 500
36+
l_max_size:
37+
description: 'Maximum lines for L size'
38+
required: false
39+
type: number
40+
default: 1000
2141

2242
jobs:
2343
check-lines:
@@ -79,15 +99,106 @@ jobs:
7999
80100
# Calculate additions and deletions across all changes between the base and HEAD,
81101
# filtering out files matching the ignore pattern.
82-
additions=$(git diff "origin/$BASE_BRANCH"...HEAD --numstat | grep -Ev "$ignore_pattern" | awk '{add += $1} END {print add}')
83-
deletions=$(git diff "origin/$BASE_BRANCH"...HEAD --numstat | grep -Ev "$ignore_pattern" | awk '{del += $2} END {print del}')
102+
additions=$(git diff "origin/$BASE_BRANCH"...HEAD --numstat | grep -Ev "$ignore_pattern" | awk '{add += $1} END {print add+0}')
103+
deletions=$(git diff "origin/$BASE_BRANCH"...HEAD --numstat | grep -Ev "$ignore_pattern" | awk '{del += $2} END {print del+0}')
84104
total=$((additions + deletions))
85105
86106
echo "Additions: $additions, Deletions: $deletions, Total: $total"
87-
echo "lines_changed=$total" >> "$GITHUB_OUTPUT"
107+
{
108+
echo "lines_changed=$total"
109+
echo "additions=$additions"
110+
echo "deletions=$deletions"
111+
} >> "$GITHUB_OUTPUT"
88112
89-
max_lines="${{ inputs.max_lines }}"
90-
if [ "$total" -gt "$max_lines" ]; then
91-
echo "Error: Total changed lines ($total) exceed the limit of $max_lines."
92-
exit 1
93-
fi
113+
- name: Check line count limit
114+
uses: actions/github-script@v7
115+
with:
116+
script: |
117+
const total = parseInt('${{ steps.line_count.outputs.lines_changed }}') || 0;
118+
const additions = parseInt('${{ steps.line_count.outputs.additions }}') || 0;
119+
const deletions = parseInt('${{ steps.line_count.outputs.deletions }}') || 0;
120+
121+
// Thresholds from inputs with fallback to defaults
122+
const maxLines = parseInt('${{ inputs.max_lines }}') || 1000;
123+
const xsMaxSize = parseInt('${{ inputs.xs_max_size }}') || 10;
124+
const sMaxSize = parseInt('${{ inputs.s_max_size }}') || 100;
125+
const mMaxSize = parseInt('${{ inputs.m_max_size }}') || 500;
126+
const lMaxSize = parseInt('${{ inputs.l_max_size }}') || 1000;
127+
128+
// Print summary
129+
console.log('Summary:');
130+
console.log(` - Additions: ${additions}`);
131+
console.log(` - Deletions: ${deletions}`);
132+
console.log(` - Total: ${total}`);
133+
console.log(` - Limit: ${maxLines}`);
134+
135+
// Determine size label based on configured criteria
136+
let sizeLabel = '';
137+
if (total <= xsMaxSize) {
138+
sizeLabel = 'size-XS';
139+
} else if (total <= sMaxSize) {
140+
sizeLabel = 'size-S';
141+
} else if (total <= mMaxSize) {
142+
sizeLabel = 'size-M';
143+
} else if (total <= lMaxSize) {
144+
sizeLabel = 'size-L';
145+
} else {
146+
sizeLabel = 'size-XL';
147+
}
148+
149+
console.log(` - Size category: ${sizeLabel}`);
150+
151+
// Manage PR labels
152+
const owner = context.repo.owner;
153+
const repo = context.repo.repo;
154+
const issue_number = context.payload.pull_request.number;
155+
156+
try {
157+
const existingSizeLabels = ['size-XS', 'size-S', 'size-M', 'size-L', 'size-XL'];
158+
159+
// Get current labels
160+
const currentLabels = await github.rest.issues.listLabelsOnIssue({
161+
owner,
162+
repo,
163+
issue_number
164+
});
165+
166+
const currentLabelNames = currentLabels.data.map(l => l.name);
167+
168+
// Build new label set: keep non-size labels and add the new size label
169+
const newLabels = currentLabelNames
170+
.filter(name => !existingSizeLabels.includes(name)) // Remove all size labels
171+
.concat(sizeLabel); // Add the correct size label
172+
173+
// Check if labels need updating
174+
const currentSizeLabel = currentLabelNames.find(name => existingSizeLabels.includes(name));
175+
if (currentSizeLabel === sizeLabel && currentLabelNames.length === newLabels.length) {
176+
console.log(`✅ Correct label '${sizeLabel}' already present, no changes needed`);
177+
} else {
178+
// Update all labels in a single API call
179+
await github.rest.issues.setLabels({
180+
owner,
181+
repo,
182+
issue_number,
183+
labels: newLabels
184+
});
185+
186+
if (currentSizeLabel && currentSizeLabel !== sizeLabel) {
187+
console.log(` - Replaced '${currentSizeLabel}' with '${sizeLabel}'`);
188+
} else if (!currentSizeLabel) {
189+
console.log(`✅ Added '${sizeLabel}' label to PR #${issue_number}`);
190+
} else {
191+
console.log(`✅ Updated labels for PR #${issue_number}`);
192+
}
193+
}
194+
} catch (error) {
195+
console.log(`⚠️ Could not manage labels: ${error.message}`);
196+
}
197+
198+
// Check if exceeds limit
199+
if (total > maxLines) {
200+
console.log(`❌ Error: Total changed lines (${total}) exceed the limit of ${maxLines}.`);
201+
process.exit(1);
202+
} else {
203+
console.log(`✅ Success: Total changed lines (${total}) are within the limit of ${maxLines}.`);
204+
}

0 commit comments

Comments
 (0)