#!/bin/bash # SPDX-License-Identifier: MIT set -e SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" TMPDIR=/tmp/cascading-pr-test source $SELF_DIR/../cascading-pr-lib.sh function push_self() { forgejo-test-helper.sh push_self_action http://user1:admin1234@${options[host_port]} user1 cascading-pr vTest } 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 DOT=$TMPDIR/$username forgejo-curl.sh "$@" } function user_token() { local username=$1 name=$2 curl -sS -f -H Content-Type:application/json --user "$username:${options[password]}" --data-raw '{"name":"'$name'","scopes":["write:repository","write:issue"]}' ${options[url]}/api/v1/users/$username/tokens | jq --raw-output .sha1 | tee $TMPDIR/$username/token } 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() { local username="$1" email="$2" forgejo-curl.sh api_json -X DELETE ${options[url]}/api/v1/admin/users/$username?purge=true >& /dev/null || true 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 } 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 ) forgejo-curl.sh api_json --data-raw '{"title":"PR","base":"main","head":"branch1"}' ${options[url]}/api/v1/repos/user1/originrepo/pulls } function run() { user_create user2 user2@example.com forgejo-test-helper.sh push tests/destinationrepo http://user2:admin1234@${options[host_port]} user2 destinationrepo 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) create_pull_request push_self echo do something } function main() { local command=run options[host_port]=$(cat forgejo-ip):3000 options[url]=http://${options[host_port]} options[token]=$(cat forgejo-token) options[password]=admin1234 while true; do case "$1" in --verbose) shift verbose ;; --debug) shift debug ;; --host_port) shift options[host_port]=$1 ;; --url) shift options[url]=$1 ;; --token) shift options[token]=$1 ;; *) "${1:-run}" return 0 ;; esac done } ${MAIN:-main} "${@}"