Skip to content

Commit

Permalink
fix: index error when there are no commits (#9)
Browse files Browse the repository at this point in the history
By returning a None when there are no commits we can detect gracefully.

Fixes Issue: #8
  • Loading branch information
Joris Conijn authored Jan 24, 2022
1 parent 9059558 commit 5b87e37
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
10 changes: 8 additions & 2 deletions pull_request_codecommit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ def main(repository_path: Optional[str]) -> None:
click.echo(f"Repo: {repo.repository_name}")
click.echo(f"Branch: {repo.branch}")
click.echo()
title, description = repo.pull_request_information()
pull_request = repo.pull_request_information()

message = click.edit(f"{title}\n\n{description}")
if not pull_request:
click.echo(
f"There are no differences between {repo.destination} and {repo.branch}!"
)
exit(0)

message = click.edit(f"{pull_request[0]}\n\n{pull_request[1]}")

if message is None:
raise click.ClickException("Pull request was not created")
Expand Down
6 changes: 3 additions & 3 deletions pull_request_codecommit/git/commits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional
import re

from .commit import Commit
Expand Down Expand Up @@ -26,8 +26,8 @@ def __init__(self, messages: str) -> None:
self.__commits.reverse()

@property
def first(self) -> Commit:
return self.__commits[0]
def first(self) -> Optional[Commit]:
return self.__commits[0] if self.__commits else None

def __iter__(self):
"""
Expand Down
5 changes: 4 additions & 1 deletion pull_request_codecommit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ def destination(self) -> str:
destination = self.__config("destination_branch")
return destination if destination else ""

def pull_request_information(self) -> Tuple[str, str]:
def pull_request_information(self) -> Optional[Tuple[str, str]]:
title_postfix = ""
commits = self.__git.get_commit_messages(destination_branch=self.destination)

if not commits.first:
return None

issues = ", ".join(commits.issues)
description = list(map(lambda commit: commit.message.subject, commits))
if issues:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ def execute(parameters, stdout):
assert result.exit_code == 0


@patch("pull_request_codecommit.repository.GitClient")
@patch("pull_request_codecommit.click.edit")
def test_invoke_no_commits(
mock_edit: MagicMock,
mock_git_client: MagicMock,
) -> None:
mock_edit.side_effect = edit_message
mock_git_client.return_value.get_commit_messages.return_value = Commits("")

mock_git_client.return_value.remote.return_value = (
f"codecommit::eu-west-1://my-profile@my-repository"
)
mock_git_client.return_value.current_branch.return_value = "feat/my-feature"
config = b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master"
configparser.open = MagicMock(return_value=TextIOWrapper(BytesIO(config))) # type: ignore

runner = CliRunner()
result = runner.invoke(main, [])
assert result.exit_code == 0
assert "There are no differences between" in result.output


@pytest.mark.parametrize(
"region, profile, config",
[
Expand Down
20 changes: 20 additions & 0 deletions tests/test_git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,28 @@ def execute(parameters, cwd, stdout):
client = Client(path)

commits = client.get_commit_messages("main")

first: Commit = next(commits)
second: Commit = next(commits)

assert first == commits.first
assert first.message.subject == "feat: my first commit"
assert second.message.subject == "feat: my second commit"


@patch("pull_request_codecommit.git.client.subprocess.run")
@patch("pull_request_codecommit.git.client.os.path.isdir")
def test_get_no_commit_messages(mock_isdir: MagicMock, mock_run: MagicMock) -> None:
mock_isdir.return_value = True

def execute(parameters, cwd, stdout):
assert -1 == stdout
mock_stdout = MagicMock()
mock_stdout.stdout = bytes("", "utf-8")
return mock_stdout

mock_run.side_effect = execute
client = Client("my-path")

commits = client.get_commit_messages("main")
assert commits.first is None

0 comments on commit 5b87e37

Please sign in to comment.