Skip to content

Commit

Permalink
Merge pull request #44 from wemogy/main
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
SebastianKuesters authored Mar 22, 2024
2 parents 0429a20 + b4fea4b commit a65bccd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ for [Conventional Commits](https://www.conventionalcommits.org/) with support fo

- run: echo ${{ steps.get-release-version.outputs.version }}
- run: echo ${{ steps.get-release-version.outputs.version-name }}
- run: echo ${{ steps.get-release-version.outputs.previous-version }}
- run: echo ${{ steps.get-release-version.outputs.previous-version-name }}
- run: echo ${{ steps.get-release-version.outputs.has-changes }}
```
Expand All @@ -35,11 +37,13 @@ for [Conventional Commits](https://www.conventionalcommits.org/) with support fo

## Outputs

| Output | Description |
| -------------- | ------------------------------------- |
| `version` | The next version, without the prefix. |
| `version-name` | The next version, with the prefix. |
| `has-changes` | If any relevant changes got detected. |
| Output | Description |
|-------------------------|-------------------------------------------|
| `version` | The next version, without the prefix. |
| `version-name` | The next version, with the prefix. |
| `previous-version` | The previous version, without the prefix. |
| `previous-version-name` | The previous version, with the prefix. |
| `has-changes` | If any relevant changes got detected. |

## Docker

Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ outputs:
description: "The next version, without the prefix"
version-name:
description: "The next version, with the prefix"
previous-version:
description: "The previous version, without the prefix"
previous-version-name:
description: "The previous version, with the prefix"
has-changes:
description: "If any relevant changes got detected"
runs:
Expand Down
49 changes: 29 additions & 20 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ def print_github_actions_output() -> None:
file_path = os.getenv('GITHUB_OUTPUT')

if not file_path:
logger.info('GITHUB_OUTPUT not in environment, skipping GitHub actions output')
logger.warning('GITHUB_OUTPUT not in environment, skipping GitHub actions output')
return

# noinspection PyBroadException
try:
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:
except Exception as exc:
# Catching every exception since this function is not necessary for the script to run
logger.exception('An error occurred')
logger.warning('An exception was ignored while trying to get contents of GITHUB_OUTPUT file', exc_info=True)


def clear_github_output() -> None:
"""Clear the GITHUB_OUTPUT file."""
file_path = os.getenv('GITHUB_OUTPUT')

if not file_path:
logger.info('GITHUB_OUTPUT not in environment, skipping GitHub actions output')
logger.warning('GITHUB_OUTPUT not in environment, skipping GitHub actions output')
return

logging.info('Clearing GITHUB_OUTPUT file "%s"', file_path)
Expand All @@ -56,7 +56,7 @@ def set_github_output(name: str, value: Any) -> None:
file_path = os.getenv('GITHUB_OUTPUT')

if not file_path:
logger.info('GITHUB_OUTPUT not in environment, skipping GitHub actions output')
logger.warning('GITHUB_OUTPUT not in environment, skipping GitHub actions output')
return

with open(file_path, 'a') as fh:
Expand Down Expand Up @@ -110,14 +110,15 @@ def get_current_version_tag(repo: git.Repo, prefix: str) -> git.TagReference | N
Get the current version (= the latest git tag).
If there are no tags, return None.
"""
try:
# Reverse the list of tags to start with the most recent one
for tag in reversed(repo.tags):
# Check if the tag name starts with the specified prefix
if tag.name.startswith(prefix):
return tag
except IndexError:
return None
# Reverse the list of tags to start with the most recent one
for tag in reversed(repo.tags):
# Check if the tag name starts with the specified prefix
if tag.name.startswith(prefix):
logger.debug('Found tag %s (%s)', tag.name, tag.commit.hexsha)
return tag

logger.debug('Found no tags that have the prefix %s', prefix)
return None


def get_new_commits(repo: git.Repo, starting_tag: git.TagReference | None) -> list[git.Commit]:
Expand Down Expand Up @@ -185,6 +186,7 @@ def get_next_version(repo: git.Repo, current_version_tag: git.TagReference | Non

# The maximum of the numbers in commit_bumps is the version we need to bump
version_to_bump = max(commit_bumps) if len(commit_bumps) > 0 else 0
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)
Expand Down Expand Up @@ -222,8 +224,12 @@ 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, bool]:
"""Get the new version, involving the only_increase_suffix flag."""
def get_new_version(prefix: str, suffix: str, only_increase_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)

Expand All @@ -234,24 +240,24 @@ def get_new_version(prefix: str, suffix: str, only_increase_suffix: bool) -> tup

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

logger.info(
logger.debug(
'current_version=%s, next_version=%s, has_changes=%s',
current_version, next_version, has_changes
)

# Example case: No change that requires a semantic version increase
if not has_changes:
logger.info('No changes detected, version stays the same.')
return next_version, has_changes
return current_version, next_version, has_changes

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

# Example case: New Release
logger.info('Semantic Version will be incremented.')
return next_version, has_changes
return current_version, next_version, has_changes


def main() -> None:
Expand Down Expand Up @@ -305,9 +311,10 @@ def main() -> None:
args.create_tag = args.create_tag.lower() == 'true'
# endregion

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.suffix, args.only_increase_suffix)

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

if args.create_tag and has_changes:
create_tag(new_version_tag_name)
Expand All @@ -317,6 +324,8 @@ def main() -> None:

set_github_output('version', new_version)
set_github_output('version-name', new_version_tag_name)
set_github_output('previous-version', previous_version)
set_github_output('previous-version-name', previous_version_tag_name)
set_github_output('has-changes', str(has_changes).lower())

print_github_actions_output()
Expand Down

0 comments on commit a65bccd

Please sign in to comment.