Skip to content

Commit 1956876

Browse files
committed
Simplify code
1 parent dd92a26 commit 1956876

File tree

1 file changed

+42
-67
lines changed

1 file changed

+42
-67
lines changed

.github/workflows/check-backport-labels.yml

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,127 +25,102 @@ jobs:
2525
json_all_labels="$(gh label list --repo ${{ github.repository }} --json name --search "backport" --limit 1000)"
2626
readarray -t all_labels < <(echo "${json_all_labels}" | jq -r -c '.[].name')
2727
28-
all_backport_labels=()
29-
all_floating_majors=()
28+
declare -A all_backport_labels=()
29+
declare -A all_floating_majors=()
3030
3131
backport_regex="^backport ([0-9])+\.([0-9]+|x)$"
3232
3333
echo "::group::Available Labels"
34+
echo "skip-backport"
3435
3536
for label in "${all_labels[@]}"; do
36-
if [ "${label}" = "skip-backport" ]; then
37-
all_backport_labels+=("${label}")
38-
echo "${label}"
39-
continue
40-
fi
41-
4237
if [[ "${label}" =~ ${backport_regex} ]]; then
4338
major="${BASH_REMATCH[1]}"
4439
minor="${BASH_REMATCH[2]}"
45-
all_backport_labels+=("${label}")
40+
all_backport_labels["${label}"]=1
4641
echo "${label}"
4742
4843
if [ "${minor}" = "x" ]; then
49-
all_floating_majors+=("${major}")
44+
all_floating_majors["${major}"]=1
5045
fi
5146
fi
5247
done
5348
5449
echo "::endgroup::"
5550
5651
has_exact_backport_label=false
57-
pr_majors=()
58-
pr_floating_majors=()
52+
declare -A pr_exact_majors=()
53+
declare -A pr_floating_majors=()
5954
6055
echo "::group::Detected Labels"
6156
6257
for pr_label in "${pr_labels[@]}"; do
63-
for backport_label in "${all_backport_labels[@]}"; do
64-
if [ "${pr_label}" = "${backport_label}" ]; then
65-
66-
if [[ "${pr_label}" =~ ${backport_regex} ]]; then
67-
major="${BASH_REMATCH[1]}"
68-
minor="${BASH_REMATCH[2]}"
69-
if [ "${minor}" != "x" ]; then
70-
pr_majors+=("${major}")
71-
has_exact_backport_label=true
72-
else
73-
pr_floating_majors+=("${major}")
74-
fi
75-
else
76-
# Matches 'skip-backport'
77-
has_exact_backport_label=true
78-
fi
79-
80-
echo "${pr_label}"
58+
if [ "${pr_label}" = "skip-backport" ]; then
59+
has_exact_backport_label=true
60+
echo "${pr_label}"
61+
continue
62+
fi
63+
64+
if [ -z "${all_backport_labels[${pr_label}]}" ]; then
65+
continue
66+
fi
67+
68+
if [[ "${pr_label}" =~ ${backport_regex} ]]; then
69+
major="${BASH_REMATCH[1]}"
70+
minor="${BASH_REMATCH[2]}"
71+
if [ "${minor}" != "x" ]; then
72+
pr_exact_majors["${major}"]=1
73+
has_exact_backport_label=true
74+
else
75+
pr_floating_majors["${major}"]=1
8176
fi
82-
done
77+
fi
78+
79+
echo "${pr_label}"
8380
done
8481
8582
echo "::endgroup::"
8683
8784
if [ "${has_exact_backport_label}" != true ]; then
8885
echo "::error::No exact backport label found. Please add at least one of the"\
89-
"'backport {major}.{minor}' labels or use 'skip-backport',"\
90-
"if this PR should not be backported."
86+
"'backport {major}.{minor}' labels or use 'skip-backport',"\
87+
"if this PR should not be backported."
9188
exit 1
9289
fi
9390
9491
# Validate that a floating backport label exists for each exact backport label major
9592
# version.
9693
97-
has_floating_backport_labels=true
98-
99-
for pr_major in "${pr_majors[@]}"; do
100-
exists_floating_major=false
101-
for floating_major in "${all_floating_majors[@]}"; do
102-
if [ "${pr_major}" -eq "${floating_major}" ]; then
103-
exists_floating_major=true
104-
break
105-
fi
106-
done
94+
has_required_floating_labels=true
10795
108-
if [ "${exists_floating_major}" = false ]; then
96+
for pr_major in "${!pr_exact_majors[@]}"; do
97+
if [ -z "${all_floating_majors[${pr_major}]}" ]; then
98+
# There is no floating version branch for the given major version.
10999
continue
110100
fi
111101
112-
has_floating_major=false
113-
for pr_floating_major in "${pr_floating_majors[@]}"; do
114-
if [ "${pr_major}" -eq "${pr_floating_major}" ]; then
115-
has_floating_major=true
116-
fi
117-
done
118-
119-
if [ "${has_floating_major}" != true ]; then
120-
has_floating_backport_labels=false
102+
if [ -z "${pr_floating_majors[${pr_major}]}" ]; then
103+
has_required_floating_labels=false
121104
echo "::error::Missing floating backport label for '${pr_major}.x'"
122105
fi
123106
done
124107
125-
if [ "${has_floating_backport_labels}" != true ]; then
108+
if [ "${has_required_floating_labels}" != true ]; then
126109
exit 1
127110
fi
128111
129112
# Validate that an exact backport label exists for each floating backport label major
130113
# version.
131114
132-
has_exact_backport_labels=true
115+
has_required_exact_labels=true
133116
134-
for pr_floating_major in "${pr_floating_majors[@]}"; do
135-
exists_exact_major=false
136-
for pr_major in "${pr_majors[@]}"; do
137-
if [ "${pr_floating_major}" -eq "${pr_major}" ]; then
138-
has_exact_major=true
139-
fi
140-
done
141-
142-
if [ "${has_exact_major}" != true ]; then
143-
has_exact_backport_labels=false
117+
for pr_floating_major in "${!pr_floating_majors[@]}"; do
118+
if [ -z "${pr_exact_majors[${pr_floating_major}]}" ]; then
119+
has_required_exact_labels=false
144120
echo "::error::Missing exact backport label for '${pr_floating_major}.x'"
145121
fi
146122
done
147123
148-
if [ "${has_exact_backport_labels}" != true ]; then
124+
if [ "${has_required_exact_labels}" != true ]; then
149125
exit 1
150126
fi
151-

0 commit comments

Comments
 (0)