-
Notifications
You must be signed in to change notification settings - Fork 6
/
pw_mon
executable file
·289 lines (246 loc) · 10.4 KB
/
pw_mon
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/sh
# SPDX-Identifier: gpl-2.0-or-later
# Copyright (C) 2018-2023 Red Hat, Inc.
#
# Monitors a project on a patchwork instance for new series submissions
# Records the submissions in the series database (and emits them on the
# stdout line for processing)
#
# Licensed under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. You may obtain a copy of the
# license at
#
# https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# 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.
[ -f "${HOME}/.pwmon-rc" ] && source "${HOME}/.pwmon-rc"
if [ "$1" != "" ]; then
if ! echo "$1" | grep -q -s -E ^-- ; then
pw_project="$1"
shift
fi
fi
if [ "$1" != "" ]; then
if ! echo "$1" | grep -q -s -E ^-- ; then
pw_instance="$1"
shift
fi
fi
userpw=""
if [ "$1" != "" ]; then
if ! echo "$1" | grep -q -s -E ^-- ; then
pw_credential="$1"
shift
fi
fi
recheck_filter=""
while [ "$1" != "" ]; do
if echo "$1" | grep -q -s -E ^--pw-project= ; then
pw_project=$(echo "$1" | sed s/^--pw-project=//)
shift
elif echo "$1" | grep -q -s -E ^--pw-instance= ; then
pw_instance=$(echo "$1" | sed s/^--pw-instance=//)
shift
elif echo "$1" | grep -q -s -E ^--pw-credentials= ; then
pw_credential=$(echo "$1" | sed s/^--pw-credentials=//)
shift
elif echo "$1" | grep -E ^--help >/dev/null 2>&1; then
echo "patchwork monitor script"
echo "$0: args"
echo "Required if not set in ~/.pwmon-rc file:"
echo " proj|--pw-project=<proj> Project name"
echo " instance|--pw-instance=<inst url> URL for pw"
echo ""
echo "Options:"
echo " --pw-credentials=u:p Sets user / password for web client"
echo " --add-filter-recheck=filter Adds a filter to flag that a recheck needs to be done"
echo ""
exit 0
elif echo "$1" | grep -q -s -E ^--add-filter-recheck=; then
filter_str=$(echo "$1" | sed s/--add-filter-recheck=//)
recheck_filter="$filter_str $recheck_filter"
shift
else
echo "Unknown option: '$1'"
echo "Rerun with --help for details"
exit 1
fi
done
if [ "X$pw_credential" != "X" ]; then
userpw="-u \"${pw_credential}\""
fi
if [ "X$pw_instance" == "X" -o "X$pw_project" == "X" ]; then
echo "ERROR: Patchwork instance and project are unset."
echo "Please setup ${HOME}/.pwmon-rc and set pw_project "
echo "(or pass it as an argument)."
echo "Also either setup pw_instance or pass it as an argument."
exit 1
fi
source $(dirname $0)/series_db_lib.sh
function emit_series() {
local instance="$1"
local project="$2"
local id="$3"
local url="$4"
local submitter_name="$5"
local submitter_email="$6"
local all="$7"
local completed=0
if [ "$all" = "true" ]; then
completed=1
fi
if ! series_id_exists "${instance}" "${id}"; then
echo "============================================="
echo "Series instance: $instance"
echo "Series id: $id"
echo "Series url: $url"
echo "submitter: $submitter_name <$submitter_email>"
echo "all: $all"
echo "recording series (${id}, \"${url}\", \"${submitter_name}\", \"${submitter_email}\")"
echo
series_db_add_false "$instance" "$project" "$id" "$url" "$submitter_name" "$submitter_email" "$completed"
fi
}
function check_new_series() {
local INSTANCE="$1"
local PROJECT="$2"
if [ "$PROJECT" == "" -o "$INSTANCE" == "" ]; then
echo "ERR: need a project"
exit 1
fi
if [ ! -e "${HOME}/.pwmon-${INSTANCE}-${PROJECT}-series" ]; then
echo "WARN: PW-series doesn't exist..."
echo " Re-running with patches since yesterday"
TRY_DATE=$(date --date="yesterday" '+%F %T')
STAMP_DATE=$(date --date="$TRY_DATE + 1 day")
else
TRY_DATE=$(stat "${HOME}/.pwmon-${INSTANCE}-${PROJECT}-series" | grep Modify | cut -d" " -f2-3 | cut -d. -f1)
STAMP_DATE=$(date)
fi
SINCE=$(echo $TRY_DATE | sed 's@ @%20@')
STAMP=$(date --date="$STAMP_DATE" '+%C%y%m%d%H%M')
GET_URL="http://${INSTANCE}/api/series/?project=${PROJECT}&since=${SINCE}"
response=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "$GET_URL")
series_info=$(echo "$response" | jq -rc '.[] | (.id|tostring) + ";" + .url + ";" + .submitter.name + ";" + .submitter.email + ";" + (.received_all|tostring)')
if [ "X$series_info" != "X" ]; then
echo "$series_info" | while IFS=\; read -r id url submitter_name submitter_email all; do
emit_series "${INSTANCE}" "${PROJECT}" "$id" "$url" "$submitter_name" "$submitter_email" "$all"
done
fi
touch -m -t "${STAMP}" "${HOME}/.pwmon-${INSTANCE}-${PROJECT}-series"
}
function check_completed_series() {
get_uncompleted_jobs_as_line "$pw_instance" "$pw_project" | while IFS=\| read -r id url submitter_name submitter_email; do
echo "Checking on series: $id"
local RESPONSE=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "$url" | jq -rc '.received_all')
if [ "$RESPONSE" = "true" ]; then
echo "Setting series $id to completed"
series_id_set_complete "$pw_instance" "$id"
fi
done
return 0
}
function check_undownloaded_series() {
get_undownloaded_jobs_as_line "$pw_instance" "$pw_project" | while IFS=\| read -r id url submitter_name submitter_email; do
echo "re-submitting series: $id"
series_id_clear_downloaded "$pw_instance" "$id"
done
return 0
}
function check_superseded_series() {
local pw_instance="$1"
series_get_active_branches "$pw_instance" | while IFS=\| read -r series_id project url repo branchname; do
# first query the patch states
local last_patch_url=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "$url" | jq -rc '.patches[-1].url')
local patch_state=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "$last_patch_url" | jq -rc '.state')
# now check to see if the patch should even be reported...
if [ "$patch_state" = "superseded" -o "$patch_state" = "rejected" -o "$patch_state" = "accepted" \
-o "$patch_state" = "changes-requested" -o "$patch_state" = "not-applicable" ]; then
series_clear_branch "$pw_instance" "$series_id"
set_synced_for_series "$series_id" "$pw_instance"
fi
echo "Worked on $series_id : state $patch_state"
done
}
function run_recheck() {
local recheck_list=$(echo "$7" | sed -e 's/^Recheck-request: // ' -e 's/,/ /g')
for filter in $recheck_filter; do
for check in $recheck_list; do
if [ "$filter" == "$check" ]; then
echo "Recheck matched: $1 $3 $8 $check $2 $9"
insert_recheck_request_if_needed "$1" "$3" "$8" "$check" "$2" "$9"
fi
done
done
}
function check_patch_for_retest_request() {
local pw_instance="$1"
local pw_project="$2"
local patch_url="$3"
local patch_json=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "$patch_url")
local patch_comments_url=$(echo "$patch_json" | jq -rc '.comments')
local patch_id=$(echo "$patch_json" | jq -rc '.id')
local series_id=$(echo "$patch_json" | jq -rc '.series[].id')
# check the patch to see if it has a good state
# This is needed because there is a disconnect in some instances between
# Patchwork's "series" state and "patch" state
local patch_state=$(echo "$patch_json" | jq -rc '.state')
if [ "$patch_state" = "superseded" -o "$patch_state" = "rejected" -o "$patch_state" = "accepted" \
-o "$patch_state" = "changes-requested" -o "$patch_state" = "not-applicable" ]; then
return
fi
if [ "Xnull" != "X$patch_comments_url" ]; then
local comments_json=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "$patch_comments_url")
local seq_end=$(echo "$comments_json" | jq -rc 'length')
if [ "$seq_end" -a $seq_end -gt 0 ]; then
seq_end=$((seq_end-1))
for comment_id in $(seq 0 $seq_end); do
local recheck_requested=$(echo "$comments_json" | jq -rc ".[$comment_id].content" | grep "^Recheck-request: ")
if [ "X$recheck_requested" != "X" ]; then
local msgid=$(echo "$comments_json" | jq -rc ".[$comment_id].msgid")
run_recheck "$pw_instance" "$series_id" "$pw_project" "$url" "$repo" "$branchname" "$recheck_requested" "$msgid" "$patch_id"
fi
done
fi
fi
}
function check_series_needs_retest() {
local pw_instance="$1"
local pw_project="$2"
local series_list=$(curl -A "(pw-ci) pw-mon-${PROJECT}" -s "$userpw" "http://${pw_instance}/api/series/?project=${pw_project}&state=new&state=rfc&state=under-review&archived=false&order=-id")
local n=$(echo "$series_list" | jq -rc 'length')
if [ "Xnull" == "X$n" -o "X" == "X$n" ]; then
return
fi
series_end=$(($n-1))
[ "$series_end" -le 0 ] && return
for series_n in $(seq 0 $series_end); do
local patches_list=$(echo "$series_list" | jq -rc ".[$series_n].patches")
o=$(echo "$patches_list" | jq -rc 'length')
if [ "Xnull" == "X$o" -o "X" == "X$o" -o "X0" == "X$o" ]; then
echo "WARN: Missing patches when checking series."
else
patch_end=$(($o-1))
for patch_n in $(seq 0 $patch_end); do
local patch_url=$(echo "$patches_list" | jq -rc ".[$patch_n].url")
if [ "Xnull" != "X$patch_url" ]; then
check_patch_for_retest_request "$pw_instance" "$pw_project" "$patch_url"
fi
done
fi
done
}
check_undownloaded_series "$pw_instance" "$pw_project"
check_completed_series "$pw_instance" "$pw_project"
check_new_series "$pw_instance" "$pw_project"
check_superseded_series "$pw_instance"
# check for retest requests after a series is still passing all the
# checks above
if [ "X$recheck_filter" != "X" ]; then
check_series_needs_retest "$pw_instance" "$pw_project"
fi