109 lines
3 KiB
Bash
109 lines
3 KiB
Bash
|
#!/bin/bash
|
||
|
# Copyright 2024 The Forgejo Authors
|
||
|
# SPDX-License-Identifier: MIT
|
||
|
|
||
|
API_TMPDIR=$(mktemp -d)
|
||
|
|
||
|
function api_branch_tip() {
|
||
|
local api="$1"
|
||
|
local repo="$2"
|
||
|
local branch="$3"
|
||
|
|
||
|
retry forgejo-curl.sh api_json $api/repos/$repo/branches/$branch >&/dev/null
|
||
|
forgejo-curl.sh api_json $api/repos/$repo/branches/$branch | jq --raw-output .commit.id
|
||
|
}
|
||
|
|
||
|
function api_pr_is_merged() {
|
||
|
local api="$1"
|
||
|
local repo="$2"
|
||
|
local pr="$3"
|
||
|
|
||
|
forgejo-curl.sh api_json $api/repos/$repo/pulls/$pr >$API_TMPDIR/pr.json
|
||
|
$(jq -r .merged <$API_TMPDIR/pr.json)
|
||
|
}
|
||
|
|
||
|
function api_pr_delete_all() {
|
||
|
local api="$1"
|
||
|
local repo="$2"
|
||
|
|
||
|
forgejo-curl.sh api_json $api/repos/${repo}/pulls | jq --raw-output '.[] | .number' | while read pr; do
|
||
|
forgejo-curl.sh api_json -X DELETE $api/repos/${repo}/issues/$pr
|
||
|
done
|
||
|
}
|
||
|
|
||
|
function api_pr_get_status() {
|
||
|
local api="$1"
|
||
|
local repo="$2"
|
||
|
local sha="$3"
|
||
|
|
||
|
forgejo-curl.sh api_json $api/repos/$repo/commits/$sha/status
|
||
|
}
|
||
|
|
||
|
function api_pr_check_status() {
|
||
|
local api="$1"
|
||
|
local repo="$2"
|
||
|
local sha="$3"
|
||
|
local expected_status="$4"
|
||
|
local expected_description="$5"
|
||
|
|
||
|
api_pr_get_status $api $repo $sha >$API_TMPDIR/status.json
|
||
|
local status="$(jq --raw-output .state <$API_TMPDIR/status.json)"
|
||
|
local description="$(jq --raw-output .statuses[0].description <$API_TMPDIR/status.json)"
|
||
|
if test "$status" = "$expected_status" && test -z "$expected_description" -o "$description" = "$expected_description"; then
|
||
|
echo OK
|
||
|
elif test "$status" = "failure" -o "$status" = "success"; then
|
||
|
echo NOK
|
||
|
else
|
||
|
echo RETRY
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
function api_pr_wait_success() {
|
||
|
api_pr_wait_status success "$@"
|
||
|
}
|
||
|
|
||
|
function api_pr_wait_failure() {
|
||
|
api_pr_wait_status failure "$@"
|
||
|
}
|
||
|
|
||
|
function api_pr_wait_running() {
|
||
|
api_pr_wait_status pending "$@" "Has started running"
|
||
|
}
|
||
|
|
||
|
function api_pr_wait_log() {
|
||
|
local sha="$1" expected_status="$2" expected_description="$3"
|
||
|
local status="$(jq --raw-output .state <$API_TMPDIR/status.json)"
|
||
|
local description="$(jq --raw-output .statuses[0].description <$API_TMPDIR/status.json)"
|
||
|
if test "$expected_description"; then
|
||
|
expected_description=" '$expected_description'"
|
||
|
fi
|
||
|
log_info "$sha status waiting '$expected_status'$expected_description, currently '$status' '$description'"
|
||
|
}
|
||
|
|
||
|
# default loop delay is 3600 sec (1 hour)
|
||
|
: ${API_LOOPS:=100}
|
||
|
: ${API_LOOP_DELAY:=36}
|
||
|
|
||
|
function api_pr_wait_status() {
|
||
|
local status="$1"
|
||
|
local api="$2"
|
||
|
local repo="$3"
|
||
|
local sha="$4"
|
||
|
local description="$5"
|
||
|
|
||
|
for i in $(seq $API_LOOPS); do
|
||
|
if test $(api_pr_check_status "$api" "$repo" "$sha" "$status" "$description") != RETRY; then
|
||
|
break
|
||
|
fi
|
||
|
api_pr_wait_log "$sha" "$status" "$description"
|
||
|
sleep $API_LOOP_DELAY
|
||
|
done
|
||
|
if test $(api_pr_check_status "$api" "$repo" "$sha" "$status" "$description") = "OK"; then
|
||
|
log_info "$sha status OK"
|
||
|
else
|
||
|
api_pr_get_status $api $repo $sha | jq .statuses
|
||
|
log_info "$sha status NOK"
|
||
|
return 1
|
||
|
fi
|
||
|
}
|