From eb276720b99bff8dbd0b96c314fb10920914f392 Mon Sep 17 00:00:00 2001
From: Zachary Brown <z.brown@chia.net>
Date: Mon, 16 Dec 2024 14:24:56 -0800
Subject: [PATCH 1/5] ci: release candidate logic

---
 .github/workflows/auto-release-rc.yml   | 80 ++++++++++++++++++++++
 .github/workflows/auto-release.yml      |  4 +-
 .github/workflows/build-installers.yaml | 90 +++++++++++++++++++++----
 3 files changed, 158 insertions(+), 16 deletions(-)
 create mode 100644 .github/workflows/auto-release-rc.yml

diff --git a/.github/workflows/auto-release-rc.yml b/.github/workflows/auto-release-rc.yml
new file mode 100644
index 0000000..9551343
--- /dev/null
+++ b/.github/workflows/auto-release-rc.yml
@@ -0,0 +1,80 @@
+# Compares the version in package.json to tags on the repo. If the tag doesn't exist, a new tag is created, which
+# then triggers the normal "on tag" release automation in the build job
+name: Auto Tag RC
+
+on:
+  push:
+    branches:
+      - develop
+      - dev-releases
+
+concurrency:
+  group: rc-release-check
+
+jobs:
+  release-dev:
+    name: Release rc version
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout current branch
+        uses: actions/checkout@v4
+        with:
+          # Need REPO_COMMIT token so when the tag is created, the tag automation runs
+          token: ${{ secrets.REPO_COMMIT }}
+          fetch-depth: 0
+
+      - name: Setup commit signing for ChiaAutomation
+        uses: Chia-Network/actions/commit-sign/gpg@main
+        with:
+          gpg_private_key: ${{ secrets.CHIA_AUTOMATION_PRIVATE_GPG_KEY }}
+          passphrase: ${{ secrets.CHIA_AUTOMATION_PRIVATE_GPG_PASSPHRASE }}
+
+      - name: Check for current version tag. Create if it doesn't exist
+        env:
+          GH_TOKEN: ${{ github.token }}
+        run: |
+          stable_version=$(gh release list --limit 1 --order desc --exclude-pre-releases --json tagName --jq ".[].tagName")
+          echo "Latest release is $stable_version"
+          rc_version=$(gh release list --json tagName --jq ".[] | select(.tagName | test(\"${version}-rc*\")) | .tagName")
+          echo "Latest release candidate is $rc_version"
+
+          if [[ -z ${rc_version} ]]; then
+            # Extract the major, minor, and patch versions
+            IFS='.' read -r major minor patch <<< "$stable_version"
+
+            # Increment the patch version
+            new_patch=$((patch + 1))
+
+            # Construct the new version string
+            version="$major.$minor.$new_patch-rc1"
+
+            echo "New version: $version"
+
+          else
+            # Extract the major, minor, patch, and rc parts
+            IFS='.-' read -r major minor patch rc <<< "$rc_version"
+
+            # Extract just the number of the rc
+            rc_number="${rc#rc}"
+
+            # Increment the rc number
+            rc_number=$((rc_number +1))
+
+            # Construct the new version string
+            version="$major.$minor.$patch-rc$rc_number"
+
+            echo "New version: $version"
+
+          fi
+
+          if [ $(git tag -l "$version") ]; then
+            echo "$version tag exists, deleting..."
+            git tag -d $version
+            git push --delete origin $version
+          fi
+          echo "Tag does not exist. Creating and pushing tag"
+          rm -f CHANGELOG.md
+          npx conventional-changelog-cli -p angular -i CHANGELOG.md -s -r 0
+          changes=$(npx conventional-changelog-cli -r 1 | tail -n +2)
+          git tag $version -m "Release $version  $changes"
+          git push origin $version
diff --git a/.github/workflows/auto-release.yml b/.github/workflows/auto-release.yml
index 445a4dc..e72132f 100644
--- a/.github/workflows/auto-release.yml
+++ b/.github/workflows/auto-release.yml
@@ -21,8 +21,8 @@ jobs:
       - name: Checkout current branch
         uses: actions/checkout@v4
         with:
-          # Need PACKAGE_ADMIN_PAT token so when the tag is created, the tag automation runs
-          token: ${{ secrets.PACKAGE_ADMIN_PAT }}
+          # Need REPO_COMMIT token so when the tag is created, the tag automation runs
+          token: ${{ secrets.REPO_COMMIT }}
           fetch-depth: 0
 
       - name: Setup commit signing for ChiaAutomation
diff --git a/.github/workflows/build-installers.yaml b/.github/workflows/build-installers.yaml
index 3deef96..b49c854 100644
--- a/.github/workflows/build-installers.yaml
+++ b/.github/workflows/build-installers.yaml
@@ -4,15 +4,12 @@ on:
   push:
     tags:
       - '**'
-    branches:
-      - refactor/refactor-base #remove this once rebuild is merged
   pull_request:
     branches:
       - '**'
 
 concurrency:
