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

[MergeQueues] Delete branch if PR is merged #193

Merged
merged 15 commits into from
Jun 28, 2024

Conversation

prebeta
Copy link
Contributor

@prebeta prebeta commented Jun 27, 2024

Summary

Github's delete branch logic doesn't work if SGTM added the pull request to the merge queue. This will make SGTM delete branches if they haven't been cleaned up yet.

Asana tasks:
https://app.asana.com/0/1125126232217429/1207661000934486/f

Relevant deployment:

CC: @suzyng83209 @prebeta @vn6 @michael-huang87

Test Plan

Run on test sgtm deployment

Risks

Pull Request: #193

Pull Request synchronized with Asana task

git_ref = repo.get_git_ref(ref)
git_ref.delete()
logger.info(f"Branch '{branch_name}' deleted successfully.")
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would imagine this is a GithubException where from github.GithubException import GithubException

@@ -219,3 +219,6 @@ def labels(self) -> List[Label]:

def base_ref_name(self) -> str:
return self._raw["baseRefName"]

def head_ref_name(self) -> str:
return self._raw["headRefName"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'm not seeing a corresponding change in FullPullRequest for this field. You need to specify which fields to query for in the graphql query to access it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i just added it into src/github/graphql/fragments/FullPullRequest.py

@@ -19,6 +19,7 @@ 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)
github_logic.maybe_add_automerge_warning_comment(pull_request)
github_logic.maybe_delete_branch_if_merged(pull_request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is probably better placed right after maybe_automerge_pull_request just to highlight the causal effect. It's good that it's not in maybe_automerge because it the deletion fails for whatever reason, it can try again in another webhook.

@asana-sgtm asana-sgtm bot assigned prebeta and unassigned suzyng83209 Jun 27, 2024
@@ -18,6 +18,8 @@ def _handle_pull_request_webhook(payload: dict) -> HttpResponse:
pull_request = graphql_client.get_pull_request(pull_request_id)
# a label change will trigger this webhook, so it may trigger automerge
github_logic.maybe_automerge_pull_request(pull_request)
if payload["action"] == "closed":
Copy link
Contributor Author

@prebeta prebeta Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this check in so we don't delete branches on arbitrary PR updates. The only time we can delete branches is if a PR is closed and merged which can only happen once per Pull Request.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose someone could create a branch with a new name, then re-open the same pull request and merge it which would result in a deletion but this seems like an unlikely situation so 👍

@prebeta prebeta removed their assignment Jun 27, 2024
Copy link
Collaborator

@suzyng83209 suzyng83209 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good. Can you add new test cases in test/github/test_webhook.py and test/github/test_logic.py? You may need to create a new field in test/impl/builders/pull_request_builder.py which is the mock pull request builder object to supply pull request objects to tests

@@ -18,6 +18,8 @@ def _handle_pull_request_webhook(payload: dict) -> HttpResponse:
pull_request = graphql_client.get_pull_request(pull_request_id)
# a label change will trigger this webhook, so it may trigger automerge
github_logic.maybe_automerge_pull_request(pull_request)
if payload["action"] == "closed":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose someone could create a branch with a new name, then re-open the same pull request and merge it which would result in a deletion but this seems like an unlikely situation so 👍

@asana-sgtm asana-sgtm bot assigned prebeta and unassigned suzyng83209 Jun 27, 2024
@prebeta prebeta force-pushed the tonyliang-delete-branch-if-merged branch from 0f88084 to 59ee004 Compare June 27, 2024 23:47
@prebeta prebeta force-pushed the tonyliang-delete-branch-if-merged branch from f88c066 to f71ee79 Compare June 28, 2024 00:43
@prebeta prebeta requested a review from suzyng83209 June 28, 2024 00:56
@prebeta prebeta assigned suzyng83209 and unassigned prebeta Jun 28, 2024
@prebeta prebeta added the merge after tests and approval SGTM will merge this pull request after tests pass and it is approved label Jun 28, 2024
Copy link

asana-sgtm bot commented Jun 28, 2024

⚠️ Reviewer: If you approve this PR, it will be auto-merged as soon as tests pass. If you don't want this to be auto-merged, either Request Changes or remove the auto-merge label before accepting.

Copy link
Collaborator

@suzyng83209 suzyng83209 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice lgtm and thanks for fixing types

@asana-sgtm asana-sgtm bot merged commit 557ad85 into master Jun 28, 2024
3 checks passed
@asana-sgtm asana-sgtm bot deleted the tonyliang-delete-branch-if-merged branch June 28, 2024 20:42
@asana-sgtm asana-sgtm bot assigned prebeta and unassigned suzyng83209 Jun 28, 2024
michael-huang87 added a commit that referenced this pull request Jul 2, 2024
michael-huang87 added a commit that referenced this pull request Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge after tests and approval SGTM will merge this pull request after tests pass and it is approved
Development

Successfully merging this pull request may close these issues.

3 participants