From 7930ffd561bc95a24b5370b6aeda5711057bd9b1 Mon Sep 17 00:00:00 2001
From: Oliver Cullimore <hello@olivercullimore.co.uk>
Date: Wed, 27 Sep 2023 18:40:30 +0100
Subject: [PATCH] ci: Migrate to commitizen to bump version

---
 .github/workflows/bump.yml                    | 33 +++++++++++++++
 .github/workflows/release.yml                 | 40 ++++---------------
 .../uk_bin_collection/manifest.json           |  4 +-
 pyproject.toml                                | 23 ++++-------
 4 files changed, 50 insertions(+), 50 deletions(-)
 create mode 100644 .github/workflows/bump.yml

diff --git a/.github/workflows/bump.yml b/.github/workflows/bump.yml
new file mode 100644
index 0000000000..3803de095a
--- /dev/null
+++ b/.github/workflows/bump.yml
@@ -0,0 +1,33 @@
+name: Bump Version
+
+on:
+  push:
+    # Trigger unless only the wiki directory changed
+    paths-ignore:
+      - "wiki/**"
+      - "**/**.md"
+    branches: [ "master" ]
+
+jobs:
+  bump:
+    if: "!startsWith(github.event.head_commit.message, 'bump:')"
+    runs-on: ubuntu-latest
+    environment: bump
+    concurrency: bump
+    permissions:
+      id-token: write
+      contents: write
+    steps:
+      - uses: actions/checkout@v4
+        with:
+          token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}"
+          fetch-depth: 0
+      - uses: actions/setup-python@v4
+        with:
+          python-version: '3.10'
+
+      - name: Bump version
+        id: cz
+        uses: commitizen-tools/commitizen-action@master
+        with:
+          github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index dff474f412..fd5663c6a8 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -2,23 +2,18 @@ name: Publish Release
 
 on:
   push:
-    # Trigger only when uk_bin_collection/uk_bin_collection directory changes
-    paths:
-      - "uk_bin_collection/uk_bin_collection/**"
-    branches: [ "master" ]
+    tags:
+      - '*'
 
 jobs:
   release:
     runs-on: ubuntu-latest
     environment: release
-    concurrency: release
     permissions:
       id-token: write
       contents: write
     steps:
       - uses: actions/checkout@v4
-        with:
-          fetch-depth: 0
       - uses: actions/setup-python@v4
         with:
           python-version: '3.10'
@@ -36,36 +31,17 @@ jobs:
       - name: Install
         run: make install
 
-      - name: Python Semantic Release
-        id: release
-        uses: python-semantic-release/python-semantic-release@v8.1.1
-        with:
-          github_token: ${{ secrets.GITHUB_TOKEN }} # May need to replace with a Personal Access Token if it fails
+      - name: Set release version
+        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
 
-      - uses: ncipollo/release-action@v1
+      - name: Create release
+        uses: ncipollo/release-action@v1
         with:
-          tag: ${{ steps.release.outputs.tag }}
-          allowUpdates: true
+          tag: ${{ env.RELEASE_VERSION }}
           generateReleaseNotes: true
-
-      - name: Update manifest.json version
-        if: steps.release.outputs.released == 'true'
-        uses: jossef/action-set-json-field@v2.1
-        with:
-          file: custom_components/uk_bin_collection/manifest.json
-          field: version
-          value: ${{ steps.release.outputs.tag }}
-
-      - name: Update manifest.json requirements
-        if: steps.release.outputs.released == 'true'
-        uses: jossef/action-set-json-field@v2.1
-        with:
-          file: custom_components/uk_bin_collection/manifest.json
-          field: requirements[0]
-          value: uk-bin-collection>=${{ steps.release.outputs.tag }}
+          prerelease: true
 
       - name: Build and publish package
-        if: steps.release.outputs.released == 'true'
         run: |
           poetry config pypi-token.pypi "${{ secrets.PYPI_API_KEY }}"
           poetry publish --build
diff --git a/custom_components/uk_bin_collection/manifest.json b/custom_components/uk_bin_collection/manifest.json
index 80dbef5c14..e35ee0bd11 100644
--- a/custom_components/uk_bin_collection/manifest.json
+++ b/custom_components/uk_bin_collection/manifest.json
@@ -3,12 +3,12 @@
     "name": "UK Bin Collection Data",
     "documentation": "https://github.com/robbrad/UKBinCollectionData/wiki",
     "issue_tracker": "https://github.com/robbrad/UKBinCollectionData/issues",
-    "requirements": ["uk-bin-collection"],
+    "requirements": ["uk-bin-collection>=0.10.0"],
     "codeowners": ["@robbrad"],
     "config_flow": true,
     "zeroconf": [],
     "homeassistant": "2021.1.0",
-    "version": "0.1.0",
+    "version": "0.10.0",
     "after_dependencies": [],
     "dependencies": [],
     "iot_class": "cloud_polling",
diff --git a/pyproject.toml b/pyproject.toml
index 675ef6b812..69d32fee2e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -55,21 +55,12 @@ requests = "*"
 selenium = "*"
 lxml = "*"
 
-[tool.semantic_release]
-version_toml = ["pyproject.toml:tool.poetry.version"]
-branch = "master"
-commit_message = "Release version {version}"
-prerelease = true
-major_on_zero = true
-
-[tool.semantic_release.commit_parser_options]
-allowed_tags = [
-    "ci",
-    "docs",
-    "feat",
-    "fix",
-    "refactor"
+[tool.commitizen]
+major_version_zero = true
+version_provider = "poetry"
+version_scheme = "semver"
+version_files = [
+    "custom_components/uk_bin_collection/manifest.json:version",
+    "custom_components/uk_bin_collection/manifest.json:requirements"
 ]
-minor_tags = ["feat"]
-patch_tags = ["fix"]