Skip to content

Commit

Permalink
Merge pull request #43 from wemogy/40-introduce-only-replace-suffix-w…
Browse files Browse the repository at this point in the history
…ith-parameter

Add `only-replace-suffix-with` parameter
  • Loading branch information
Bennet Ranft authored Mar 22, 2024
2 parents 4a4d253 + 21632e8 commit ad6f0f5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 39 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ for [Conventional Commits](https://www.conventionalcommits.org/) with support fo
id: get-release-version
with:
prefix: "v"
suffix: "hotfix"
only-increase-suffix: "false"
suffix: "beta"
previous-version-suffix: "dev"
bumping-suffix: "hotfix"
only-bump-suffix: "true"
create-tag: "true"

- run: echo ${{ steps.get-release-version.outputs.version }}
Expand All @@ -28,12 +30,14 @@ for [Conventional Commits](https://www.conventionalcommits.org/) with support fo
## Inputs
| Input | Required | Default | Description |
| ---------------------- | -------- | -------- | ----------------------------------------------------------------------- |
| `prefix` | `false` | `v` | The prefix that should be prepended to the version. |
| `suffix` | `false` | `hotfix` | The suffix that should be incremented / appended to the version. |
| `only-increase-suffix` | `false` | `false` | Increment the suffix if any changes got detected. |
| `create-tag` | `false` | `true` | Create a Git Tag for the version and push it if a remote is configured. |
| Input | Required | Default | Description |
|---------------------------|----------|----------|----------------------------------------------------------------------------------------------------------|
| `prefix` | `false` | `v` | The prefix that should be prepended to the version. |
| `suffix` | `false` | `` | The suffix that should appended to the version (e.g. `beta`). |
| `previous-version-suffix` | `false` | `` | The suffix that should be replaced with the value in `suffix` (e.g. `dev`). |
| `bumping-suffix` | `false` | `hotfix` | The suffix to append to the version (or increment if it already exists) if `only-bump-suffix` is `true`. |
| `only-bump-suffix` | `false` | `false` | Bump the `bumping-suffix` instead of the version if changes were detected. |
| `create-tag` | `false` | `true` | Create a git tag for the version and push it if a remote is configured. |

## Outputs

Expand Down
34 changes: 25 additions & 9 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ name: "Get release version"
description: "A GitHub Action to determine the next version by checking the commit history for Conventional Commits"
inputs:
prefix:
description: "The prefix that should be prepended to the version"
description: "The prefix that should be prepended to the version."
required: false
default: "v"
suffix:
description: "The suffix that should be incremented / appended to the version"
description: "The suffix that should appended to the version."
required: false
default: ""
previous-version-suffix:
description: "The suffix that should be replaced with the value in `suffix`."
required: false
default: ""
bumping-suffix:
description: "The suffix to append to the version (or increment if it already exists) if `only-bump-suffix` is `true`."
required: false
default: "hotfix"
only-increase-suffix:
description: "Increment the suffix if any changes got detected"
only-bump-suffix:
description: "Bump the `bumping-suffix` instead of the version if changes were detected."
required: false
default: "false"
create-tag:
description: "Create a Git Tag for the version and push it if a remote is configured"
description: "Create a Git Tag for the version and push it if a remote is configured."
required: false
default: "true"
outputs:
Expand All @@ -33,11 +41,19 @@ runs:
using: "docker"
image: "Dockerfile"
args:
- --only-increase-suffix
- ${{ inputs.only-increase-suffix }}
- --prefix
- ${{ inputs.prefix }}
- --suffix
- ${{ inputs.suffix }}
- --only-increase-suffix
- ${{ inputs.only-increase-suffix }}
- --only-replace-suffix-with
- ${{ inputs.only-replace-suffix-with }}
- --create-tag
- ${{ inputs.create-tag }}
- --prefix
- ${{ inputs.prefix }}
- --previous-version-suffix
- ${{ inputs.previous-version-suffix }}
- --bumping-suffix
- ${{ inputs.bumping-suffix }}
- --only-bump-suffix
- ${{ inputs.only-bump-suffix }}
94 changes: 72 additions & 22 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@
from typing import Any

import git
import semver
import yaml
from semantic_release import LevelBump, ParseError
from semantic_release.commit_parser import AngularCommitParser, AngularParserOptions
from semver import Version

logger = logging.getLogger('wemogy.get-release-version-action')


def parse_bool(string: str) -> bool:
"""Parse a string into a boolean."""
return string.lower() == 'true'


def nullable_string(string: str) -> str | None:
"""Return None if the string is empty or only contains whitespace characters."""
if string.strip() == '':
return None
return string


def print_github_actions_output() -> None:
"""Print the contents of the GITHUB_OUTPUT file."""
file_path = os.getenv('GITHUB_OUTPUT')
Expand All @@ -32,7 +44,7 @@ def print_github_actions_output() -> None:
with open(file_path, 'r') as fh:
content = fh.read()
logger.debug('Content of GITHUB_OUTPUT file "%s":\n%s', file_path, content)
except Exception as exc:
except Exception:
# Catching every exception since this function is not necessary for the script to run
logger.warning('An exception was ignored while trying to get contents of GITHUB_OUTPUT file', exc_info=True)

Expand Down Expand Up @@ -105,15 +117,15 @@ def run_command(*command: str | bytes | os.PathLike[str] | os.PathLike[bytes]) -
return process.stdout


def get_current_version_tag(repo: git.Repo, prefix: str) -> git.TagReference | None:
def get_current_version_tag(repo: git.Repo, prefix: str, suffix: str) -> git.TagReference | None:
"""
Get the current version (= the latest git tag).
If there are no tags, return None.
"""
# Reverse the list of tags to start with the most recent one
for tag in sorted(repo.tags, key=lambda t: t.commit.committed_datetime, reverse=True):
# Check if the tag name starts with the specified prefix
if tag.name.startswith(prefix):
if tag.name.startswith(prefix) and suffix in tag.name:
logger.debug('Found tag %s (%s)', tag.name, tag.commit.hexsha)
return tag

Expand Down Expand Up @@ -189,7 +201,7 @@ def get_next_version(repo: git.Repo, current_version_tag: git.TagReference | Non
logger.debug('Version to bump is %s (0 = chore / unknown, 1 = patch, 2 = minor, 3 = major)', version_to_bump)

# 4. Bump the version
current_version_obj = semver.Version.parse(current_version)
current_version_obj = Version.parse(current_version)

if version_to_bump == 1:
return str(current_version_obj.bump_patch()), True
Expand All @@ -203,7 +215,7 @@ def get_next_version(repo: git.Repo, current_version_tag: git.TagReference | Non

def increment_suffix(version: str, suffix: str) -> str:
"""Increment the version suffix (-{suffix}.) or append -{suffix}.1, if the suffix does not exist."""
return str(semver.Version.parse(version).bump_prerelease(suffix))
return str(Version.parse(version).bump_prerelease(suffix))


def create_tag(version: str) -> None:
Expand All @@ -224,19 +236,26 @@ def create_tag(version: str) -> None:
logger.info('Pushed tag %s to remote', version)


def get_new_version(prefix: str, suffix: str, only_increase_suffix: bool) -> tuple[str, str, bool]:
def get_new_version(
prefix: str,
previous_version_suffix: str | None,
bumping_suffix: str,
only_bump_suffix: bool
) -> tuple[str, str, bool]:
"""
Get the new version, involving the only_increase_suffix flag.
:returns: A tuple of the previous version, the next version and if any changes were detected.
"""
repo = git.Repo(os.getcwd())
current_version_tag = get_current_version_tag(repo, prefix)
current_version_tag = get_current_version_tag(repo, prefix, previous_version_suffix)

if current_version_tag is None:
current_version = '0.0.0'
else:
current_version = current_version_tag.name.removeprefix(prefix)
if previous_version_suffix is not None:
current_version = current_version.replace(f'-{previous_version_suffix}', '', 1)

next_version, has_changes = get_next_version(repo, current_version_tag, current_version)

Expand All @@ -251,9 +270,9 @@ def get_new_version(prefix: str, suffix: str, only_increase_suffix: bool) -> tup
return current_version, next_version, has_changes

# Example case: Hotfix
if only_increase_suffix:
if only_bump_suffix:
logger.info('Only the suffix will be incremented.')
return current_version, increment_suffix(current_version, suffix), has_changes
return current_version, increment_suffix(current_version, bumping_suffix), has_changes

# Example case: New Release
logger.info('Semantic Version will be incremented.')
Expand Down Expand Up @@ -285,33 +304,64 @@ def main() -> None:
parser.add_argument(
'--suffix',
dest='suffix',
type=nullable_string,
default='',
help='The suffix that should be appended to the version (e.g. `beta`).'
)

parser.add_argument(
'--previous-version-suffix',
dest='previous_version_suffix',
type=nullable_string,
default='',
help='The suffix that should be replaced with the value in `suffix`.'
)

parser.add_argument(
'--bumping-suffix',
dest='bumping_suffix',
type=str,
default='hotfix',
help='The suffix that should be incremented / appended to the version.'
help='The suffix to append to the version (or increment if it already exists) if `only-bump-suffix` is `true`.'
)

parser.add_argument(
'--only-increase-suffix',
dest='only_increase_suffix',
type=str,
default='False',
help='Increment the suffix if any changes got detected.'
'--only-bump-suffix',
dest='only_bump_suffix',
type=parse_bool,
default='false',
help='Bump the `bumping-suffix` instead of the version if changes were detected.'
)

parser.add_argument(
'--create-tag',
dest='create_tag',
type=str,
default='False',
help='Create a Git Tag for the version and push it if a remote is configured.'
type=parse_bool,
default='true',
help='Create a git tag for the version and push it if a remote is configured.'
)

args = parser.parse_args()
args.only_increase_suffix = args.only_increase_suffix.lower() == 'true'
args.create_tag = args.create_tag.lower() == 'true'
# endregion

previous_version, new_version, has_changes = get_new_version(args.prefix, args.suffix, args.only_increase_suffix)
previous_version, new_version, has_changes = get_new_version(
args.prefix,
args.previous_version_suffix,
args.bumping_suffix,
args.only_bump_suffix
)

if args.previous_version_suffix is not None:
if '-' in previous_version:
previous_version = previous_version.replace('-', f'-{args.previous_version_suffix}-', 1)
else:
previous_version += f'-{args.previous_version_suffix}'

if args.suffix is not None:
if '-' in new_version:
new_version = new_version.replace('-', f'-{args.suffix}-', 1)
else:
new_version += f'-{args.suffix}'

new_version_tag_name = f'{args.prefix}{new_version}'
previous_version_tag_name = f'{args.prefix}{previous_version}'
Expand Down

0 comments on commit ad6f0f5

Please sign in to comment.