Skip to content

Commit 336eda4

Browse files
committed
Label PR with a size label based on lines changed
1 parent 6786630 commit 336eda4

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright The Conforma Contributors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
---
18+
name: Pull request size
19+
on: pull_request_target
20+
21+
jobs:
22+
size-label:
23+
name: Size label
24+
permissions:
25+
contents: read
26+
pull-requests: write
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Calculate size
30+
run: |
31+
#!/usr/bin/bash
32+
set -euo pipefail
33+
34+
additions=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" | jq '.additions')
35+
deletions=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" | jq '.deletions')
36+
size=$((additions + deletions))
37+
38+
sizelabel=XS
39+
if [ ${size} -gt 600 ]; then
40+
sizelabel="XXL"
41+
elif [ ${size} -gt 240 ]; then
42+
sizelabel="XL"
43+
elif [ ${size} -gt 120 ]; then
44+
sizelabel="L"
45+
elif [ ${size} -gt 60 ]; then
46+
sizelabel="M"
47+
elif [ ${size} -gt 35 ]; then
48+
sizelabel="S"
49+
fi
50+
51+
echo "Removing existing size labels..."
52+
# Get current labels
53+
current_labels=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
54+
-H "Accept: application/vnd.github.v3+json" \
55+
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels | \
56+
jq -r '.[] | select(.name | startswith("size:")) | .name')
57+
58+
# Remove each existing size label (only if labels exist)
59+
if [ -n "$current_labels" ]; then
60+
echo "$current_labels" | while read -r label; do
61+
if [ -n "$label" ]; then
62+
echo "Removing label: $label"
63+
curl -s --fail-with-body -X DELETE -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
64+
-H "Accept: application/vnd.github.v3+json" \
65+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/$(echo "$label" | sed 's/ /%20/g')"
66+
fi
67+
done
68+
else
69+
echo "No existing size labels found"
70+
fi
71+
72+
echo "Adding size: ${sizelabel}"
73+
curl -s --fail-with-body -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
74+
-H "Accept: application/vnd.github.v3+json" \
75+
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
76+
-d "[\"size: ${sizelabel}\"]"

0 commit comments

Comments
 (0)