Skip to content

Commit fd3b652

Browse files
authored
Merge pull request #482 from Martchus/pending-submissions
Throttle automatic maintenance updates for Leap 16.X
2 parents 11bd858 + 98086c0 commit fd3b652

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

os-autoinst-obs-auto-submit

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dry_run="${dry_run:-"0"}"
1313
osc_poll_interval="${osc_poll_interval:-2}"
1414
osc_build_start_poll_tries="${osc_build_start_poll_tries:-30}"
1515
throttle_days=${throttle_days:-2}
16+
git_user=${git_user:-os-autoinst-obs-workflow}
1617
XMLSTARLET=$(command -v xmlstarlet || true)
1718
[[ -n $XMLSTARLET ]] || (echo "Need xmlstarlet" >&2 && exit 1)
1819

@@ -175,6 +176,7 @@ update_package() {
175176
wait_for_package_build "$submit_target"
176177
for target in "${targets[@]}"; do
177178
[[ $target == none ]] && continue # allow specifying submit_target_extra=none for easier testing
179+
has_pending_submission "$package" "$target" || continue
178180
log-info "## Ready to submit $package to $target ##"
179181
local submit_cmd=make_obs_submit_request
180182
local args=("$package" "$target" "$version")
@@ -279,16 +281,35 @@ get_project_packages() {
279281
}
280282

281283
has_pending_submission() {
282-
log-info "has_pending_submission()"
283-
local requestlist
284-
# throttle number of submissions to allow only one SR within a certain number of days
285-
# note: Avoid using `grep --quiet` here to keep consuming input so osc does not run into "BrokenPipeError: [Errno 32] Broken pipe".
286284
[[ $throttle_days == 0 ]] && return 0
287-
requestlist=$($osc request list --project "$dst_project" --type submit --state new,review --mine --days "$throttle_days")
288-
if echo "$requestlist" | grep 'Created by' > /dev/null; then
289-
log-info "Skipping submission, there is still a pending SR younger than $throttle_days days."
290-
echo "$requestlist"
291-
return 1
285+
286+
local package=$1 target=$2
287+
local git_branch=${git_branches[$target]:-}
288+
local requestlist recent_pr_url
289+
log-info "has_pending_submission($package, $target)"
290+
291+
# throttle number of submissions to allow only one PR or SR within a certain number of days
292+
if [[ $git_branch ]]; then
293+
# check for PR on Gitea
294+
recent_pr_url=$($git_obs -q api "repos/pool/$package/pulls?state=open&sort=recentupdate" 2> /dev/null \
295+
| jq --raw-output \
296+
--arg days "$throttle_days" \
297+
--arg target "$git_branch" \
298+
--arg login "$git_user" \
299+
'[.[] | select(.base.ref == $target) | select(.user.login == $login) | select(.updated_at | strptime("%Y-%m-%dT%H:%M:%S%z") | mktime > (now - ($days | tonumber) * 86400)) | .html_url] | first')
300+
if [[ $recent_pr_url != null ]]; then
301+
log-info "Skipping submission, there is the still pending PR '$recent_pr_url' by $git_user targeting $git_branch younger than $throttle_days days."
302+
return 1
303+
fi
304+
else
305+
# check for SR on OBS
306+
# note: Avoid using `grep --quiet` here to keep consuming input so osc does not run into "BrokenPipeError: [Errno 32] Broken pipe".
307+
requestlist=$($osc request list --project "$target" --package "$package" --type submit --state new,review --mine --days "$throttle_days")
308+
if echo "$requestlist" | grep 'Created by' > /dev/null; then
309+
log-info "Skipping submission, there is still a pending SR for package $package in project $target younger than $throttle_days days."
310+
echo "$requestlist"
311+
return 1
312+
fi
292313
fi
293314
return 0
294315
}
@@ -306,7 +327,6 @@ submit() {
306327
cat job_post_skip_submission
307328
return 0
308329
fi
309-
has_pending_submission || return 0
310330
(
311331
cd "$TMPDIR"
312332
rc=0

test/08-auto-submit.t

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
source test/init
4-
plan tests 5
4+
plan tests 11
55

66
mock_osc() {
77
local cmd=$1
@@ -15,23 +15,54 @@ _request_list() {
1515
echo "Created by: foo"
1616
}
1717

18+
mock_git_obs() {
19+
if [[ $3 == 'repos/pool/openQA/pulls?state=open&sort=recentupdate' ]]; then
20+
_pr_list leap-16.0
21+
else
22+
_pr_list foo # PR targeting "foo" is supposed to be ignored
23+
fi
24+
}
25+
26+
_pr_list() {
27+
local ref=$1
28+
echo "[{\"updated_at\":\"$two_days_ago\", \"html_url\": \"https://foo/bar\", \"user\": {\"login\": \"$git_user\"}, \"base\": {\"ref\": \"$ref\"}}]"
29+
}
30+
31+
two_days_ago=$(date --iso-8601=seconds --date='-2 day')
1832
osc=mock_osc
33+
git_obs=mock_git_obs
1934
source os-autoinst-obs-auto-submit
2035

2136
note "########### has_pending_submission"
2237

2338
throttle_days=0
24-
try has_pending_submission
39+
package=os-autoinst
40+
try has_pending_submission "$package" "$submit_target"
2541
is "$rc" 0 "returns 0 with throttle_days=0"
2642

2743
throttle_days=1
28-
try has_pending_submission
44+
try has_pending_submission "$package" "$submit_target"
2945
is "$rc" 1 "returns 1 with existing SRs"
3046
like "$got" "Created by: foo" "expected output"
3147

3248
_request_list() {
3349
echo ""
3450
}
35-
try has_pending_submission
51+
try has_pending_submission "$package" "$submit_target"
3652
is "$rc" 0 "returns 0 without existing SRs"
3753
like "$got" "info.*has_pending_submission" "no output"
54+
55+
submit_target=openSUSE:Leap:16.0
56+
try has_pending_submission "$package" "$submit_target"
57+
is "$rc" 0 "returns 0 without existing PRs"
58+
like "$got" "info.*has_pending_submission\\($package, $submit_target\\)$" "no output (no PR)"
59+
60+
package=openQA
61+
try has_pending_submission "$package" "$submit_target"
62+
is "$rc" 0 "returns 0 with existing PR older than throttle config of $throttle_days days"
63+
like "$got" "info.*has_pending_submission\\($package, $submit_target\\)$" "no output (old PR)"
64+
65+
throttle_days=3
66+
try has_pending_submission "$package" "$submit_target"
67+
is "$rc" 1 "returns 1 with existing PR recent than throttle config of $throttle_days days"
68+
like "$got" "info.*Skipping submission.*pending PR.*https://foo/bar" "expected output (recent PR)"

0 commit comments

Comments
 (0)