Skip to content

Commit

Permalink
refactor: prepare codebase for auto-merge feature (#19)
Browse files Browse the repository at this point in the history
* refactor: simplify the cli interaction
* refactor: encapsulate pull request information
* refactor: move pull request creation
  • Loading branch information
Joris Conijn authored Feb 5, 2022
1 parent fda47da commit 16c0421
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 32 deletions.
49 changes: 36 additions & 13 deletions pull_request_codecommit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import click

from pull_request_codecommit.repository import Repository
from .pull_request import PullRequest
from .repository import Repository


@click.command()
Expand All @@ -11,37 +12,59 @@ def main(repository_path: Optional[str]) -> None:
"""
pull-request-codecommit
"""
repo = __load_repository(repository_path)
__display_repository_information(repo)
__create_pull_request(repo)


def __load_repository(repository_path: Optional[str]) -> Repository:
repo = Repository(repository_path)

if not repo.remote.supported:
raise click.ClickException("The repository is not compatible with this tool!")

return repo


def __create_pull_request(repo: Repository) -> PullRequest:
pr = PullRequest(repo)

if not pr.has_changes:
click.echo(
f"There are no differences between {repo.destination} and {repo.branch}!"
)
exit(0)

__propose_title_description(pr)
pr.create()

click.echo(f"Please review/approve: {pr.link}")
click.echo()
click.echo(click.style(pr.title, bold=True))
click.echo(pr.description)

return pr


def __display_repository_information(repo: Repository) -> None:
click.echo(f"Remote: {repo.remote.url}")
click.echo(f"Region: {repo.remote.region}")
click.echo(f"Profile: {repo.remote.profile}")
click.echo(f"Repo: {repo.remote.name}")
click.echo(f"Branch: {repo.branch}")
click.echo()
pull_request = repo.pull_request_information()

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.title}\n\n{pull_request.description}")
def __propose_title_description(pr: PullRequest) -> None:
message = click.edit(f"{pr.title}\n\n{pr.description}")

if message is None:
raise click.ClickException("Pull request was not created")

title = message.splitlines()[0]
description = "\n".join(message.splitlines()[1:]).lstrip("\n")
link = repo.create_pull_request(title, description)
click.echo(f"Please review/approve: {link}")
click.echo()
click.echo(click.style(title, bold=True))
click.echo(description)

pr.update(title, description)


if __name__ == "__main__":
Expand Down
46 changes: 44 additions & 2 deletions pull_request_codecommit/pull_request.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
from .repository import Repository
from pull_request_codecommit.git import Commits
from .aws import Client as AwsClient


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

def __init__(self, commits: Commits) -> None:
self.__commits = commits
def __init__(self, repo: Repository) -> None:
self.__repo: Repository = repo
self.__commits: Commits = self.__repo.commits()
self.__title: str = ""
self.__description: str = ""
self.__link: str = ""

def update(self, title: str, description: str) -> None:
self.__title = title
self.__description = description

def create(self) -> None:
self.__repo.push()
client = AwsClient(
profile=self.__repo.remote.profile, region=self.__repo.remote.region
)
response = client.create_pull_request(
title=self.title,
description=self.description,
repository=self.__repo.remote.name,
source=self.__repo.branch,
destination=self.__repo.destination,
)

self.__link = "".join(
[
f"https://{self.__repo.remote.region}.console.aws.amazon.com/",
"codesuite/codecommit/repositories/",
f"pull-requests/{response.get('pullRequestId')}/details",
f"?region={self.__repo.remote.region}",
]
)

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

@property
def title(self) -> str:
if self.__title:
return self.__title

if not self.__commits.first:
return ""

Expand All @@ -27,6 +62,9 @@ def title(self) -> str:

@property
def description(self) -> str:
if self.__description:
return self.__description

if len(self.__commits) == 1:
description = (
[self.__commits.first.message.body] if self.__commits.first else []
Expand All @@ -40,3 +78,7 @@ def description(self) -> str:
description.append(f"\nIssues: " + ", ".join(self.__commits.issues))

return "\n".join(description).lstrip("\n")

@property
def link(self) -> str:
return self.__link
20 changes: 4 additions & 16 deletions pull_request_codecommit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
from typing import Optional

from .config import Config
from .git import Client as GitClient, Remote
from .aws import Client as AwsClient
from .pull_request import PullRequest
from .git import Client as GitClient, Remote, Commits


class Repository:
Expand Down Expand Up @@ -43,18 +41,8 @@ def destination(self) -> str:
destination = self.__config("destination_branch")
return destination if destination else ""

def pull_request_information(self) -> PullRequest:
commits = self.__git.get_commit_messages(destination_branch=self.destination)
return PullRequest(commits)
def commits(self) -> Commits:
return self.__git.get_commit_messages(destination_branch=self.destination)

def create_pull_request(self, title: str, description: str) -> str:
def push(self) -> None:
self.__git.push()
client = AwsClient(profile=self.remote.profile, region=self.remote.region)
response = client.create_pull_request(
title=title,
description=description,
repository=self.remote.name,
source=self.branch,
destination=self.destination,
)
return f"https://{self.remote.region}.console.aws.amazon.com/codesuite/codecommit/repositories/{self.remote.name}/pull-requests/{response.get('pullRequestId')}/details?region={self.remote.region}"
7 changes: 6 additions & 1 deletion tests/test_pull_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import MagicMock

import pytest

from pull_request_codecommit.git import Commits
Expand Down Expand Up @@ -66,6 +68,9 @@
def test_git_client(
commits: Commits, expected_title: str, expected_description: str
) -> None:
pull_request = PullRequest(commits)
mock_repo = MagicMock()
mock_repo.commits.return_value = commits

pull_request = PullRequest(mock_repo)
assert pull_request.title == expected_title
assert pull_request.description == expected_description

0 comments on commit 16c0421

Please sign in to comment.