-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpr-porting-checks.sh
executable file
·169 lines (128 loc) · 3.64 KB
/
pr-porting-checks.sh
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
#!/bin/bash
#
# Copyright (c) 2020 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
script_name=${0##*/}
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
[ -n "${DEBUG:-}" ] && set -o xtrace
die()
{
echo >&2 "::error::$*"
exit 1
}
usage()
{
cat <<EOT
Usage: $script_name [options] <pr> <repo-slug>
Description: Check the specified PR to ensure it is labelled with
the correct porting labels.
Options:
-h : Show this help statement.
Notes:
- Designed to be called as a GitHub action.
- Must be run from inside a Git(1) checkout.
EOT
}
setup()
{
local cmd
for cmd in git hub jq
do
command -v "$cmd" &>/dev/null || \
die "need command: $cmd"
done
git config --get remote.origin.url &>/dev/null || \
die "not a git checkout"
}
handle_args()
{
[ "${1:-}" = '-h' ] && usage && exit 0
local pr="${1:-}"
local repo="${2:-}"
[ -z "$pr" ] && die "need PR number"
[ -z "$repo" ] && die "need repository"
# Most PRs must have two porting labels before they can be merged.
#
# This is to required to ensure it is clear that a reviewer has
# considered both porting directions.
#
# The exception are PRs for an actual backport or forward port which
# only need one of these labels to be applied since by definition they
# can only apply to one porting direction.
local backport_labels=("needs-backport" "no-backport-needed" "backport")
# If a PR is labelled with one of these labels, ignore all further
# checks (since the PR is not yet "ready").
local ignore_labels=("do-not-merge" "rfc" "wip")
local labels=$(hub issue labels)
local label
# Note: no validation done on ignore_labels as they are not actually
# porting labels, and so are not essential.
for label in ${backport_labels[@]}
do
local ret
{ echo "$labels" | egrep -q "^${label}$"; ret=$?; } || true
[ $ret -eq 0 ] || die "Expected label '$label' not available in repository $repo"
done
local pr_details=$(hub pr list -f '%I;%L%n' | grep "^${pr}" || true)
[ -z "$pr_details" ] && die "Cannot determine details for PR $pr"
local pr_labels=$(echo "$pr_details" |\
cut -d';' -f2 |\
sed 's/, /,/g' |\
tr ',' '\n')
[ -z "$pr_labels" ] && {
printf "::error::PR %s does not have required porting labels (expected one of '%s')\n" \
"$pr" \
$(echo "${backport_labels[@]}" | tr ' ' ',') \
exit 1
}
local ignore_labels_found=()
for label in ${ignore_labels[@]}
do
echo "$pr_labels" | egrep -q "^${label}$" \
&& ignore_labels_found+=("$label")
done
[ "${#ignore_labels_found[@]}" -gt 0 ] && {
printf "::debug::Ignoring porting checks as PR %s contains the following special labels: '%s'" \
"$pr" \
$(echo "${ignore_labels_found[@]}" | tr ' ' ',')
exit 0
}
local backport_labels_found=()
for label in ${backport_labels[@]}
do
echo "$pr_labels" | egrep -q "^${label}$" \
&& backport_labels_found+=("$label")
done
local backport_pr="false"
[ "${#backport_labels_found[@]}" -eq 1 ] && \
[ "$backport_labels_found" = 'backport' ] && \
backport_pr="true"
# PR should have at least one backport label
[ "${#backport_labels_found[@]}" -eq 0 ] && {
printf "::error::PR %s missing a backport label (expected one of '%s')\n" \
"$pr" \
$(echo "${backport_labels[@]}" | tr ' ' ',')
exit 1
}
[ "${#backport_labels_found[@]}" -gt 1 ] && {
printf "::error::PR %s has too many backport labels (expected one of '%s', found '%s')\n" \
"$pr" \
$(echo "${backport_labels[@]}" | tr ' ' ',') \
$(echo "${backport_labels_found[@]}" | tr ' ' ',')
exit 1
}
printf "::debug::PR %s has required porting labels (backport label '%s')\n" \
"$pr" \
"${backport_labels_found[@]}"
}
main()
{
setup
handle_args "$@"
}
main "$@"