From ed3ffecb5230f62219a437050e7687dd2c111eb1 Mon Sep 17 00:00:00 2001 From: Patrick Stevens <3138005+Smaug123@users.noreply.github.com> Date: Thu, 30 May 2024 13:32:40 +0100 Subject: [PATCH] Fix and test GitHub release script (#148) --- .github/workflows/dotnet.yaml | 21 ++++++- .github/workflows/tag.sh | 106 +++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/.github/workflows/dotnet.yaml b/.github/workflows/dotnet.yaml index e975e16..594b906 100644 --- a/.github/workflows/dotnet.yaml +++ b/.github/workflows/dotnet.yaml @@ -196,8 +196,27 @@ jobs: # Verify that there is exactly one nupkg in the artifact that would be NuGet published run: if [[ $(find packed-attribute -maxdepth 1 -name 'WoofWare.Myriad.Plugins.Attributes.*.nupkg' -printf c | wc -c) -ne "1" ]]; then exit 1; fi + github-release-plugin-dry-run: + needs: [nuget-pack] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Download NuGet artifact (plugin) + uses: actions/download-artifact@v4 + with: + name: nuget-package-plugin + - name: Download NuGet artifact (attribute) + uses: actions/download-artifact@v4 + with: + name: nuget-package-attribute + - name: Tag and release plugin + env: + DRY_RUN: 1 + GITHUB_TOKEN: mock-token + run: sh .github/workflows/tag.sh + all-required-checks-complete: - needs: [check-dotnet-format, check-nix-format, check-accurate-generations, build, build-nix, linkcheck, flake-check, analyzers, nuget-pack, expected-pack] + needs: [check-dotnet-format, check-nix-format, check-accurate-generations, build, build-nix, linkcheck, flake-check, analyzers, nuget-pack, expected-pack, github-release-plugin-dry-run] runs-on: ubuntu-latest steps: - run: echo "All required checks complete." diff --git a/.github/workflows/tag.sh b/.github/workflows/tag.sh index 3b8d313..b20e15c 100644 --- a/.github/workflows/tag.sh +++ b/.github/workflows/tag.sh @@ -1,6 +1,13 @@ -#!/bin/sh +#!/bin/bash -find . -maxdepth 1 -type f -name '*.nupkg' -exec sh -c 'tag=$(basename "$1" .nupkg); git tag "$tag"; git push origin "$tag"' shell {} \; +echo "Dry-run? $DRY_RUN!" + +find . -maxdepth 1 -type f ! -name "$(printf "*\n*")" -name '*.nupkg' | while IFS= read -r file +do + tag=$(basename "$file" .nupkg) + git tag "$tag" + ${DRY_RUN:+echo} git push origin "$tag" +done export TAG TAG=$(find . -maxdepth 1 -type f -name 'WoofWare.Myriad.Plugins.*.nupkg' -exec sh -c 'basename "$1" .nupkg' shell {} \; | grep -v Attributes) @@ -14,15 +21,100 @@ case "$TAG" in esac # target_commitish empty indicates the repo default branch -if curl --fail-with-body -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/Smaug123/WoofWare.Myriad/releases -d '{"tag_name":"'"$TAG"'","target_commitish":"","name":"'"$TAG"'","draft":false,"prerelease":false,"generate_release_notes":false}' > curl_output.json; then - echo "Curl succeeded." -else - exit_message=$(jq -r --exit-status 'if .errors | length == 1 then .errors[0].code else null' curl_output.json) +curl_body='{"tag_name":"'"$TAG"'","target_commitish":"","name":"'"$TAG"'","draft":false,"prerelease":false,"generate_release_notes":false}' + +echo "cURL body: $curl_body" + +failed_output=$(cat <<'EOF' +{ + "message": "Validation Failed", + "errors": [ + { + "resource": "Release", + "code": "already_exists", + "field": "tag_name" + } + ], + "documentation_url": "https://docs.github.com/rest/releases/releases#create-a-release" +} +EOF +) + +success_output=$(cat <<'EOF' +{ + "url": "https://api.github.com/repos/Smaug123/WoofWare.Myriad/releases/158152116", + "assets_url": "https://api.github.com/repos/Smaug123/WoofWare.Myriad/releases/158152116/assets", + "upload_url": "https://uploads.github.com/repos/Smaug123/WoofWare.Myriad/releases/158152116/assets{?name,label}", + "html_url": "https://github.com/Smaug123/WoofWare.Myriad/releases/tag/WoofWare.Myriad.Plugins.2.1.30", + "id": 158152116, + "author": { + "login": "github-actions[bot]", + "id": 41898282, + "node_id": "MDM6Qm90NDE4OTgyODI=", + "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/github-actions%5Bbot%5D", + "html_url": "https://github.com/apps/github-actions", + "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers", + "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}", + "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}", + "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions", + "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs", + "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos", + "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}", + "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events", + "type": "Bot", + "site_admin": false + }, + "node_id": "RE_kwDOJfksgc4JbTW0", + "tag_name": "WoofWare.Myriad.Plugins.2.1.30", + "target_commitish": "main", + "name": "WoofWare.Myriad.Plugins.2.1.30", + "draft": false, + "prerelease": false, + "created_at": "2024-05-30T11:00:55Z", + "published_at": "2024-05-30T11:03:02Z", + "assets": [ + + ], + "tarball_url": "https://api.github.com/repos/Smaug123/WoofWare.Myriad/tarball/WoofWare.Myriad.Plugins.2.1.30", + "zipball_url": "https://api.github.com/repos/Smaug123/WoofWare.Myriad/zipball/WoofWare.Myriad.Plugins.2.1.30", + "body": null +} +EOF +) + +HANDLE_OUTPUT='' +handle_error() { + ERROR_OUTPUT="$1" + exit_message=$(echo "$ERROR_OUTPUT" | jq -r --exit-status 'if .errors | length == 1 then .errors[0].code else null end') if [ "$exit_message" = "already_exists" ] ; then - echo "Did not create GitHub release because it already exists at this version." + HANDLE_OUTPUT="Did not create GitHub release because it already exists at this version." else echo "Unexpected error output from curl: $(cat curl_output.json)" echo "JQ output: $(exit_message)" exit 2 fi +} + +run_tests() { + handle_error "$failed_output" + if [ "$HANDLE_OUTPUT" != "Did not create GitHub release because it already exists at this version." ]; then + echo "Bad output from handler: $HANDLE_OUTPUT" + exit 3 + fi + HANDLE_OUTPUT='' + echo "Tests passed." +} + +run_tests + +if [ "$DRY_RUN" != 1 ] ; then + if curl --fail-with-body -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/Smaug123/WoofWare.Myriad/releases -d "$curl_body" > curl_output.json; then + echo "Curl succeeded." + else + handle_error "$(cat curl_output.json)" + echo "$HANDLE_OUTPUT" + fi fi