Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add branch deletion exceptions #194

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/github/graphql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
GetPullRequestByRepositoryAndNumber,
GetPullRequestAndComment,
GetPullRequestAndReview,
GetRepository,
IteratePullRequestIdsForCommitId,
IterateReviewsForPullRequestId,
)
Expand Down Expand Up @@ -146,3 +147,8 @@ def is_review(review_edge):
},
)["node"]["reviews"]["edges"]
return None


def get_repository(repository_id: str):
data = _execute_graphql_query(GetRepository, {"repositoryId": repository_id})
return data["repository"]
17 changes: 17 additions & 0 deletions src/github/graphql/queries/GetRepository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import FrozenSet

# @GraphqlInPython
_get_repository = """
query GetRepository($repositoryId: ID!) {
repository: node($repositoryId: ID!) {
... on Repository {
deleteBranchOnMerge
defaultBranchRef{
name
}
}
}
}
"""

GetRepository: FrozenSet[str] = frozenset([_get_repository])
5 changes: 3 additions & 2 deletions src/github/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,13 @@ def maybe_automerge_pull_request(pull_request: PullRequest) -> bool:
return False


def maybe_delete_branch_if_merged(pull_request: PullRequest):
def maybe_delete_branch_if_merged(pull_request: PullRequest, repository):
if pull_request.merged():
owner = pull_request.repository_owner_handle()
repo_name = pull_request.repository_name()
branch_name = pull_request.head_ref_name()
github_client.delete_branch_if_exists(owner, repo_name, branch_name)
if branch_name != repository["default_branch"] and repository["deleteBranchOnMerge"]:
github_client.delete_branch_if_exists(owner, repo_name, branch_name)


# ----------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/github/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def _handle_pull_request_webhook(payload: dict) -> HttpResponse:
# a label change will trigger this webhook, so it may trigger automerge
github_logic.maybe_automerge_pull_request(pull_request)
if payload["action"] == "closed":
github_logic.maybe_delete_branch_if_merged(pull_request)
repository = graphql_client.get_repository(pull_request.repository_id())
github_logic.maybe_delete_branch_if_merged(pull_request, repository)
github_logic.maybe_add_automerge_warning_comment(pull_request)
github_controller.upsert_pull_request(pull_request)
return HttpResponse("200")
Expand Down
17 changes: 17 additions & 0 deletions test/github/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,23 @@ def test_do_not_delete_branch_if_closed(self, mock_delete_branch):
github_logic.maybe_delete_branch_if_merged(pull_request)
mock_delete_branch.assert_not_called()

def test_do_not_delete_default_branch(self, mock_delete_branch):
pull_request = build(builder.pull_request().merged(True).head_ref_name("main"))
repository = {"deleteBranchOnMerge": True, "defaultBranchRef": {"name": "main"}}
github_logic.maybe_delete_branch_if_merged(pull_request, repository)
mock_delete_branch.assert_not_called()

def test_do_not_delete_if_repo_setting(self, mock_delete_branch):
pull_request = build(
builder.pull_request().merged(True).head_ref_name("feature-branch")
)
repository = {
"deleteBranchOnMerge": False,
"defaultBranchRef": {"name": "main"},
}
github_logic.maybe_delete_branch_if_merged(pull_request, repository)
mock_delete_branch.assert_not_called()


if __name__ == "__main__":
from unittest import main as run_tests
Expand Down