From a5aa633273b7834ec49650d586e0807d40a359ba Mon Sep 17 00:00:00 2001 From: benniekiss Date: Mon, 8 Jul 2024 18:49:13 +0000 Subject: [PATCH] Support downloading the latest release of a repo (#16) This PR adds the functionality to download the latest release from a repo. The API provides an endpoint to do so, so an option has been added to the action. This closes #12 Co-authored-by: benniekiss Reviewed-on: https://code.forgejo.org/actions/forgejo-release/pulls/16 Reviewed-by: earl-warren Co-authored-by: benniekiss Co-committed-by: benniekiss --- README.md | 39 ++++++++++--------- action.yml | 5 +++ forgejo-release.sh | 12 ++++-- .../.forgejo/workflows/test.yml | 37 ++++++++++++++++++ .../upload-dir-v2/file1-v2.txt | 1 + .../upload-dir-v2/file2-v2.txt | 1 + .../.forgejo/workflows/test.yml | 28 ++++++++++++- .../upload-dir-v2/file1-v2.txt | 1 + .../upload-dir-v2/file2-v2.txt | 1 + 9 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 testdata/nested-upload-download/upload-dir-v2/file1-v2.txt create mode 100644 testdata/nested-upload-download/upload-dir-v2/file2-v2.txt create mode 100644 testdata/upload-download/upload-dir-v2/file1-v2.txt create mode 100644 testdata/upload-download/upload-dir-v2/file2-v2.txt diff --git a/README.md b/README.md index 2a18930..13e5aac 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,31 @@ # forgejo-release - + ## Description Upload or download the assets of a release to a Forgejo instance. - - + + ## Inputs -| parameter | description | required | default | +| name | description | required | default | | --- | --- | --- | --- | -| url | URL of the Forgejo instance | `false` | | -| repo | owner/project relative to the URL | `false` | | -| tag | Tag of the release | `false` | | -| sha | SHA of the release | `false` | | -| token | Forgejo application token | `true` | | -| release-dir | Directory in whichs release assets are uploaded or downloaded | `true` | | -| release-notes | Release notes | `false` | | -| direction | Can either be download or upload | `true` | | -| gpg-private-key | GPG Private Key to sign the release artifacts | `false` | | -| gpg-passphrase | Passphrase of the GPG Private Key | `false` | | -| download-retry | Number of times to retry if the release is not ready (default 1) | `false` | | -| verbose | Increase the verbosity level | `false` | false | -| override | Override an existing release by the same {tag} | `false` | false | -| prerelease | Mark Release as Pre-Release | `false` | false | - +| `url` |

URL of the Forgejo instance

| `false` | `""` | +| `repo` |

owner/project relative to the URL

| `false` | `""` | +| `tag` |

Tag of the release

| `false` | `""` | +| `sha` |

SHA of the release

| `false` | `""` | +| `token` |

Forgejo application token

| `true` | `""` | +| `release-dir` |

Directory in whichs release assets are uploaded or downloaded

| `true` | `""` | +| `release-notes` |

Release notes

| `false` | `""` | +| `direction` |

Can either be download or upload

| `true` | `""` | +| `gpg-private-key` |

GPG Private Key to sign the release artifacts

| `false` | `""` | +| `gpg-passphrase` |

Passphrase of the GPG Private Key

| `false` | `""` | +| `download-retry` |

Number of times to retry if the release is not ready (default 1)

| `false` | `""` | +| `download-latest` |

Download the latest release

| `false` | `false` | +| `verbose` |

Increase the verbosity level

| `false` | `false` | +| `override` |

Override an existing release by the same {tag}

| `false` | `false` | +| `prerelease` |

Mark Release as Pre-Release

| `false` | `false` | + ## Example diff --git a/action.yml b/action.yml index 1f2cf83..3a640ea 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,9 @@ inputs: description: 'Passphrase of the GPG Private Key' download-retry: description: 'Number of times to retry if the release is not ready (default 1)' + download-latest: + description: 'Download the latest release' + default: 'false' verbose: description: 'Increase the verbosity level' default: 'false' @@ -66,6 +69,8 @@ runs: TAG=${TAG##refs/tags/} fi + export DOWNLOAD_LATEST="${{ inputs.download-latest }}" + export PRERELEASE="${{ inputs.prerelease }}" export TOKEN="${{ inputs.token }}" diff --git a/forgejo-release.sh b/forgejo-release.sh index e113b4c..c9dae1c 100755 --- a/forgejo-release.sh +++ b/forgejo-release.sh @@ -8,6 +8,7 @@ if ${VERBOSE:-false}; then set -x; fi : ${FORGEJO:=https://codeberg.org} : ${REPO:=forgejo-integration/forgejo} : ${RELEASE_DIR:=dist/release} +: ${DOWNLOAD_LATEST:=false} : ${TMP_DIR:=$(mktemp -d)} : ${GNUPGHOME:=$TMP_DIR} : ${BIN_DIR:=$TMP_DIR} @@ -143,18 +144,23 @@ wait_release() { download() { setup_api - wait_release ( mkdir -p $RELEASE_DIR cd $RELEASE_DIR - api GET repos/$REPO/releases/tags/$TAG > $TMP_DIR/assets.json + if [[ ${DOWNLOAD_LATEST} == "true" ]] ; then + echo "Downloading the latest release" + api GET repos/$REPO/releases/latest > $TMP_DIR/assets.json + elif [[ ${DOWNLOAD_LATEST} == "false" ]] ; then + wait_release + echo "Downloading tagged release ${TAG}" + api GET repos/$REPO/releases/tags/$TAG > $TMP_DIR/assets.json + fi jq --raw-output '.assets[] | "\(.name) \(.browser_download_url)"' < $TMP_DIR/assets.json | while read name url ; do curl --fail -H "Authorization: token $TOKEN" -o $name -L $url done ) } - missing() { echo need upload or download argument got nothing exit 1 diff --git a/testdata/nested-upload-download/.forgejo/workflows/test.yml b/testdata/nested-upload-download/.forgejo/workflows/test.yml index 65c3bfa..4c6bd91 100644 --- a/testdata/nested-upload-download/.forgejo/workflows/test.yml +++ b/testdata/nested-upload-download/.forgejo/workflows/test.yml @@ -35,6 +35,31 @@ jobs: release-dir: upload-dir release-notes: "RELEASE NOTES" verbose: true + - id: release-upload-override + uses: SELF@vTest + with: + direction: upload + url: ${{ steps.forgejo.outputs.url }} + repo: testuser/testrepo + tag: v1.0 + sha: ${{ steps.testrepo.outputs.sha }} + token: ${{ steps.forgejo.outputs.token }} + release-dir: upload-dir + release-notes: "RELEASE NOTES" + override: true + verbose: true + - id: release-upload-v2 + uses: SELF@vTest + with: + direction: upload + url: ${{ steps.forgejo.outputs.url }} + repo: testuser/testrepo + tag: v2.0 + sha: ${{ steps.testrepo.outputs.sha }} + token: ${{ steps.forgejo.outputs.token }} + release-dir: upload-dir-v2 + release-notes: "RELEASE NOTES V2" + verbose: true - id: release-download uses: SELF@vTest with: @@ -47,5 +72,17 @@ jobs: verbose: true - run: | diff -u upload-dir download-dir + - id: release-download-latest + uses: SELF@vTest + with: + direction: download + url: ${{ steps.forgejo.outputs.url }} + repo: testuser/testrepo + token: ${{ steps.forgejo.outputs.token }} + release-dir: download-dir-v2 + download-latest: true + verbose: true + - run: | + diff -u upload-dir-v2 download-dir-v2 - if: failure() run: docker logs forgejo diff --git a/testdata/nested-upload-download/upload-dir-v2/file1-v2.txt b/testdata/nested-upload-download/upload-dir-v2/file1-v2.txt new file mode 100644 index 0000000..eb43d98 --- /dev/null +++ b/testdata/nested-upload-download/upload-dir-v2/file1-v2.txt @@ -0,0 +1 @@ +FILE1-V2 diff --git a/testdata/nested-upload-download/upload-dir-v2/file2-v2.txt b/testdata/nested-upload-download/upload-dir-v2/file2-v2.txt new file mode 100644 index 0000000..f9c8d3c --- /dev/null +++ b/testdata/nested-upload-download/upload-dir-v2/file2-v2.txt @@ -0,0 +1 @@ +FILE2-V2 diff --git a/testdata/upload-download/.forgejo/workflows/test.yml b/testdata/upload-download/.forgejo/workflows/test.yml index f78ec12..244826f 100644 --- a/testdata/upload-download/.forgejo/workflows/test.yml +++ b/testdata/upload-download/.forgejo/workflows/test.yml @@ -18,7 +18,7 @@ jobs: release-notes: "RELEASE NOTES" verbose: true - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} - id: release-upload + id: release-upload-override uses: SELF@vTest with: direction: upload @@ -26,7 +26,17 @@ jobs: token: FORGEJO_TOKEN release-dir: upload-dir release-notes: "RELEASE NOTES" - override: "true" + override: true + verbose: true + - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} + id: release-upload-v2 + uses: SELF@vTest + with: + direction: upload + tag: v2.0 + token: FORGEJO_TOKEN + release-dir: upload-dir-v2 + release-notes: "RELEASE NOTES V2" verbose: true - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} id: release-download @@ -40,3 +50,17 @@ jobs: - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} run: | diff -u upload-dir download-dir + - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} + id: release-download-latest + uses: SELF@vTest + with: + direction: download + token: FORGEJO_TOKEN + release-dir: download-dir-v2 + download-latest: true + verbose: true + - if: ${{ !startsWith(github.ref, 'refs/tags/v') }} + run: | + diff -u upload-dir-v2 download-dir-v2 + - if: failure() + run: docker logs forgejo diff --git a/testdata/upload-download/upload-dir-v2/file1-v2.txt b/testdata/upload-download/upload-dir-v2/file1-v2.txt new file mode 100644 index 0000000..eb43d98 --- /dev/null +++ b/testdata/upload-download/upload-dir-v2/file1-v2.txt @@ -0,0 +1 @@ +FILE1-V2 diff --git a/testdata/upload-download/upload-dir-v2/file2-v2.txt b/testdata/upload-download/upload-dir-v2/file2-v2.txt new file mode 100644 index 0000000..f9c8d3c --- /dev/null +++ b/testdata/upload-download/upload-dir-v2/file2-v2.txt @@ -0,0 +1 @@ +FILE2-V2