-  # SHA is added to the end if on `main` to let all main workflows run
-  group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ github.ref == 'refs/heads/main' && github.sha || '' }}
+  group: ${{ github.ref }}-${{ github.workflow }}
   cancel-in-progress: true
 
 permissions:
@@ -35,7 +32,17 @@ jobs:
       - name: Setup Node 20
         uses: actions/setup-node@v4
         with:
-          node-version: '20.10'
+          node-version: '20.16'
+
+      - name: Change the package.json version if an RC tag
+        if: startsWith(github.ref, 'refs/tags/') && contains( github.ref, '-rc')
+        run: |
+          echo "Github ref: $GITHUB_REF"
+          IFS='/' read -r base directory tag <<< "$GITHUB_REF"
+          echo "Extracted tag is $tag"
+
+          jq ".version = \"${tag}\"" package.json > package.tmp
+          mv package.tmp package.json
 
       - name: Install Husky
         run: npm install --save-dev husky
@@ -59,7 +66,7 @@ jobs:
           SIGNING_SECRET: "${{ secrets.SM_CLIENT_CERT_FILE_B64 }}"
 
       - name: Import Apple installer signing certificate
-        if: steps.check_secrets.outputs.HAS_SIGNING_SECRET
+        if: steps.check_secrets.outputs.HAS_SIGNING_SECRET && startsWith(github.ref, 'refs/tags/')
         uses: Apple-Actions/import-codesign-certs@v3
         with:
           p12-file-base64: ${{ secrets.APPLE_DEV_ID_APP }}
@@ -71,7 +78,7 @@ jobs:
         run: npm run electron:package:mac
 
       - name: Notarize
-        if: steps.check_secrets.outputs.HAS_SIGNING_SECRET
+        if: steps.check_secrets.outputs.HAS_SIGNING_SECRET && startsWith(github.ref, 'refs/tags/')
         run: |
           DMG_FILE=$(find ${{ github.workspace }}/dist/ -type f -name '*.dmg')
           xcrun notarytool submit \
@@ -94,10 +101,19 @@ jobs:
       - name: Checkout Code
         uses: actions/checkout@v4
 
-      - name: Setup Node 20.10
+      - name: Setup Node 20.16
         uses: actions/setup-node@v4
         with:
-          node-version: '20.10'
+          node-version: '20.16'
+
+      - name: Change the package.json version if an RC tag
+        if: startsWith(github.ref, 'refs/tags/') && contains( github.ref, '-rc')
+        shell: bash
+        run: |
+          IFS='/' read -r base directory tag <<< "$GITHUB_REF"
+
+          jq ".version = \"${tag}\"" package.json > package.tmp
+          mv package.tmp package.json
 
       - name: Install Husky
         run: npm install --save-dev husky
@@ -155,10 +171,19 @@ jobs:
       - name: Checkout Code
         uses: actions/checkout@v4
 
-      - name: Setup Node 20.10
+      - name: Setup Node 20.16
         uses: actions/setup-node@v4
         with:
-          node-version: '20.10'
+          node-version: '20.16'
+
+      - name: Change the package.json version if an RC tag
+        if: startsWith(github.ref, 'refs/tags/') && contains( github.ref, '-rc')
+        shell: bash
+        run: |
+          IFS='/' read -r base directory tag <<< "$GITHUB_REF"
+
+          jq ".version = \"${tag}\"" package.json > package.tmp
+          mv package.tmp package.json
 
       - name: Install Husky
         run: npm install --save-dev husky
@@ -190,10 +215,19 @@ jobs:
       - name: Checkout Code
         uses: actions/checkout@v4
 
-      - name: Setup Node 20.10
+      - name: Setup Node 20.16
         uses: actions/setup-node@v4
         with:
-          node-version: '20.10'
+          node-version: '20.16'
+
+      - name: Change the package.json version if an RC tag
+        if: startsWith(github.ref, 'refs/tags/') && contains( github.ref, '-rc')
+        shell: bash
+        run: |
+          IFS='/' read -r base directory tag <<< "$GITHUB_REF"
+
+          jq ".version = \"${tag}\"" package.json > package.tmp
+          mv package.tmp package.json
 
       - name: Install Husky
         run: npm install --save-dev husky
@@ -258,9 +292,33 @@ jobs:
           echo "EXE_FILE=$EXE_FILE" >>$GITHUB_ENV
           echo "WEB_FILE=$WEB_FILE" >>$GITHUB_ENV
 
+      # RC release should not be set as latest
+      - name: Decide if release should be set as latest
+        id: is_latest
+        shell: bash
+        run: |
+          unset IS_LATEST
+
+          echo "Github ref is $GITHUB_REF"
+
+          if [[ "$GITHUB_REF" =~ "-rc" ]]; then
+            echo "release candidate tag matched"
+            IS_LATEST='false'
+            IS_PRERELEASE='true'
+          else
+            echo "main branch release matched"
+            IS_LATEST='true'
+            IS_PRERELEASE='false'
+          fi
+
+          echo "IS_LATEST=${IS_LATEST}" >> "$GITHUB_OUTPUT"
+          echo "IS_PRERELEASE=${IS_PRERELEASE}" >> "$GITHUB_OUTPUT"
+
       - name: Release
