8000 feat(github-actions): add `is_prerelease` output to the version action by codejedi365 · Pull Request #1038 · python-semantic-release/python-semantic-release · GitHub
[go: up one dir, main page]

Skip to content

feat(github-actions): add is_prerelease output to the version action #1038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ inputs:
Build metadata to append to the new version

outputs:
is_prerelease:
description: |
"true" if the version is a prerelease, "false" otherwise

released:
description: |
"true" if a release was made, "false" otherwise
Expand Down
11 changes: 11 additions & 0 deletions docs/automatic-releases/github-actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ and any actions that were taken.

----

.. _gh_actions-psr-outputs-is_prerelease:

``is_prerelease``
""""""""""""""""

**Type:** ``Literal["true", "false"]``

A boolean value indicating whether the released version is a prerelease.

----

.. _gh_actions-psr-outputs-released:

``released``
Expand Down
17 changes: 10 additions & 7 deletions semantic_release/cli/github_actions_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class VersionGitHubActionsOutput:
OUTPUT_ENV_VAR = "GITHUB_OUTPUT"

def __init__(
self, released: bool | None = None, version: Version | None = None
self,
released: bool | None = None,
version: Version | None = None,
) -> None:
self._released = released
self._version = version
Expand All @@ -29,9 +31,7 @@ def released(self, value: bool) -> None:

@property
def version(self) -> Version | None:
if self._version is None:
return None
return self._version
return self._version if self._version is not None else None

@version.setter
def version(self, value: Version) -> None:
Expand All @@ -41,9 +41,11 @@ def version(self, value: Version) -> None:

@property
def tag(self) -> str | None:
if self._version is None:
return None
return self._version.as_tag()
return self.version.as_tag() if self.version is not None else None

@property
def is_prerelease(self) -> bool | None:
return self.version.is_prerelease if self.version is not None else None

def to_output_text(self) -> str:
missing = set()
Expand All @@ -61,6 +63,7 @@ def to_output_text(self) -> str:
"released": str(self.released).lower(),
"version": str(self.version),
"tag": self.tag,
"is_prerelease": str(self.is_prerelease).lower(),
}

return str.join("", [f"{key}={value!s}\n" for key, value in outputs.items()])
Expand Down
20 changes: 15 additions & 5 deletions tests/unit/semantic_release/cli/test_github_actions_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@
from pathlib import Path


@pytest.mark.parametrize(
"version, is_prerelease",
[
("1.2.3", False),
("1.2.3-alpha.1", True),
],
)
@pytest.mark.parametrize("released", (True, False))
def test_version_github_actions_output_format(released: bool):
version_str = "1.2.3"
def test_version_github_actions_output_format(
released: bool, version: str, is_prerelease: bool
):
expected_output = dedent(
f"""\
released={'true' if released else 'false'}
version={version_str}
tag=v{version_str}
version={version}
tag=v{version}
is_prerelease={'true' if is_prerelease else 'false'}
"""
)
output = VersionGitHubActionsOutput(
released=released,
version=Version.parse(version_str),
version=Version.parse(version),
)

# Evaluate (expected -> actual)
Expand Down Expand Up @@ -63,6 +72,7 @@ def test_version_github_actions_output_writes_to_github_output_if_available(
# Evaluate (expected -> actual)
assert version_str == action_outputs["version"]
assert str(True).lower() == action_outputs["released"]
assert str(False).lower() == action_outputs["is_prerelease"]


def test_version_github_actions_output_no_error_if_not_in_gha(
Expand Down
0