Skip to content

Commit

Permalink
releaser
Browse files Browse the repository at this point in the history
[release:0.1]
  • Loading branch information
heitorpolidoro committed Jan 16, 2024
1 parent 3e5bd47 commit 6f103d1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def handle(event: CheckSuiteRequestedEvent):
- Creates a Pull Request, if not exists, and/or enable the auto merge flag
"""
repository = event.repository
# handle_create_pull_request(repository, event.check_suite.head_branch)
handle_create_pull_request(repository, event.check_suite.head_branch)
handle_release(event)


Expand Down
42 changes: 40 additions & 2 deletions src/handlers/release.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
from github import UnknownObjectException
from githubapp.events import CheckSuiteRequestedEvent

from src.helpers.command import get_command
from src.helpers.pull_request import get_existing_pull_request


def handle_release(event: CheckSuiteRequestedEvent):
repository = event.repository

head_sha = event.check_suite.head_sha
head_branch = event.check_suite.head_branch

event.start_check_run("Releaser", head_sha, title="Checking for release command")

while True:
head_commit = repository.get_commit(head_sha)
if head_branch == repository.default_branch:
repository.create_git_release(
tag=version_to_release, generate_release_notes=True
)
return
else:
if pull_request := get_existing_pull_request(repository, head_branch):
version_to_release = None
for commit in pull_request.get_commits().reversed:
if version_to_release := get_command(commit.commit.message, "release"):
break

if not version_to_release:
event.update_check_run(
title="No release command found", conclusion="success"
)
return

event.update_check_run(
title=f"Prepared to release {version_to_release}",
summary="Release command found ✅",
)

# try:
# last_release = repository.get_latest_release()
# except UnknownObjectException:
# last_release = "0"

# if commit.message.startswith("release"):
# event.create_check_run(
# "Releaser", head_sha, title="Found release command", conclusion="success"
# )
# return
# while True:
# head_commit = repository.get_commit(head_sha)
# commit
19 changes: 19 additions & 0 deletions src/helpers/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import re
from typing import Optional


def get_command(text: str, command_prefix: str) -> Optional[str]:
"""
Retrieve the command from the commit message.
The command in the commit message must be in the format [command_prefix: command]
:param text: The Commit object.
:param command_prefix: The command prefix to look for in the commit message.
:return: The extracted command or None if there is no command.
:raises: ValueError if the command is not valid.
"""
command_pattern = rf"\[{command_prefix}:(.+?)\]"
commands_found = re.findall(command_pattern, text)
if commands_found:
return commands_found[-1].strip()
return None
15 changes: 13 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ def head_commit():
"""
commit = Mock()
commit.sha = "sha"
commit.commit.message = "message"
return commit

@pytest.fixture
def pull_request(head_commit):
"""
This fixture returns a mock PullRequest object with default values for the attributes.
:return: Mocked PullRequest
"""
pull_request = Mock()
pull_request.get_commits.return_value.reversed = [head_commit]
return pull_request


@pytest.fixture
def repository(head_commit):
def repository(head_commit, pull_request):
"""
This fixture returns a mock repository object with default values for the attributes.
:return: Mocked Repository
Expand All @@ -24,7 +35,7 @@ def repository(head_commit):
repository.default_branch = "master"
repository.full_name = "heitorpolidoro/bartholomew-smith"
repository.owner.login = "heitorpolidoro"
repository.get_pulls.return_value = []
repository.get_pulls.return_value = [pull_request]
repository.get_commit.return_value = head_commit
return repository

Expand Down
24 changes: 22 additions & 2 deletions tests/handlers/test_release.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
from unittest.mock import patch, Mock

from src.handlers.release import handle_release


def test_handle_release(event, repository):
def test_handle_release_when_there_is_no_command(event, repository, pull_request):
handle_release(event)
event.start_check_run.assert_called_once_with(
"Releaser", "sha", title="Checking for release command"
)
pull_request.get_commits.assert_called_once()
event.update_check_run.assert_called_once_with(
title="No release command found", conclusion="success"
)


def test_handle_release_when_there_is_a_command(event, repository, pull_request):
commit = Mock(commit=Mock(message="[release:1.2.3]"))
pull_request.get_commits.return_value.reversed = [commit]

handle_release(event)
repository.get_commit.assert_called_once_with("sha")
event.start_check_run.assert_called_once_with(
"Releaser", "sha", title="Checking for release command"
)
pull_request.get_commits.assert_called_once()
event.update_check_run.assert_called_once_with(
title="Prepared to release 1.2.3",
summary="Release command found ✅\nReleasing 1.2.3",
)
8 changes: 7 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ def test_sentry_dont_init(monkeypatch):


def test_handle_check_suite_requested(event, repository):
with patch("app.handle_create_pull_request") as mock_handle_create_pull_request:
with (
patch("app.handle_create_pull_request") as mock_handle_create_pull_request,
patch("app.handle_release") as mock_handle_release,
):
handle(event)
mock_handle_create_pull_request.assert_called_once_with(
repository, event.check_suite.head_branch
)
mock_handle_release.assert_called_once_with(
event
)


class TestApp(TestCase):
Expand Down

0 comments on commit 6f103d1

Please sign in to comment.