-        uses: softprops/action-gh-release@v2.1.0
+        uses: softprops/action-gh-release@v2
         with:
+          prerelease: ${{steps.is_latest.outputs.IS_PRERELEASE}}
+          make_latest: "${{steps.is_latest.outputs.IS_LATEST}}"
           files: |
             ${{ env.DMG_FILE }}
             ${{ env.DEB_FILE }}
@@ -268,19 +326,23 @@ jobs:
             ${{ env.WEB_FILE }}
 
       - name: Get repo name
+        if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')
         id: repo-name
         run: |
           echo "REPO_NAME=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 2)" >>$GITHUB_OUTPUT
 
       - name: Get tag name
+        if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')
         id: tag-name
         run: |
           echo "TAGNAME=$(echo $GITHUB_REF | cut -d / -f 3)" >>$GITHUB_OUTPUT
 
       - name: Gets JWT Token from GitHub
+        if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')
         uses: Chia-Network/actions/github/jwt@main
 
       - name: Trigger apt repo update
+        if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')
         uses: Chia-Network/actions/github/glue@main
         with:
           json_data: '{"climate_tokenization_repo":"${{ steps.repo-name.outputs.REPO_NAME }}","application_name":"[\"${{ env.APP_NAME }}\"]","release_version":"${{ steps.tag-name.outputs.TAGNAME }}","add_debian_version":"true","arm64":"available"}'

From 692aaebd03b8e51de7e8c3d4b8e9018e97cc908e Mon Sep 17 00:00:00 2001
From: Zachary Brown <z.brown@chia.net>
Date: Mon, 16 Dec 2024 14:27:52 -0800
Subject: [PATCH 2/5] ci: allow tagging from testing branch

---
 .github/workflows/auto-release-rc.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/auto-release-rc.yml b/.github/workflows/auto-release-rc.yml
index 9551343..9ef7835 100644
--- a/.github/workflows/auto-release-rc.yml
+++ b/.github/workflows/auto-release-rc.yml
@@ -6,7 +6,7 @@ on:
   push:
     branches:
       - develop
-      - dev-releases
+      - rc-release-ci
 
 concurrency:
   group: rc-release-check

From 50d96c5e453f9eae2b90e45b453072bb14a6cca4 Mon Sep 17 00:00:00 2001
From: Zachary Brown <z.brown@chia.net>
Date: Mon, 16 Dec 2024 14:38:55 -0800
Subject: [PATCH 3/5] ci: get rid of steps that are now obsolete

---
 .github/workflows/build-installers.yaml | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/.github/workflows/build-installers.yaml b/.github/workflows/build-installers.yaml
index b49c854..ed9365c 100644
--- a/.github/workflows/build-installers.yaml
+++ b/.github/workflows/build-installers.yaml
@@ -24,8 +24,6 @@ jobs:
     name: Build Mac Installer
     runs-on: macos-latest
     steps:
-      - uses: Chia-Network/actions/clean-workspace@main
-
       - name: Checkout Code
         uses: actions/checkout@v4
 
@@ -335,11 +333,6 @@ jobs:
         if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')
         id: tag-name
         run: |
-          echo "TAGNAME=$(echo $GITHUB_REF | cut -d / -f 3)" >>$GITHUB_OUTPUT
-
-      - name: Gets JWT Token from GitHub
-        if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')
-        uses: Chia-Network/actions/github/jwt@main
 
       - name: Trigger apt repo update
         if: startsWith(github.ref, 'refs/tags/') && !contains( github.ref, '-rc')

From d05364ef7d6bcfa6b5f9ca6c17f1f5b718422b44 Mon Sep 17 00:00:00 2001
From: Zachary Brown <z.brown@chia.net>
Date: Mon, 16 Dec 2024 14:39:30 -0800
Subject: [PATCH 4/5] ci: run only for develop branch

---
 .github/workflows/auto-release-rc.yml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/.github/workflows/auto-release-rc.yml b/.github/workflows/auto-release-rc.yml
index 9ef7835..274659b 100644
--- a/.github/workflows/auto-release-rc.yml
+++ b/.github/workflows/auto-release-rc.yml
@@ -6,7 +6,6 @@ on:
   push:
     branches:
       - develop
-      - rc-release-ci
 
 concurrency:
   group: rc-release-check

From 02589d35971cf336af6609fc643a443c7efe770a Mon Sep 17 00:00:00 2001
From: Zachary Brown <z.brown@chia.net>
Date: Mon, 16 Dec 2024 14:42:36 -0800
Subject: [PATCH 5/5] ci: version bump

---
 package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/package.json b/package.json
index 52affce..6ac455e 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
   "name": "climate-tokenization-engine-ui",
   "private": true,
-  "version": "1.2.3",
+  "version": "1.2.4",
   "type": "module",
   "main": "build/main.js",
   "engineStrict": true,