-
Notifications
You must be signed in to change notification settings - Fork 77
187 lines (175 loc) · 6.37 KB
/
labeler.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
# Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
name: "Pull Request Labeler"
on:
- pull_request
jobs:
dump-contexts-to-log:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/dump-context
triage:
name: Triage
runs-on: ubuntu-latest
if: ${{ (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Set Git config
run: |
git config --global --add safe.directory ${GITHUB_WORKSPACE}
- uses: actions/labeler@v5
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
configuration-path: ".github/labeler.yaml"
- name: Add labels
run: |
pr_num=`cat $GITHUB_EVENT_PATH | jq -r ".number"`
branch_name=`cat $GITHUB_EVENT_PATH | jq -r ".pull_request.head.ref"`
type_labels=($(curl -s "https://api.github.com/repos/${REPOSITORY}/labels?per_page=5000" | jq -r ".[].name" | grep -e "^type/"))
send_labels=()
git fetch origin main
echo "branch name: " $branch_name
for label in ${type_labels[@]}
do
if [[ "${label#type/}" =~ "${branch_name%%/*}" ]] || [[ "${branch_name%%/*}" =~ "${label#type/}" ]]; then
send_labels+=(\"$label\")
fi
done
size=0
diff=`git diff --shortstat origin/main...${branch_name} ':!./*\.yaml' ':!./*\.yml' ':!./*\.svg' ':!./apis/*' ':!./design/*'`
if [ -n "$diff" ]; then
echo $diff
size=`echo $diff | awk '{print $4+$6}'`
fi
echo "pull request size: " $size
if [ $size -le 29 ]; then
send_labels+=(\"size/S\")
elif [ $size -le 99 ]; then
send_labels+=(\"size/M\")
elif [ $size -le 499 ]; then
send_labels+=(\"size/L\")
elif [ $size -le 999 ]; then
send_labels+=(\"size/XL\")
elif [ $size -le 4999 ]; then
send_labels+=(\"size/XXL\")
else
send_labels+=(\"size/XXXL\")
fi
if [ ${#send_labels[@]} -gt 0 ]; then
str="$(IFS=,; echo "${send_labels[*]}")"
data="{\"labels\": [$str]}"
echo "send data: " $data
curl --request POST \
--url "https://api.github.com/repos/${REPOSITORY}/issues/${pr_num}/labels" \
--header "authorization: Bearer ${GITHUB_TOKEN}" \
--header 'content-type: application/json' \
--data "$data"
fi
env:
REPOSITORY: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
remove-duplication:
name: Remove duplication
needs:
- triage
runs-on: ubuntu-latest
steps:
- name: Remove duplication
run: |
declare -A SIZES
declare -A PRIORITIES
SIZES=(
["size/S"]=0
["size/M"]=30
["size/L"]=100
["size/XL"]=500
["size/XXL"]=1000
["size/XXXL"]=5000
)
PRIORITIES=(
["priority/low"]=10
["priority/medium"]=20
["priority/high"]=30
)
pr_num=`cat $GITHUB_EVENT_PATH | jq -r ".number"`
existing_labels=($(curl -s "https://api.github.com/repos/${REPOSITORY}/issues/${pr_num}/labels" | jq -r ".[].name"))
send_labels=()
size_label="size/S"
priority_label="priority/low"
for label in ${existing_labels[@]}
do
if [ "${label%%/*}" = "size" ]; then
if [ -z "${size_label}" ]; then
size_label=$label
else
c="${SIZES[$size_label]}"
s="${SIZES[$label]}"
if [ ! -z "${s}" ] && [ $c -lt $s ]; then
size_label=$label
fi
fi
elif [ "${label%%/*}" = "priority" ]; then
if [ -z "${priority_label}" ]; then
priority_label=$label
else
c="${PRIORITIES[$priority_label]}"
s="${PRIORITIES[$label]}"
if [ ! -z "${s}" ] && [ $c -lt $s ]; then
priority_label=$label
fi
fi
else
send_labels+=(\"$label\")
fi
if [ "${label}" = "type/security" ]; then
l="priority/high"
c="${PRIORITIES[$priority_label]}"
s="${PRIORITIES[$l]}"
if [ ! -z "${s}" ] && [ $c -lt $s ]; then
priority_label=$l
fi
elif [ "${label}" = "type/bug" ] || [ "${label}" = "type/feature" ]; then
l="priority/medium"
c="${PRIORITIES[$priority_label]}"
s="${PRIORITIES[$l]}"
if [ ! -z "${s}" ] && [ $c -lt $s ]; then
priority_label=$l
fi
fi
done
if [ ! -z "${size_label}" ]; then
send_labels+=(\"$size_label\")
fi
if [ ! -z "${priority_label}" ]; then
send_labels+=(\"$priority_label\")
fi
if [ ${#send_labels[@]} -gt 0 ]; then
str="$(IFS=,; echo "${send_labels[*]}")"
data="{\"labels\": [$str]}"
echo "send data: " $data
curl --request PUT \
--url "https://api.github.com/repos/${REPOSITORY}/issues/${pr_num}/labels" \
--header "authorization: Bearer ${GITHUB_TOKEN}" \
--header 'content-type: application/json' \
--data "$data"
fi
env:
REPOSITORY: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}