actions: add automerge example

This commit is contained in:
Earl Warren 2024-05-27 16:57:05 +02:00
parent c6cd39b074
commit f9e20e7770
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
7 changed files with 202 additions and 2 deletions

108
lib/api.sh Normal file
View file

@ -0,0 +1,108 @@
#!/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
}

View file

@ -4,6 +4,8 @@
LIB_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $LIB_DIR/api.sh
if ${VERBOSE:-false} ; then
set -ex
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '