From 245db64a1ee93633f5d88b35dd91bc823cca3c24 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 26 Mar 2023 23:03:56 +0200 Subject: [PATCH] adapt and test --- .forgejo/workflows/integration.yml | 8 ++++-- forgejo-release.sh | 25 ++++++++--------- testdata/forgejo-release-test.sh | 45 +++++++++++++++++------------- 3 files changed, 42 insertions(+), 36 deletions(-) mode change 100644 => 100755 testdata/forgejo-release-test.sh diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index c3434bc..d8d14ea 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -7,8 +7,12 @@ jobs: - id: forgejo uses: https://code.forgejo.org/actions/setup-forgejo@v1 with: + user: testuser image-version: 1.19 - run: | set -ex - curl ${{ steps.forgejo.outputs.url }}/api/forgejo/v1/version | grep 1.19 - ls -l forgejo-release.sh + curl -sS ${{ steps.forgejo.outputs.url }}/api/forgejo/v1/version | grep 1.19 + export FORGEJO="${{ steps.forgejo.outputs.url }}" + export RELEASETEAMTOKEN="${{ steps.forgejo.outputs.token }}" + export CI_REPO_OWNER=testuser + testdata/forgejo-release-test.sh test_run diff --git a/forgejo-release.sh b/forgejo-release.sh index 15de8e8..1fd8ac2 100755 --- a/forgejo-release.sh +++ b/forgejo-release.sh @@ -3,13 +3,10 @@ set -ex : ${PULL_USER:=forgejo-integration} -if test "$CI_REPO" = "forgejo/release" ; then - : ${PUSH_USER:=forgejo} -else - : ${PUSH_USER:=forgejo-experimental} -fi +: ${PUSH_USER:=forgejo} : ${TAG:=${CI_COMMIT_TAG}} -: ${DOMAIN:=codeberg.org} +: ${FORGEJO:=https://codeberg.org} +: ${REPO:=forgejo} : ${RELEASE_DIR:=dist/release} : ${BIN_DIR:=/tmp} : ${TEA_VERSION:=0.9.0} @@ -23,7 +20,7 @@ setup_tea() { } ensure_tag() { - if api GET repos/$PUSH_USER/forgejo/tags/$TAG > /tmp/tag.json ; then + if api GET repos/$PUSH_USER/$REPO/tags/$TAG > /tmp/tag.json ; then local sha=$(jq --raw-output .commit.sha < /tmp/tag.json) if test "$sha" != "$CI_COMMIT_SHA" ; then cat /tmp/tag.json @@ -31,7 +28,7 @@ ensure_tag() { false fi else - api POST repos/$PUSH_USER/forgejo/tags --data-raw '{"tag_name": "'$CI_COMMIT_TAG'", "target": "'$CI_COMMIT_SHA'"}' + api POST repos/$PUSH_USER/$REPO/tags --data-raw '{"tag_name": "'$CI_COMMIT_TAG'", "target": "'$CI_COMMIT_SHA'"}' fi } @@ -42,19 +39,19 @@ upload() { test ${RELEASETYPE+false} || echo "Uploading as Stable" ensure_tag anchor=$(echo $CI_COMMIT_TAG | sed -e 's/^v//' -e 's/[^a-zA-Z0-9]/-/g') - $BIN_DIR/tea release create $ASSETS --repo $PUSH_USER/forgejo --note "See https://codeberg.org/forgejo/forgejo/src/branch/forgejo/RELEASE-NOTES.md#${anchor}" --tag $CI_COMMIT_TAG --title $CI_COMMIT_TAG ${RELEASETYPE} + $BIN_DIR/tea release create $ASSETS --repo $PUSH_USER/$REPO --note "$RELEASENOTES" --tag $CI_COMMIT_TAG --title $CI_COMMIT_TAG ${RELEASETYPE} } push() { setup_api setup_tea - GITEA_SERVER_TOKEN=$RELEASETEAMTOKEN $BIN_DIR/tea login add --name $RELEASETEAMUSER --url $DOMAIN + GITEA_SERVER_TOKEN=$RELEASETEAMTOKEN $BIN_DIR/tea login add --name $RELEASETEAMUSER --url $FORGEJO upload } setup_api() { - if ! which jq || ! which curl ; then - apk --update --no-cache add jq curl + if ! which jq curl ; then + apt-get install -y -qq jq curl fi } @@ -64,7 +61,7 @@ api() { path=$1 shift - curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $RELEASETEAMTOKEN" "$@" https://$DOMAIN/api/v1/$path + curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $RELEASETEAMTOKEN" "$@" $FORGEJO/api/v1/$path } pull() { @@ -72,7 +69,7 @@ pull() { ( mkdir -p $RELEASE_DIR cd $RELEASE_DIR - api GET repos/$PULL_USER/forgejo/releases/tags/$TAG > /tmp/assets.json + api GET repos/$PULL_USER/$REPO/releases/tags/$TAG > /tmp/assets.json jq --raw-output '.assets[] | "\(.name) \(.browser_download_url)"' < /tmp/assets.json | while read name url ; do wget --quiet -O $name $url done diff --git a/testdata/forgejo-release-test.sh b/testdata/forgejo-release-test.sh old mode 100644 new mode 100755 index b592e92..210c317 --- a/testdata/forgejo-release-test.sh +++ b/testdata/forgejo-release-test.sh @@ -2,31 +2,43 @@ set -ex +DIR=$(mktemp -d) + +#trap "rm -fr $DIR" EXIT + test_teardown() { setup_api - api DELETE repos/$PUSH_USER/forgejo/releases/tags/$TAG || true - api DELETE repos/$PUSH_USER/forgejo/tags/$TAG || true + api DELETE repos/$PUSH_USER/$REPO/releases/tags/$TAG || true + api DELETE repos/$PUSH_USER/$REPO/tags/$TAG || true rm -fr dist/release setup_tea $BIN_DIR/tea login delete $RELEASETEAMUSER || true } +test_reset_repo() { + api DELETE repos/$PUSH_USER/$REPO || true + api POST user/repos --data-raw '{"name":"'$REPO'", "auto_init":true}' + git clone $FORGEJO/$PUSH_USER/$REPO $DIR/repo + CI_COMMIT_SHA=$(git -C $DIR/repo rev-parse HEAD) +} + test_setup() { + test_reset_repo mkdir -p $RELEASE_DIR touch $RELEASE_DIR/file-one.txt touch $RELEASE_DIR/file-two.txt } test_ensure_tag() { - api DELETE repos/$PUSH_USER/forgejo/tags/$TAG || true + api DELETE repos/$PUSH_USER/$REPO/tags/$TAG || true # # idempotent # ensure_tag - api GET repos/$PUSH_USER/forgejo/tags/$TAG > /tmp/tag1.json + api GET repos/$PUSH_USER/$REPO/tags/$TAG > $DIR/tag1.json ensure_tag - api GET repos/$PUSH_USER/forgejo/tags/$TAG > /tmp/tag2.json - diff -u /tmp/tag[12].json + api GET repos/$PUSH_USER/$REPO/tags/$TAG > $DIR/tag2.json + diff -u $DIR/tag[12].json # # sanity check on the SHA of an existing tag # @@ -34,21 +46,13 @@ test_ensure_tag() { CI_COMMIT_SHA=12345 ! ensure_tag ) - api DELETE repos/$PUSH_USER/forgejo/tags/$TAG + api DELETE repos/$PUSH_USER/$REPO/tags/$TAG } -# -# Running the test locally instead of within Woodpecker -# -# 1. Setup: obtain a token at https://codeberg.org/user/settings/applications -# 2. Run: RELEASETEAMUSER= RELEASETEAMTOKEn= binaries-pull-push-test.sh test_run -# 3. Verify: (optional) manual verification at https://codeberg.org//forgejo/releases -# 4. Cleanup: RELEASETEAMUSER= RELEASETEAMTOKEn= binaries-pull-push-test.sh test_teardown -# test_run() { test_teardown - to_push=/tmp/binaries-releases-to-push - pulled=/tmp/binaries-releases-pulled + to_push=$DIR/binaries-releases-to-push + pulled=$DIR/binaries-releases-pulled RELEASE_DIR=$to_push test_setup test_ensure_tag @@ -60,10 +64,11 @@ test_run() { echo "================================ TEST END" } -: ${CI_REPO_OWNER:=dachary} +: ${RELEASETEAMUSER:=root} +: ${REPO:=testrepo} +: ${CI_REPO_OWNER:=root} : ${PULL_USER=$CI_REPO_OWNER} : ${PUSH_USER=$CI_REPO_OWNER} -: ${CI_COMMIT_TAG:=W17.8.20-1} -: ${CI_COMMIT_SHA:=$(git rev-parse HEAD)} +: ${CI_COMMIT_TAG:=v17.8.20-1} . $(dirname $0)/../forgejo-release.sh