From ba4ac4e80dfb30b8cc34b6077c4f8912ba857a61 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Wed, 29 Mar 2023 17:16:26 +0200 Subject: [PATCH] test upload & download --- .forgejo/workflows/integration.yml | 6 +- action.yml | 41 +++++++- forgejo-release.sh | 53 +++++----- testdata/run.sh | 97 ------------------- testdata/try-setup-forgejo-release.yml | 11 --- .../.forgejo/workflows/test.yml | 36 +++++++ testdata/upload-download/upload-dir/file1.txt | 1 + testdata/upload-download/upload-dir/file2.txt | 1 + 8 files changed, 107 insertions(+), 139 deletions(-) delete mode 100755 testdata/run.sh delete mode 100644 testdata/try-setup-forgejo-release.yml create mode 100644 testdata/upload-download/.forgejo/workflows/test.yml create mode 100644 testdata/upload-download/upload-dir/file1.txt create mode 100644 testdata/upload-download/upload-dir/file2.txt diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index b8c8659..c8da8e2 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -12,13 +12,13 @@ jobs: image-version: 1.19 - name: 001 run: | + set -ex export FORGEJO_RUNNER_LOGS="${{ steps.forgejo.outputs.runner-logs }}" - testdata/run.sh push_self http://testuser:admin1234@${{ steps.forgejo.outputs.host-port }} testuser setup-forgejo-release - testdata/run.sh workflow http://testuser:admin1234@${{ steps.forgejo.outputs.host-port }} testuser try-setup-forgejo-release + forgejo-test-helper.sh push_self_action http://testuser:admin1234@${{ steps.forgejo.outputs.host-port }} testuser forgejo-release vTest + forgejo-test-helper.sh run_workflow testdata/upload-download http://testuser:admin1234@${{ steps.forgejo.outputs.host-port }} testuser upload-download forgejo-release - name: 002 run: | set -ex - 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 diff --git a/action.yml b/action.yml index 3fbc0db..966a170 100644 --- a/action.yml +++ b/action.yml @@ -1,10 +1,45 @@ -name: 'Setup Forgejo release tools' +name: 'Forgejo release download and upload' author: 'Forgejo authors' description: | - Install dependencies of the forgejo-release-upload and forgejo-release-download - actions. + Upload or download the assets of a release to a Forgejo instance. +inputs: + url: + description: 'URL of the Forgejo instance' + required: true + repo: + description: 'owner/project relative to the URL' + required: true + tag: + description: 'Tag of the release' + required: true + doer: + description: 'Forgejo user authoring the upload' + token: + description: 'Forgejo application token' + required: true + release-dir: + description: 'Directory in whichs release assets are uploaded or downloaded' + required: true + direction: + description: 'Can either be download or upload' + required: true + verbose: + description: 'Increase the verbosity level' + default: 'false' + runs: using: "composite" steps: - run: echo "${{ github.action_path }}" >> $GITHUB_PATH shell: bash + - run: | + export FORGEJO="${{ inputs.url }}" + export REPO="${{ inputs.repo }}" + export TAG="${{ inputs.tag }}" + export DOER="${{ inputs.doer }}" + export TOKEN="${{ inputs.token }}" + export RELEASE_DIR="${{ inputs.release-dir }}" + export VERBOSE="${{ inputs.verbose }}" + export SHA="${{ github.sha }}" + forgejo-release.sh ${{ inputs.direction }} + shell: bash diff --git a/forgejo-release.sh b/forgejo-release.sh index 1fd8ac2..a6cd273 100755 --- a/forgejo-release.sh +++ b/forgejo-release.sh @@ -1,17 +1,15 @@ #!/bin/bash -set -ex +set -e + +if ${VERBOSE:-false}; then set -x; fi -: ${PULL_USER:=forgejo-integration} -: ${PUSH_USER:=forgejo} -: ${TAG:=${CI_COMMIT_TAG}} : ${FORGEJO:=https://codeberg.org} -: ${REPO:=forgejo} +: ${REPO:=forgejo-integration/forgejo} : ${RELEASE_DIR:=dist/release} -: ${BIN_DIR:=/tmp} +: ${BIN_DIR:=$(mktemp -d)} : ${TEA_VERSION:=0.9.0} - setup_tea() { if ! test -f $BIN_DIR/tea ; then curl -sL https://dl.gitea.io/tea/$TEA_VERSION/tea-$TEA_VERSION-linux-amd64 > $BIN_DIR/tea @@ -20,33 +18,38 @@ setup_tea() { } ensure_tag() { - if api GET repos/$PUSH_USER/$REPO/tags/$TAG > /tmp/tag.json ; then + if api GET repos/$REPO/tags/$TAG > /tmp/tag.json ; then local sha=$(jq --raw-output .commit.sha < /tmp/tag.json) - if test "$sha" != "$CI_COMMIT_SHA" ; then + if test "$sha" != "$SHA" ; then cat /tmp/tag.json - echo "the tag SHA in the $PUSH_USER repository does not match the tag SHA that triggered the build: $CI_COMMIT_SHA" + echo "the tag SHA in the $REPO repository does not match the tag SHA that triggered the build: $SHA" false fi else - api POST repos/$PUSH_USER/$REPO/tags --data-raw '{"tag_name": "'$CI_COMMIT_TAG'", "target": "'$CI_COMMIT_SHA'"}' + api POST repos/$REPO/tags --data-raw '{"tag_name": "'$TAG'", "target": "'$SHA'"}' fi } -upload() { - ASSETS=$(ls $RELEASE_DIR/* | sed -e 's/^/-a /') - echo "${CI_COMMIT_TAG}" | grep -qi '\-rc' && export RELEASETYPE="--prerelease" && echo "Uploading as Pre-Release" - echo "${CI_COMMIT_TAG}" | grep -qi '\-test' && export RELEASETYPE="--draft" && echo "Uploading as Draft" - test ${RELEASETYPE+false} || echo "Uploading as Stable" +upload_release() { + local assets=$(ls $RELEASE_DIR/* | sed -e 's/^/-a /') + local releasetype + echo "${TAG}" | grep -qi '\-rc' && export releasetype="--prerelease" && echo "Uploading as Pre-Release" + echo "${TAG}" | grep -qi '\-test' && export releasetype="--draft" && echo "Uploading as Draft" + 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/$REPO --note "$RELEASENOTES" --tag $CI_COMMIT_TAG --title $CI_COMMIT_TAG ${RELEASETYPE} + anchor=$(echo $TAG | sed -e 's/^v//' -e 's/[^a-zA-Z0-9]/-/g') + $BIN_DIR/tea release create $assets --repo $REPO --note "$RELEASENOTES" --tag $TAG --title $TAG ${releasetype} } -push() { +upload() { setup_api setup_tea - GITEA_SERVER_TOKEN=$RELEASETEAMTOKEN $BIN_DIR/tea login add --name $RELEASETEAMUSER --url $FORGEJO - upload + if ! test "$DOER"; then + echo 'missing DOER' + return 1 + fi + GITEA_SERVER_TOKEN=$TOKEN $BIN_DIR/tea login add --name $DOER --url $FORGEJO + upload_release } setup_api() { @@ -61,15 +64,15 @@ api() { path=$1 shift - curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $RELEASETEAMTOKEN" "$@" $FORGEJO/api/v1/$path + curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $TOKEN" "$@" $FORGEJO/api/v1/$path } -pull() { +download() { setup_api ( mkdir -p $RELEASE_DIR cd $RELEASE_DIR - api GET repos/$PULL_USER/$REPO/releases/tags/$TAG > /tmp/assets.json + api GET repos/$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 @@ -78,7 +81,7 @@ pull() { missing() { - echo need pull or push argument got nothing + echo need upload or download argument got nothing exit 1 } diff --git a/testdata/run.sh b/testdata/run.sh deleted file mode 100755 index d2aef31..0000000 --- a/testdata/run.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/bin/bash - -set -ex - -: ${FORGEJO_RUNNER_LOGS:=../setup-forgejo/forgejo-runner.log} -DATA=$(dirname $0) -DIR=$(mktemp -d) - -trap "rm -fr $DIR" EXIT - -function check_status() { - local forgejo="$1" - local repo="$2" - local sha="$3" - - if ! which jq > /dev/null ; then - apt-get install -y -qq jq - fi - local state=$(curl --fail -sS "$forgejo/api/v1/repos/$repo/commits/$sha/status" | jq --raw-output .state) - echo $state - test "$state" != "" -a "$state" != "pending" -a "$state" != "running" -a "$state" != "null" -} - -function wait_success() { - local forgejo="$1" - local repo="$2" - local sha="$3" - - for i in $(seq 40); do - if check_status "$forgejo" "$repo" "$sha"; then - break - fi - tail $FORGEJO_RUNNER_LOGS - sleep 10 - done - if ! test "$(check_status "$forgejo" "$repo" "$sha")" = "success" ; then - cat $FORGEJO_RUNNER_LOGS - return 1 - fi -} - -function push() { - local forgejo="$1" - local owner="$2" - local workflow="$3" - - local dir="$DIR/$workflow" - mkdir -p $dir/.forgejo/workflows - sed -e "s|SELF|$forgejo/$owner|" \ - < $DATA/$workflow.yml > $dir/.forgejo/workflows/$workflow.yml - ( - cd $dir - git init - git checkout -b main - git config user.email root@example.com - git config user.name username - git add . - git commit -m 'initial commit' - git remote add origin $forgejo/$owner/$workflow - git push --force -u origin main - git rev-parse HEAD > SHA - ) -} - -function workflow() { - local forgejo="${1}" - local owner="${2}" - local workflow="${3}" - - push "$forgejo" "$owner" "$workflow" - wait_success "$forgejo" "$owner/$workflow" $(cat $DIR/$workflow/SHA) -} - -function push_self() { - local forgejo="$1" - local owner="$2" - local self="$3" - - local dir="$DIR/self" - git clone . $dir - ( - cd $dir - rm -fr .forgejo .git - git init - git checkout -b main - git remote add origin $forgejo/$owner/$self - git config user.email root@example.com - git config user.name username - git add . - git commit -m 'initial commit' - git push --force origin main - git tag --force vTest HEAD - git push --force origin vTest - ) -} - -"$@" diff --git a/testdata/try-setup-forgejo-release.yml b/testdata/try-setup-forgejo-release.yml deleted file mode 100644 index 8fd502f..0000000 --- a/testdata/try-setup-forgejo-release.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: Setup Forgejo Release -run-name: ${{ github.actor }} is setting up Forgejo Release -on: [push] -jobs: - setup-forgejo: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: SELF/setup-forgejo-release@vTest - - run: | - type forgejo-release.sh diff --git a/testdata/upload-download/.forgejo/workflows/test.yml b/testdata/upload-download/.forgejo/workflows/test.yml new file mode 100644 index 0000000..f1c122f --- /dev/null +++ b/testdata/upload-download/.forgejo/workflows/test.yml @@ -0,0 +1,36 @@ +name: Upload a Forgejo Release +on: [push] +jobs: + setup-forgejo: + runs-on: self-hosted + steps: + - uses: actions/checkout@v3 + - id: forgejo + uses: https://code.forgejo.org/actions/setup-forgejo@v1 + with: + user: root + password: admin1234 + - id: release-upload + uses: SELF@vTest + with: + direction: upload + url: ${{ steps.forgejo.outputs.url }} + repo: root/upload-download + tag: v1.0 + doer: root + token: ${{ steps.forgejo.outputs.token }} + release-dir: upload-dir + verbose: true + - id: release-download + uses: SELF@vTest + with: + direction: download + url: ${{ steps.forgejo.outputs.url }} + repo: root/upload-download + tag: v1.0 + doer: root + token: ${{ steps.forgejo.outputs.token }} + release-dir: download-dir + verbose: true + - run: | + diff -u upload-dir download-dir diff --git a/testdata/upload-download/upload-dir/file1.txt b/testdata/upload-download/upload-dir/file1.txt new file mode 100644 index 0000000..cb4ee5e --- /dev/null +++ b/testdata/upload-download/upload-dir/file1.txt @@ -0,0 +1 @@ +FILE1 diff --git a/testdata/upload-download/upload-dir/file2.txt b/testdata/upload-download/upload-dir/file2.txt new file mode 100644 index 0000000..d77231c --- /dev/null +++ b/testdata/upload-download/upload-dir/file2.txt @@ -0,0 +1 @@ +FILE2