2023-10-11 15:39:52 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-10-11 19:20:52 +02:00
|
|
|
set -e
|
|
|
|
|
2023-10-11 15:39:52 +02:00
|
|
|
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2023-10-12 15:12:52 +02:00
|
|
|
TMPDIR=/tmp/cascading-pr-test
|
2023-10-13 22:30:22 +02:00
|
|
|
mkdir -p $TMPDIR
|
2023-10-11 18:05:11 +02:00
|
|
|
source $SELF_DIR/../cascading-pr-lib.sh
|
2023-10-11 15:39:52 +02:00
|
|
|
|
|
|
|
function push_self() {
|
2023-10-12 15:12:52 +02:00
|
|
|
forgejo-test-helper.sh push_self_action http://user1:admin1234@${options[host_port]} user1 cascading-pr vTest
|
2023-10-11 15:39:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 19:20:52 +02:00
|
|
|
function user_login() {
|
|
|
|
local username=$1
|
|
|
|
(
|
|
|
|
export DOT=$TMPDIR/$username
|
|
|
|
forgejo-curl.sh logout
|
|
|
|
forgejo-curl.sh --user $username --password "${options[password]}" login ${options[url]}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function user_curl() {
|
|
|
|
local username=$1
|
|
|
|
shift
|
2023-10-12 15:12:52 +02:00
|
|
|
DOT=$TMPDIR/$username forgejo-curl.sh "$@"
|
2023-10-11 19:20:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 22:32:24 +02:00
|
|
|
function user_token() {
|
|
|
|
local username=$1 name=$2
|
|
|
|
|
2023-10-12 22:43:32 +02:00
|
|
|
curl -sS -f -H Content-Type:application/json --user "$username:${options[password]}" --data-raw '{"name":"'$name'","scopes":["write:repository","write:issue","read:user"]}' ${options[url]}/api/v1/users/$username/tokens | jq --raw-output .sha1 | tee $TMPDIR/$username/repo-token
|
2023-10-11 22:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function user_secret() {
|
|
|
|
local username=$1 name=$2 token=$3
|
|
|
|
|
|
|
|
user_curl $username api_json -X PUT --data-raw '{"data":"'$token'"}' ${options[url]}/api/v1/repos/user1/originrepo/actions/secrets/$name
|
|
|
|
}
|
|
|
|
|
|
|
|
function user_create() {
|
2023-10-11 19:20:52 +02:00
|
|
|
local username="$1" email="$2"
|
2023-10-11 20:00:00 +02:00
|
|
|
forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/admin/users/$username?purge=true >& /dev/null || true
|
2023-10-11 19:20:52 +02:00
|
|
|
forgejo-curl.sh api_json --data-raw '{"username":"'$username'","email":"'$email'","password":"'${options[password]}'","must_change_password":false}' ${options[url]}/api/v1/admin/users
|
|
|
|
user_login $username
|
|
|
|
}
|
|
|
|
|
2023-10-13 19:10:50 +02:00
|
|
|
function close_pull_request() {
|
|
|
|
forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user1/originrepo/pulls | jq --raw-output '.[] | .number' | while read pr ; do
|
|
|
|
forgejo-curl.sh api_json -X PATCH --data-raw '{"state":"closed"}' ${options[url]}/api/v1/repos/user1/originrepo/issues/$pr
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:08:52 +02:00
|
|
|
function merge_pull_request() {
|
|
|
|
forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user1/originrepo/pulls | jq --raw-output '.[] | .number' | while read pr ; do
|
|
|
|
forgejo-curl.sh api_json --data-raw '{"Do":"merge"}' ${options[url]}/api/v1/repos/user1/originrepo/pulls/$pr/merge
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-10-12 00:03:58 +02:00
|
|
|
function create_pull_request() {
|
|
|
|
forgejo-curl.sh api_json ${options[url]}/api/v1/repos/user1/originrepo/pulls | jq --raw-output '.[] | .number' | while read pr ; do
|
|
|
|
forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/repos/user1/originrepo/issues/$pr
|
|
|
|
done
|
|
|
|
forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/repos/user1/originrepo/branches/branch1 >& /dev/null || true
|
|
|
|
forgejo-curl.sh api_json --data-raw '{"new_branch_name":"branch1"}' ${options[url]}/api/v1/repos/user1/originrepo/branches
|
|
|
|
(
|
|
|
|
cd $TMPDIR
|
|
|
|
rm -fr originrepo
|
|
|
|
git clone -b branch1 http://user1:admin1234@${options[host_port]}/user1/originrepo
|
|
|
|
cd originrepo
|
|
|
|
echo CONTENT > README
|
|
|
|
git config user.email root@example.com
|
|
|
|
git config user.name username
|
|
|
|
git add .
|
|
|
|
git commit -m 'update'
|
|
|
|
git push origin branch1
|
2023-10-13 15:30:22 +02:00
|
|
|
git rev-parse HEAD > ../originrepo.sha
|
2023-10-12 00:03:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
forgejo-curl.sh api_json --data-raw '{"title":"PR","base":"main","head":"branch1"}' ${options[url]}/api/v1/repos/user1/originrepo/pulls
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:23:50 +02:00
|
|
|
function finalize_options() {
|
|
|
|
if test -f forgejo-ip; then
|
|
|
|
: ${options[host_port]:=$(cat forgejo-ip):3000}
|
|
|
|
fi
|
|
|
|
options[url]=http://${options[host_port]}
|
|
|
|
if test -f forgejo-token; then
|
|
|
|
: ${options[token]:=$(cat forgejo-token)}
|
|
|
|
fi
|
|
|
|
options[password]=admin1234
|
|
|
|
}
|
|
|
|
|
2023-10-14 16:01:37 +02:00
|
|
|
function unit_retry_fail() {
|
|
|
|
local file=$1
|
|
|
|
local value=$(cat $file)
|
|
|
|
expr $value - 1 > $file
|
|
|
|
echo RESULT
|
|
|
|
if test $value -gt 0 ; then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function unit_retry() {
|
|
|
|
local two=$TMPDIR/unit_retry_two
|
|
|
|
|
|
|
|
rm -f $TMPDIR/retry.*
|
|
|
|
|
|
|
|
#
|
|
|
|
# Succeeds after two tries
|
|
|
|
#
|
|
|
|
echo 2 > $TMPDIR/unit_retry_two
|
|
|
|
RETRY_DELAYS='1 1 1 1' retry unit_retry_fail $two > $TMPDIR/retry.result 2> $TMPDIR/retry.log
|
|
|
|
test "$(cat $TMPDIR/retry.result)" = "RESULT"
|
|
|
|
test "$(grep -c 'waiting 1 unit_retry_fail' $TMPDIR/retry.log)" = 2
|
|
|
|
|
|
|
|
#
|
|
|
|
# Succeeds immediately
|
|
|
|
#
|
|
|
|
RETRY_DELAYS='1' retry unit_retry_fail $two > $TMPDIR/retry.result 2> $TMPDIR/retry.log
|
|
|
|
test "$(cat $TMPDIR/retry.result)" = "RESULT"
|
|
|
|
test "$(grep -c 'waiting 1 unit_retry_fail' $TMPDIR/retry.log)" = 0
|
|
|
|
|
|
|
|
#
|
|
|
|
# Verify the output is only the output of the last run and is not polluted by
|
|
|
|
# unrelated output
|
|
|
|
#
|
|
|
|
echo 2 > $TMPDIR/unit_retry_two
|
|
|
|
test "$(RETRY_DELAYS='1 1 1' retry unit_retry_fail $two)" = "RESULT"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Fails after one try
|
|
|
|
#
|
|
|
|
echo 2 > $TMPDIR/unit_retry_two
|
|
|
|
if RETRY_DELAYS='1' retry unit_retry_fail $two |& tee $TMPDIR/retry.log ; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
grep --quiet 'retry failed' $TMPDIR/retry.log
|
|
|
|
}
|
|
|
|
|
|
|
|
function integration() {
|
2023-10-11 22:32:24 +02:00
|
|
|
user_create user2 user2@example.com
|
|
|
|
forgejo-test-helper.sh push tests/destinationrepo http://user2:admin1234@${options[host_port]} user2 destinationrepo
|
2023-10-11 20:00:00 +02:00
|
|
|
|
2023-10-11 22:32:24 +02:00
|
|
|
user_create user1 user1@example.com
|
|
|
|
forgejo-test-helper.sh push tests/originrepo http://user1:admin1234@${options[host_port]} user1 originrepo cascading-pr
|
|
|
|
user_secret user1 ORIGIN_TOKEN $(user_token user1 ORIGIN_TOKEN)
|
|
|
|
user_secret user1 DESTINATION_TOKEN $(user_token user2 DESTINATION_TOKEN)
|
2023-10-11 19:20:52 +02:00
|
|
|
|
2023-10-13 15:23:50 +02:00
|
|
|
push_self
|
|
|
|
|
2023-10-13 21:08:52 +02:00
|
|
|
#
|
|
|
|
# create & close
|
|
|
|
#
|
2023-10-12 00:03:58 +02:00
|
|
|
create_pull_request
|
2023-10-13 15:30:22 +02:00
|
|
|
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha)
|
2023-10-13 19:10:50 +02:00
|
|
|
close_pull_request
|
2023-10-13 21:08:52 +02:00
|
|
|
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha)
|
2023-10-13 19:10:50 +02:00
|
|
|
|
2023-10-13 21:08:52 +02:00
|
|
|
#
|
|
|
|
# create & merge
|
|
|
|
#
|
|
|
|
create_pull_request
|
|
|
|
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha)
|
|
|
|
merge_pull_request
|
2023-10-13 19:10:50 +02:00
|
|
|
wait_success ${options[url]}/api/v1/repos/user1/originrepo $(cat $TMPDIR/originrepo.sha)
|
2023-10-11 15:39:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 16:01:37 +02:00
|
|
|
function unit() {
|
|
|
|
unit_retry
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
unit
|
|
|
|
integration
|
|
|
|
}
|
|
|
|
|
2023-10-11 15:39:52 +02:00
|
|
|
function main() {
|
|
|
|
local command=run
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
--verbose)
|
|
|
|
shift
|
|
|
|
verbose
|
|
|
|
;;
|
|
|
|
--debug)
|
|
|
|
shift
|
|
|
|
debug
|
|
|
|
;;
|
2023-10-11 15:50:12 +02:00
|
|
|
--host_port)
|
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
options[host_port]=$1
|
2023-10-13 15:45:50 +02:00
|
|
|
shift
|
2023-10-11 15:50:12 +02:00
|
|
|
;;
|
|
|
|
--url)
|
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
options[url]=$1
|
2023-10-13 15:45:50 +02:00
|
|
|
shift
|
2023-10-11 15:50:12 +02:00
|
|
|
;;
|
|
|
|
--token)
|
|
|
|
shift
|
2023-10-11 18:05:11 +02:00
|
|
|
options[token]=$1
|
2023-10-13 15:45:50 +02:00
|
|
|
shift
|
2023-10-11 15:50:12 +02:00
|
|
|
;;
|
2023-10-11 15:39:52 +02:00
|
|
|
*)
|
2023-10-13 15:23:50 +02:00
|
|
|
finalize_options
|
2023-10-11 18:05:11 +02:00
|
|
|
"${1:-run}"
|
2023-10-11 15:39:52 +02:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:47:22 +02:00
|
|
|
dependencies
|
|
|
|
|
2023-10-11 15:39:52 +02:00
|
|
|
${MAIN:-main} "${@}"
|