Skip to content

Commit

Permalink
refactor: introduce pull request class (#10)
Browse files Browse the repository at this point in the history
By using an object it makes the code easier to read. It also moves the logic to create pull request proposals to its own class.

Issue: #8
  • Loading branch information
Joris Conijn committed Jan 24, 2022
1 parent 5b87e37 commit e15ccf9
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pull_request_codecommit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def main(repository_path: Optional[str]) -> None:
click.echo()
pull_request = repo.pull_request_information()

if not pull_request:
if not pull_request.has_changes:
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]}")
message = click.edit(f"{pull_request.title}\n\n{pull_request.description}")

if message is None:
raise click.ClickException("Pull request was not created")
Expand Down
4 changes: 3 additions & 1 deletion pull_request_codecommit/git/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def __next__(self) -> Commit:

@property
def issues(self) -> List[str]:
return list(set(map(lambda commit: commit.message.issue, self.__commits)))
return list(
set(filter(None, map(lambda commit: commit.message.issue, self.__commits)))
)

@staticmethod
def __read_commit_message(message: str) -> Commit:
Expand Down
35 changes: 35 additions & 0 deletions pull_request_codecommit/pull_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pull_request_codecommit.git import Commits


class PullRequest:
"""
Understands pull requests
"""

def __init__(self, commits: Commits) -> None:
self.__commits = commits

@property
def has_changes(self) -> bool:
return True if self.__commits.first else False

@property
def title(self) -> str:
if not self.__commits.first:
return ""

title = self.__commits.first.message.subject

if self.__commits.issues:
title += f" ({', '.join(self.__commits.issues)})"

return title

@property
def description(self) -> str:
description = list(map(lambda commit: commit.message.subject, self.__commits))

if self.__commits.issues:
description.append(f"\nIssues: " + ", ".join(self.__commits.issues))

return "\n".join(description)
17 changes: 3 additions & 14 deletions pull_request_codecommit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .config import Config
from .git import Client as GitClient
from .aws import Client as AwsClient
from .pull_request import PullRequest


class Repository:
Expand Down Expand Up @@ -75,21 +76,9 @@ def destination(self) -> str:
destination = self.__config("destination_branch")
return destination if destination else ""

def pull_request_information(self) -> Optional[Tuple[str, str]]:
title_postfix = ""
def pull_request_information(self) -> PullRequest:
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:
description.append(f"\nIssues: {issues}")
title_postfix = f" ({issues})"

title = f"{commits.first.message.subject}{title_postfix}"
return title, "\n".join(description)
return PullRequest(commits)

def create_pull_request(self, title: str, description: str) -> str:
client = AwsClient(profile=self.__aws_profile, region=self.__aws_region)
Expand Down
67 changes: 67 additions & 0 deletions tests/test_pull_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest

from pull_request_codecommit.git import Commits
from pull_request_codecommit.pull_request import PullRequest


COMMIT_SIMPLE_MESSAGE = """commit my-hash-1
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my first commit"""

COMMIT_DETAILED_MESSAGE = """commit my-hash-1
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my first commit
Some additional information"""

COMMIT_SIMPLE_MESSAGE_ISSUE = """commit my-hash-1
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my first commit
Issue: #1"""

COMMIT_DETAILED_MESSAGE_ISSUE = """commit my-hash-1
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my first commit
Some additional information
Issue: #1"""


@pytest.mark.parametrize(
"commits, expected_title, expected_description",
[
(Commits(COMMIT_SIMPLE_MESSAGE), "feat: my first commit", ""),
(Commits(COMMIT_DETAILED_MESSAGE), "feat: my first commit", ""),
(
Commits(COMMIT_SIMPLE_MESSAGE_ISSUE),
"feat: my first commit (#1)",
"\nIssues: #1",
),
(
Commits(COMMIT_DETAILED_MESSAGE_ISSUE),
"feat: my first commit (#1)",
"\nIssues: #1",
),
(
Commits(""),
"",
"",
),
],
)
def test_git_client(
commits: Commits, expected_title: str, expected_description: str
) -> None:
pull_request = PullRequest(commits)
assert pull_request.title == expected_title
assert pull_request.description == expected_description

0 comments on commit e15ccf9

Please sign in to comment.