2024-05-27 16:57:05 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# Copyright 2024 The Forgejo Authors
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
API_TMPDIR=$(mktemp -d)
|
2024-05-27 23:08:39 +02:00
|
|
|
: ${PASSWORD:=admin1234}
|
|
|
|
|
|
|
|
function api_user_make_admin() {
|
|
|
|
local api="$1" username="$2"
|
|
|
|
|
|
|
|
forgejo-curl.sh api_json -X PATCH --data '{"admin":true}' $api/admin/users/$username
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_user_create() {
|
|
|
|
local api="$1" username="$2" email="$3"
|
|
|
|
log_info "(re)create user $username"
|
2024-08-07 08:31:33 +02:00
|
|
|
forgejo-curl.sh api_json -X DELETE $api/admin/users/$username?purge=true >&/dev/null || true
|
2024-05-27 23:08:39 +02:00
|
|
|
forgejo-curl.sh api_json --data '{"username":"'$username'","email":"'$email'","password":"admin1234","must_change_password":false}' $api/admin/users
|
|
|
|
}
|
|
|
|
|
|
|
|
function user_login() {
|
|
|
|
local username=$1
|
|
|
|
(
|
2024-08-07 08:31:33 +02:00
|
|
|
export DOT=$API_TMPDIR/$username
|
|
|
|
forgejo-curl.sh logout
|
|
|
|
forgejo-curl.sh --user $username --password "admin1234" login http://${HOST_PORT}
|
2024-05-27 23:08:39 +02:00
|
|
|
)
|
|
|
|
}
|
2024-05-27 16:57:05 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-27 23:08:39 +02:00
|
|
|
function api_branch_protect() {
|
|
|
|
local api="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local branch="$3"
|
|
|
|
|
|
|
|
forgejo-curl.sh api_json -X DELETE $api/repos/${repo}/branch_protections/$branch >&/dev/null || true
|
|
|
|
forgejo-curl.sh api_json --data '{"branch_name":"'$branch'","required_approvals":1}' $api/repos/${repo}/branch_protections
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_pr_approve() {
|
|
|
|
local api="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local pr="$3"
|
|
|
|
|
|
|
|
forgejo-curl.sh api_json --data '{"event":"APPROVED"}' $api/repos/${repo}/pulls/$pr/reviews
|
|
|
|
}
|
|
|
|
|
2024-05-27 16:57:05 +02:00
|
|
|
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
|
|
|
|
}
|