From f4d0c03302d9b86c247faa7ab4a1c6b5cf1afe9d Mon Sep 17 00:00:00 2001 From: Twenty Panda Date: Fri, 15 Mar 2024 09:57:35 +0700 Subject: [PATCH] upgrades: remove ad-hoc code and use forgejo-curl.sh instead --- .gitignore | 3 -- forgejo/upgrades/fixtures.sh | 18 +++++------ forgejo/upgrades/test-upgrade.sh | 55 +++++++++++--------------------- 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index 5c2e2ef..ff9fe83 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,9 @@ *~ -forgejo-api -forgejo-header forgejo-ip forgejo-runner-home forgejo-runner-pid forgejo-runner-token forgejo-runner.clientpid forgejo-runner.log -forgejo-token .runner #* diff --git a/forgejo/upgrades/fixtures.sh b/forgejo/upgrades/fixtures.sh index 335b53c..abad7e3 100644 --- a/forgejo/upgrades/fixtures.sh +++ b/forgejo/upgrades/fixtures.sh @@ -97,8 +97,8 @@ function fixture_lfs_assert_local() { function fixture_packages_create() { echo PACKAGE_CONTENT > $DIR/fixture/package - $work_path/forgejo-api -X DELETE http://${HOST_PORT}/api/packages/${FORGEJO_USER}/generic/test_package/1.0.0/file.txt || true - $work_path/forgejo-api --upload-file $DIR/fixture/package http://${HOST_PORT}/api/packages/${FORGEJO_USER}/generic/test_package/1.0.0/file.txt + forgejo-curl.sh api_json -X DELETE http://${HOST_PORT}/api/packages/${FORGEJO_USER}/generic/test_package/1.0.0/file.txt || true + forgejo-curl.sh api_json --upload-file $DIR/fixture/package http://${HOST_PORT}/api/packages/${FORGEJO_USER}/generic/test_package/1.0.0/file.txt } function fixture_packages_assert_s3() { @@ -115,7 +115,7 @@ function fixture_packages_assert_local() { function fixture_avatars_create() { echo -n $ONEPIXEL | base64 --decode > $DIR/avatar.png - $work_path/forgejo-client --form avatar=@$DIR/avatar.png http://${HOST_PORT}/user/settings/avatar + forgejo-curl.sh web --form avatar=@$DIR/avatar.png http://${HOST_PORT}/user/settings/avatar } function fixture_avatars_assert_s3() { @@ -132,9 +132,9 @@ function fixture_avatars_assert_local() { function fixture_repo_avatars_create() { echo -n $ONEPIXEL | base64 --decode > $DIR/repo-avatar.png - $work_path/forgejo-client --form avatar=@$DIR/repo-avatar.png http://${HOST_PORT}/${FORGEJO_USER}/${FORGEJO_REPO}/settings/avatar + forgejo-curl.sh web --form avatar=@$DIR/repo-avatar.png http://${HOST_PORT}/${FORGEJO_USER}/${FORGEJO_REPO}/settings/avatar # v1.21 only - #$work_path/forgejo-api -X POST --data-raw '{"body":"'$avatar'"}' http://${HOST_PORT}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}/avatar + #forgejo-curl.sh api_json -X POST --data-raw '{"body":"'$avatar'"}' http://${HOST_PORT}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}/avatar } function fixture_repo_avatars_assert_s3() { @@ -153,17 +153,17 @@ function fixture_repo_avatars_assert_local() { function fixture_attachments_create_1_18() { echo -n $ONEPIXEL | base64 --decode > $DIR/attachment.png - $work_path/forgejo-client --trace-ascii - --form file=@$DIR/attachment.png http://${HOST_PORT}/${FORGEJO_USER}/${FORGEJO_REPO}/issues/attachments + forgejo-curl.sh web --trace-ascii - --form file=@$DIR/attachment.png http://${HOST_PORT}/${FORGEJO_USER}/${FORGEJO_REPO}/issues/attachments } function fixture_attachments_create() { - if $work_path/forgejo-api http://${HOST_PORT}/api/v1/version | grep --quiet --fixed-strings 1.18. ; then + if forgejo-curl.sh api_json http://${HOST_PORT}/api/v1/version | grep --quiet --fixed-strings 1.18. ; then fixture_attachments_create_1_18 return fi - id=$($work_path/forgejo-api --data-raw '{"title":"TITLE"}' http://${HOST_PORT}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}/issues | jq .id) + id=$(forgejo-curl.sh api_json --data-raw '{"title":"TITLE"}' http://${HOST_PORT}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}/issues | jq .id) echo -n $ONEPIXEL | base64 --decode > $DIR/attachment.png - $work_path/forgejo-client -H @$DIR/forgejo-work-path/forgejo-header --form name=attachment.png --form attachment=@$DIR/attachment.png http://${HOST_PORT}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}/issues/$id/assets + forgejo-curl.sh api --form name=attachment.png --form attachment=@$DIR/attachment.png http://${HOST_PORT}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}/issues/$id/assets } function fixture_attachments_assert_s3() { diff --git a/forgejo/upgrades/test-upgrade.sh b/forgejo/upgrades/test-upgrade.sh index 14313c4..8d825f1 100755 --- a/forgejo/upgrades/test-upgrade.sh +++ b/forgejo/upgrades/test-upgrade.sh @@ -21,6 +21,8 @@ HOST_PORT=0.0.0.0:3000 STORAGE_PATHS="attachments avatars lfs packages repo-archive repo-avatars" STORAGE_FUN="attachments avatars lfs packages repo_archive repo_avatars" DIR=/tmp/forgejo-upgrades +export DOT_FORGEJO_CURL=$DIR/forgejo-curl +export DOT=$DOT_FORGEJO_CURL # for backward compatibility with forgejo-curl.sh 1.0.0 if ${VERBOSE:-false} ; then set -ex PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: ' @@ -47,17 +49,23 @@ function log_info() { function dependencies() { maybe_sudo + + if ! test -f /usr/local/bin/forgejo-curl.sh ; then + $SUDO curl --fail -sS https://code.forgejo.org/forgejo/forgejo-curl/raw/branch/main/forgejo-curl.sh -o /usr/local/bin/forgejo-curl.sh + $SUDO chmod +x /usr/local/bin/forgejo-curl.sh + fi + if ! which curl daemon jq git-lfs > /dev/null ; then export DEBIAN_FRONTEND=noninteractive $SUDO apt-get update -qq $SUDO apt-get install -y -qq curl daemon git-lfs jq sqlite3 gettext-base fi - if ! test -f /usr/local/bin/mc || ! test -f /usr/local/bin/minio > /dev/null ; then + if ! test -f /usr/local/bin/mc || ! test -f /usr/local/bin/minio ; then $SUDO curl --fail -sS https://dl.min.io/client/mc/release/linux-amd64/mc -o /usr/local/bin/mc $SUDO curl --fail -sS https://dl.min.io/server/minio/release/linux-amd64/minio -o /usr/local/bin/minio fi - if ! test -x /usr/local/bin/mc || ! test -x /usr/local/bin/minio > /dev/null ; then + if ! test -x /usr/local/bin/mc || ! test -x /usr/local/bin/minio ; then $SUDO chmod +x /usr/local/bin/mc $SUDO chmod +x /usr/local/bin/minio fi @@ -155,7 +163,7 @@ function start_forgejo() { grep '' $work_path/log/*.log 2> /dev/null return 1 fi - create_user $version + create_user_and_login $version } function start_minio() { @@ -228,47 +236,22 @@ function start() { start_forgejo $version } -function create_user() { +function create_user_and_login() { local version=$1 local work_path=$DIR/forgejo-work-path - - if test -f $work_path/forgejo-token; then - return + local email="$FORGEJO_USER@example.com" + local cli="$DIR/forgejo-$version --config $work_path/app.ini --work-path $work_path" + if ! $cli admin user list | grep --quiet "$email" ; then + $cli admin user create --admin --username "$FORGEJO_USER" --password "$FORGEJO_PASSWORD" --email $email fi - local cli="$DIR/forgejo-$version --config $work_path/app.ini --work-path $work_path" - $cli admin user create --admin --username "$FORGEJO_USER" --password "$FORGEJO_PASSWORD" --email "$FORGEJO_USER@example.com" - local scopes="--scopes all" + forgejo-curl.sh logout + local scopes='--scopes ["all"]' if echo $version | grep --quiet 1.18 ; then scopes="" fi - - # - # forgejo-cli is to use with api/v1 enpoints - # - # tail -1 is because there are logs creating noise in the output in v1.19.4-0 - # - $cli admin user generate-access-token -u $FORGEJO_USER --raw $scopes | tail -1 > $work_path/forgejo-token - ( echo -n 'Authorization: token ' ; cat $work_path/forgejo-token ) > $work_path/forgejo-header - ( echo "#!/bin/sh" ; echo 'curl -f -sS -H "Content-Type: application/json" -H @'$work_path/forgejo-header' "$@"' ) > $work_path/forgejo-api && chmod +x $work_path/forgejo-api - $work_path/forgejo-api http://${HOST_PORT}/api/v1/version - - # - # forgejo-client is to use with web endpoints - # - # - # login and obtain a CSRF, all stored in the cookie file - # - ( echo "#!/bin/sh" ; echo 'curl --cookie-jar '$DIR/cookies' --cookie '$DIR/cookies' -f -sS "$@"' ) > $work_path/forgejo-client-update-cookies && chmod +x $work_path/forgejo-client-update-cookies - $work_path/forgejo-client-update-cookies http://${HOST_PORT}/user/login -o /dev/null - $work_path/forgejo-client-update-cookies --verbose -X POST --data user_name=${FORGEJO_USER} --data password=${FORGEJO_PASSWORD} http://${HOST_PORT}/user/login >& $DIR/login.html - $work_path/forgejo-client-update-cookies http://${HOST_PORT}/user/login -o /dev/null - local csrf=$(sed -n -e '/csrf/s/.*csrf\t//p' $DIR/cookies) - # - # use the cookie file but do not modify it - # - ( echo "#!/bin/sh" ; echo 'curl --cookie '$DIR/cookies' -H "X-Csrf-Token: '$csrf'" -f -sS "$@"' ) > $work_path/forgejo-client && chmod +x $work_path/forgejo-client + forgejo-curl.sh --user "$FORGEJO_USER" --password "$FORGEJO_PASSWORD" $scopes login http://${HOST_PORT} } function stop_daemon() {