Skip to content

Commit

Permalink
feat: pre-determine release body content (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored Jun 8, 2024
1 parent 361701f commit 3119a3a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ The action does the following:
| tag_prefix | The tag prefix. This will be used when searching for existing releases in GitHub API. | `v` | `false` |

## Outputs
| Name | Description |
|--------------------------------|--------------------------------------------------------------------|
| publish_release | Whether or not to publish a release |
| release_commit | The commit hash for the release |
| release_tag | The tag for the release (i.e. `release_version` with prefix) |
| release_version | The version for the release (i.e. `yyyy.mmdd.hhmmss`) |
| Name | Description |
|--------------------------------|----------------------------------------------------------------------------|
| publish_release | Whether or not to publish a release |
| release_body | The body for the release |
| release_commit | The commit hash for the release |
| release_generate_release_notes | Whether or not to generate release notes. True if `release_body` is blank. |
| release_tag | The tag for the release (i.e. `release_version` with prefix) |
| release_version | The version for the release (i.e. `yyyy.mmdd.hhmmss`) |
53 changes: 51 additions & 2 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,45 @@ def get_push_event_details() -> dict:
return push_event_details


def generate_release_body(tag_name: str, target_commitish: str) -> str:
"""
Generate the release body, by comparing this SHA to the previous latest release.
Parameters
----------
tag_name : str
Tag name of the release.
target_commitish : str
The commitish value that determines where the Git tag is created from.
Returns
-------
str
Release body.
"""
# Get the latest release from the GitHub API
github_api_url = f'https://api.github.com/repos/{REPOSITORY_NAME}/releases/latest'
response = requests.get(github_api_url, headers=GITHUB_HEADERS)

# Check if the release exists
if not response.status_code == 200:
return ''

latest_release = response.json()

# generate release notes
github_api_url = f'https://api.github.com/repos/{REPOSITORY_NAME}/releases/generate-notes'
data = {
'tag_name': tag_name,
'target_commitish': target_commitish,
'previous_tag_name': latest_release['tag_name'],
}
response = requests.post(github_api_url, headers=GITHUB_HEADERS, json=data)

release_notes = response.json()
return release_notes['body']


def main() -> dict:
"""
Main function for the action.
Expand All @@ -222,17 +261,27 @@ def main() -> dict:
# Get the push event details
push_event_details = get_push_event_details()

release_generate_release_notes = True
release_version = push_event_details["release_version"]
release_tag = f"{release_version}"

# generate release notes
if push_event_details['publish_release']:
release_notes = generate_release_body(
tag_name=release_tag,
target_commitish=push_event_details["release_commit"],
)
else:
release_notes = ''
release_generate_release_notes = True if not release_notes else False

version_prefix = ''
if os.getenv('INPUT_INCLUDE_TAG_PREFIX_IN_OUTPUT', 'true').lower() == 'true':
version_prefix = os.getenv('INPUT_TAG_PREFIX', 'v')

job_outputs['publish_release'] = str(push_event_details['publish_release']).lower()
job_outputs['release_commit'] = push_event_details['release_commit']
job_outputs['release_generate_release_notes'] = str(release_generate_release_notes).lower()
job_outputs['release_notes'] = release_notes
job_outputs['release_version'] = release_version
job_outputs['release_tag'] = f'{version_prefix if release_tag else ""}{release_tag}'

Expand All @@ -246,4 +295,4 @@ def main() -> dict:


if __name__ == "__main__":
main()
main() # pragma: no cover
42 changes: 40 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# standard imports
import os
from unittest.mock import patch, Mock

# lib imports
from dotenv import load_dotenv
Expand Down Expand Up @@ -103,7 +104,14 @@ def github_event_path(request):
# true is original file from GitHub context
# false is dummy file

original_value = os.environ['GITHUB_EVENT_PATH']
original_value = os.getenv(
'GITHUB_EVENT_PATH',
os.path.join(
os.path.dirname(os.path.dirname(__file__)),
'dummy_github_event.json'
)
)

if request.param:
yield
else:
Expand All @@ -112,7 +120,8 @@ def github_event_path(request):
'dummy_github_event.json'
)
yield
os.environ['GITHUB_EVENT_PATH'] = original_value

os.environ['GITHUB_EVENT_PATH'] = original_value


@pytest.fixture(scope='function')
Expand All @@ -139,3 +148,32 @@ def input_dotnet(request):
yield

del os.environ['INPUT_DOTNET']


@pytest.fixture(scope='function')
def requests_get_error():
original_get = requests.get

mock_response = Mock()
mock_response.status_code = 500
requests.get = Mock(return_value=mock_response)

yield

requests.get = original_get


@pytest.fixture(params=[True, False])
def mock_get_push_event_details(request):
if request.param:
# If the parameter is True, return a mock
with patch('action.main.get_push_event_details') as mock:
mock.return_value = {
'publish_release': True,
'release_commit': 'master',
'release_version': 'test',
}
yield
else:
# If the parameter is False, don't patch anything
yield
22 changes: 21 additions & 1 deletion tests/unit/test_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# standard imports
import os

# lib imports
import pytest

Expand Down Expand Up @@ -33,6 +36,7 @@ def test_set_github_action_output(github_output_file, outputs):

@pytest.mark.parametrize('version', [
('1970.1.1', False),
('2023.1127.235828', True),
])
def test_check_release(version):
assert main.check_release(version=version[0]) == version[1]
Expand All @@ -57,7 +61,23 @@ def test_get_push_event_details_fail_on_error(dummy_github_event_path, dummy_com
main.get_push_event_details()


def test_main(github_output_file, github_step_summary_file, github_token, input_dotnet):
def test_generate_release_body(github_token):
assert main.generate_release_body(tag_name='test', target_commitish=os.environ['GITHUB_SHA'])


def test_generate_release_body_non_200_status_code(github_token, requests_get_error):
assert main.generate_release_body(tag_name='test', target_commitish='abc') == ''


@pytest.mark.parametrize('should_mock', [True, False])
def test_main(
github_output_file,
github_step_summary_file,
github_token,
input_dotnet,
should_mock,
mock_get_push_event_details,
):
job_outputs = main.main()

with open(github_output_file, 'r') as f:
Expand Down

0 comments on commit 3119a3a

Please sign in to comment.