Skip to content

Commit

Permalink
feat: delete working branch and pull destination
Browse files Browse the repository at this point in the history
When you use the `--auto-merge` flag. We can cleanup the working branch and pull the merged result. This will remove some commands that you need to type.

Issue: #21
  • Loading branch information
Joris Conijn committed Feb 6, 2022
1 parent 3ba156a commit 0a3509e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pull_request_codecommit/git/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ def get_commit_messages(self, destination_branch: str) -> Commits:
)

return Commits(messages)

def checkout(self, destination: str) -> None:
self.__execute(["checkout", destination])

def pull(self) -> None:
self.__execute(["pull"])

def delete_branch(self, branch: str) -> None:
self.__execute(["branch", "-d", branch])
6 changes: 5 additions & 1 deletion pull_request_codecommit/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ def merge(self) -> str:
response = self.__client.merge_pull_request(
repository=self.__repo.remote.name, pull_request_id=self.__pull_request_id
)
status = response.get("pullRequestStatus", "")

return response.get("pullRequestStatus", "")
if status == "CLOSED":
self.__repo.checkout_destination()

return status

@property
def has_changes(self) -> bool:
Expand Down
6 changes: 6 additions & 0 deletions pull_request_codecommit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ def commits(self) -> Commits:

def push(self) -> None:
self.__git.push()

def checkout_destination(self) -> None:
branch = self.branch
self.__git.checkout(self.destination)
self.__git.pull()
self.__git.delete_branch(branch)
7 changes: 7 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit://my-repository-pr-failure",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
]
3 changes: 2 additions & 1 deletion tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def aws_client_execute_side_effect(parameters, stdout):
data = {"pullRequest": {"pullRequestId": 1}}

elif "merge-pull-request-by-fast-forward" in parameters:
data = {"pullRequest": {"pullRequestStatus": "CLOSED"}}
status = "OPEN" if parameters[-1] == "my-repository-pr-failure" else "CLOSED"
data = {"pullRequest": {"pullRequestStatus": status}}

mock_stdout.stdout = bytes(json.dumps(data), "utf-8")
return mock_stdout
Expand Down
48 changes: 48 additions & 0 deletions tests/test_git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,51 @@ def execute(parameters, cwd, stdout):

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


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

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

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


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

def execute(parameters, cwd, stdout):
assert parameters == ["git", "branch", "-d", "feat/my-feature"]
assert -1 == stdout
mock_stdout = MagicMock()
mock_stdout.stdout = bytes("", "utf-8")
return mock_stdout

mock_run.side_effect = execute
Client("my-path").delete_branch("feat/my-feature")


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

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

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

0 comments on commit 0a3509e

Please sign in to comment.