Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
feat: Support bump-my-version alongside bump2version. (#93)
Browse files Browse the repository at this point in the history
bump-my-version supports pyproject.toml and has active development,
bump2version has no releases in 4 years.

Refs: #90
  • Loading branch information
EdgyEdgemond authored Mar 2, 2024
1 parent 56db9f5 commit 7916f6a
Show file tree
Hide file tree
Showing 11 changed files with 602 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: |
python -m pip install --upgrade pip poetry
poetry config virtualenvs.create false
poetry install
poetry install --extras=bump-my-version
- name: Lint with ruff
run: |
pre-commit run --all-files
23 changes: 21 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
python -m pip install --upgrade pip poetry
poetry config virtualenvs.create false
poetry install
poetry install --extras=bump-my-version
- name: Generate coverage report
run: |
pytest --cov=changelog_gen --cov-report=xml
Expand All @@ -40,6 +40,25 @@ jobs:
yml: ./codecov.yml
fail_ci_if_error: false

test-legacy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Set up Python 3.9
uses: actions/setup-python@v1
with:
python-version: 3.9.18
- name: Install dependencies
run: |
python -m pip install --upgrade pip poetry
poetry config virtualenvs.create false
poetry install --extras=bump2version
- name: Generate coverage report
run: |
pytest -m "backwards_compat"
test-python-versions:

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -68,7 +87,7 @@ jobs:
run: |
python -m pip install --upgrade pip poetry
poetry config virtualenvs.create false
poetry install
poetry install --extras=bump-my-version
- name: Test with pytest
run: |
pytest
4 changes: 1 addition & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ repos:
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude: setup.cfg
- id: check-added-large-files
- id: check-ast
- id: check-json
Expand Down Expand Up @@ -31,5 +30,4 @@ repos:
hooks:
- id: remove-crlf
- id: remove-tabs
exclude: setup.cfg
exclude: setup.cfg|Makefile
exclude: Makefile
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
[![codecov](https://codecov.io/gh/EdgyEdgemond/changelog-gen/branch/master/graph/badge.svg)](https://codecov.io/gh/EdgyEdgemond/changelog-gen)

`changelog-gen` is a CHANGELOG generator intended to be used in conjunction
with [bumpversion](https://github.com/c4urself/bump2version) to generate
with [bump-my-version](https://github.com/callowayproject/bump-my-version) to generate
changelogs and create release tags.

## Installation

```bash
pip install changelog-gen
pip install changelog-gen[bump-my-version] # recommended

pip install changelog-gen[bump2version] # bump2version support will be dropped in the future
```

or clone this repo and install with poetry.

```bash
poetry install
poetry install --extras=bump-my-version
```

## Usage
Expand Down Expand Up @@ -426,6 +428,16 @@ headers."content-type" = "application/json"
Also partially available as `--post-process-url` and `--post-process-auth-env` (e.g. `changelog generate --post-process-url 'http://my-api-url.domain/comment/::issue_ref::' --post-process-auth-env MY_API_AUTH`)
### Pre-release flows
If your versioning uses prerelease version parts, after a major/minor/patch update creates e.g. `v0.0.1rc0`, use
`--version-part=<part>` to trigger release flows, based on your configuration.
```bash
$ changelog generate --version-part build
... v0.0.1rc1
```
## Contributing
This project uses pre-commit hooks, please run `pre-commit install` after
Expand Down
70 changes: 65 additions & 5 deletions changelog_gen/version.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
import re
import subprocess
from typing import TypeVar
from warnings import warn

try:
from bumpversion import bump # noqa: F401
except ImportError:
bump_library = "bump2version"
warn(
"bump2version deprecated, recommend installing extras[bump-my-version].",
DeprecationWarning,
stacklevel=2,
)
else:
bump_library = "bump-my-version"

from changelog_gen import errors

T = TypeVar("T", bound="BumpVersion")


def parse_bump_my_version_info(semver: str, lines: list[str]) -> tuple[str, str]:
"""Parse output from bump-my-version info command."""
reg = re.compile(rf".*({semver}) [-]+ (.*)")

current = lines[0].split(" -- ")[0].strip()
for line in lines:
m = reg.match(line)
if m:
new = m[2].strip()

return current, new


def parse_bump2version_info(_semver: str, lines: list[str]) -> tuple[str, str]:
"""Parse output from bump2version info command."""
bumpversion_data = {v.split("=")[0].strip(): v.split("=")[1].strip() for v in lines if "_version" in v}

return bumpversion_data["current_version"], bumpversion_data["new_version"]


commands = {
"bump-my-version": {
"get_version_info": ["bump-my-version", "show-bump", "--ascii"],
"release": ["bump-my-version", "bump", "patch", "--new-version", "VERSION"],
"parser": parse_bump_my_version_info,
},
"bump2version": {
"get_version_info": ["bumpversion", "SEMVER", "--dry-run", "--list", "--allow-dirty"],
"release": ["bumpversion", "patch", "--new-version", "VERSION"],
"parser": parse_bump2version_info,
},
}


class BumpVersion: # noqa: D101
@classmethod
def _version_info_cmd(cls: type[T], semver: str) -> list[str]:
command = commands[bump_library]["get_version_info"]
return [c.replace("SEMVER", semver) for c in command]

@classmethod
def _release_cmd(cls: type[T], version: str) -> list[str]:
command = commands[bump_library]["release"]
return [c.replace("VERSION", version) for c in command]

@classmethod
def get_version_info(cls: type[T], semver: str) -> dict[str, str]:
"""Get version info for a semver release."""
try:
describe_out = (
subprocess.check_output(
["bumpversion", semver, "--dry-run", "--list", "--allow-dirty"], # noqa: S603, S607
cls._version_info_cmd(semver), # noqa: S603
stderr=subprocess.STDOUT,
)
.decode()
Expand All @@ -24,14 +82,16 @@ def get_version_info(cls: type[T], semver: str) -> dict[str, str]:
msg = "Unable to get version data from bumpversion."
raise errors.VersionDetectionError(msg) from e

bumpversion_data = {v.split("=")[0].strip(): v.split("=")[1].strip() for v in describe_out if "_version" in v}
current, new = commands[bump_library]["parser"](semver, describe_out)

return {
"current": bumpversion_data["current_version"],
"new": bumpversion_data["new_version"],
"current": current,
"new": new,
}

@classmethod
def release(cls: type[T], version: str) -> None:
"""Generate new release."""
subprocess.check_output(["bumpversion", "--new-version", version, "patch"]) # noqa: S603, S607
subprocess.check_output(
cls._release_cmd(version), # noqa: S603
)
Loading

0 comments on commit 7916f6a

Please sign in to comment.