From 8b5c29923f11fc7806f9822f7181bb5d560c8f75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20OLIVIER?= <cedric3.olivier@orange.com>
Date: Sat, 25 Nov 2023 17:48:18 +0000
Subject: [PATCH] fix: switch from bumpversion to bump-my-version

---
 README.md                      | 33 ++++++++++++++++++++++-----------
 templates/gitlab-ci-python.yml | 23 +++++++++++++----------
 2 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/README.md b/README.md
index 0169593..bbbb707 100644
--- a/README.md
+++ b/README.md
@@ -295,7 +295,7 @@ This job is **disabled by default** and allows to perform a complete release of
 The Python template supports two packaging systems:
 
 * [Poetry](https://python-poetry.org/): uses Poetry-specific [version](https://python-poetry.org/docs/cli/#version), [build](https://python-poetry.org/docs/cli/#build) and [publish](https://python-poetry.org/docs/cli/#publish) commands.
-* [Setuptools](https://setuptools.pypa.io/): uses [Bumpversion](https://github.com/peritus/bumpversion) as version management, [build](https://pypa-build.readthedocs.io/) as package builder and [Twine](https://twine.readthedocs.io/) to publish.
+* [Setuptools](https://setuptools.pypa.io/): uses [bump-my-version](https://github.com/callowayproject/bump-my-version) as version management, [build](https://pypa-build.readthedocs.io/) as package builder and [Twine](https://twine.readthedocs.io/) to publish.
 
 The release job is bound to the `publish` stage, appears only on production and integration branches and uses the following variables:
 
@@ -314,19 +314,30 @@ The release job is bound to the `publish` stage, appears only on production and
 
 #### Setuptools tip
 
-If you're using a `setup.cfg` declarative file for your project Setuptools configuration, then you will have to write a
-`.bumpversion.cfg` file to workaround a bug that prevents Bumpversion from updating the project version in your `setup.cfg` file.
+If you're using a  Setuptools configuration, then you will have to write a `.bumpversion.toml` or `pyproject.toml` file.
 
-Example of `.bumpversion.cfg` file:
+Example of `.bumpversion.toml` file:
 
-```ini
-[bumpversion]
-# same version as in your setup.cfg
-current_version = 0.5.0
+```toml
+[tool.bumpversion]
+current_version = "0.0.0"
+```
+
+Example of `pyproject.toml` file:
+
+```toml
+[project]
+name = "project-name"
+
+[build-system]
+requires = ["setuptools"]
+build-backend = "setuptools.build_meta"
+
+[tool.bumpversion]
+current_version = "0.0.0"
 
-[bumpversion:file:setup.cfg]
-# any additional config here
-# see: https://github.com/peritus/bumpversion#file-specific-configuration
+[[tool.bumpversion.files]]
+filename = "project-name/__init__.py"
 ```
 
 #### `semantic-release` integration
diff --git a/templates/gitlab-ci-python.yml b/templates/gitlab-ci-python.yml
index 380732f..f78b392 100644
--- a/templates/gitlab-ci-python.yml
+++ b/templates/gitlab-ci-python.yml
@@ -524,7 +524,7 @@ variables:
       fi
     fi
 
-    # 2: bumpversion (+ Git commit & tag)
+    # 2: bump-my-version (+ Git commit & tag)
     if [[ "$PYTHON_BUILD_SYSTEM" == poetry* ]]
     then
       maybe_install_poetry
@@ -539,34 +539,37 @@ variables:
       py_next_version=$(poetry version --short)
       # Git commit and tag
       git add pyproject.toml
-      # emulate Bumpversion to generate commit message
+      # emulate bump-my-version to generate commit message
       py_commit_message=$(python -c "print('$PYTHON_RELEASE_COMMIT_MESSAGE'.format(current_version='$py_cur_version', new_version='$py_next_version'))")
       git commit -m "$py_commit_message"
       git tag "$py_next_version"
     else
-      # Setuptools / bumpversion
+      # Setuptools / bump-my-version
       # shellcheck disable=SC2086
-      pip install ${PIP_OPTS} bumpversion
+      pip install ${PIP_OPTS} bump-my-version
       if [[ "$py_next_version" ]]
       then
         # explicit release version (semantic-release)
         log_info "[bumpversion] change version \\e[1;94m${py_cur_version}\\e[0m → \\e[1;94m${py_next_version}\\e[0m"
         # create cfg in case it doesn't exist - will be updated by bumpversion
-        touch .bumpversion.cfg
-        bumpversion ${TRACE+--verbose} --current-version "$py_cur_version" --commit ${PYTHON_RELEASE_COMMIT_MESSAGE+--message "$PYTHON_RELEASE_COMMIT_MESSAGE"} --tag --tag-name "{new_version}" "$py_release_part"
+        if [[ ! "$py_cur_version" && ! -f ".bumpversion.cfg" && ! -f ".bumpversion.toml" && ! -f "pyproject.toml" && ! -f "setup.cfg" ]]
+        then
+          log_error "Current version not defined and not version file found, set initial version at least in .bumpversion.toml or pyproject.toml"
+        fi
+        bump-my-version bump ${TRACE+--verbose} --current-version "${py_cur_version:-${PYTHON_RELEASE_START_VERSION:-0.0.0}}" --new-version "$py_next_version" --commit ${PYTHON_RELEASE_COMMIT_MESSAGE+--message "$PYTHON_RELEASE_COMMIT_MESSAGE"} --tag --tag-name "{new_version}" "$py_release_part"
       elif [[ -f ".bumpversion.cfg" ]]
       then
         # current version shall be set in .bumpversion.cfg
         py_release_part="$PYTHON_RELEASE_NEXT"
-        log_info "[bumpversion] increase \\e[1;94m${py_release_part}\\e[0m"
-        bumpversion ${TRACE+--verbose} --commit ${PYTHON_RELEASE_COMMIT_MESSAGE+--message "$PYTHON_RELEASE_COMMIT_MESSAGE"} --tag --tag-name "{new_version}" "$py_release_part"
+        log_info "[bump-my-version bump] increase \\e[1;94m${py_release_part}\\e[0m"
+        bump-my-version bump ${TRACE+--verbose} --commit ${PYTHON_RELEASE_COMMIT_MESSAGE+--message "$PYTHON_RELEASE_COMMIT_MESSAGE"} --tag --tag-name "{new_version}" "$py_release_part"
       elif [[ -f "setup.py" ]]
       then
         # retrieve current version from setup.py
         py_cur_version=$(python setup.py --version)
         py_release_part="$PYTHON_RELEASE_NEXT"
-        log_info "[bumpversion] increase \\e[1;94m${py_release_part}\\e[0m (from current \\e[1;94m${py_cur_version}\\e[0m)"
-        bumpversion ${TRACE+--verbose} --current-version "$py_cur_version" --commit ${PYTHON_RELEASE_COMMIT_MESSAGE+--message "$PYTHON_RELEASE_COMMIT_MESSAGE"} --tag --tag-name "{new_version}" "$py_release_part" setup.py
+        log_info "[bump-my-version] increase \\e[1;94m${py_release_part}\\e[0m (from current \\e[1;94m${py_cur_version}\\e[0m)"
+        bump-my-version bump ${TRACE+--verbose} --current-version "$py_cur_version" --commit ${PYTHON_RELEASE_COMMIT_MESSAGE+--message "$PYTHON_RELEASE_COMMIT_MESSAGE"} --tag --tag-name "{new_version}" "$py_release_part" setup.py
       else
         log_error "--- setup.py or .bumpversion.cfg file required to retrieve current version: cannot perform release"
         exit 1
-- 
GitLab