1
1
import requests
2
2
from requests .auth import HTTPBasicAuth
3
3
4
- from github import PullRequest # type: ignore
4
+ from github .PullRequest import PullRequest
5
+ from github .GithubException import GithubException
5
6
from src .github .get_app_token import sgtm_github_auth
6
7
from src .logger import logger
7
8
8
9
gh_client = sgtm_github_auth .get_rest_client ()
9
10
10
11
11
- def _get_pull_request (owner : str , repository : str , number : int ) -> PullRequest : # type: ignore
12
+ def _get_pull_request (owner : str , repository : str , number : int ) -> PullRequest :
12
13
repo = gh_client .get_repo (f"{ owner } /{ repository } " )
13
14
pr = repo .get_pull (number )
14
- return pr # type: ignore
15
+ return pr
15
16
16
17
17
18
def edit_pr_description (owner : str , repository : str , number : int , description : str ):
18
19
pr = _get_pull_request (owner , repository , number )
19
- pr .edit (body = description ) # type: ignore
20
+ pr .edit (body = description )
20
21
21
22
22
23
def edit_pr_title (owner : str , repository : str , number : int , title : str ):
23
24
pr = _get_pull_request (owner , repository , number )
24
- pr .edit (title = title ) # type: ignore
25
+ pr .edit (title = title )
25
26
26
27
27
28
def add_pr_comment (owner : str , repository : str , number : int , comment : str ):
28
29
pr = _get_pull_request (owner , repository , number )
29
- pr .create_issue_comment (comment ) # type: ignore
30
+ pr .create_issue_comment (comment )
30
31
31
32
32
33
def set_pull_request_assignee (owner : str , repository : str , number : int , assignee : str ):
33
34
repo = gh_client .get_repo (f"{ owner } /{ repository } " )
34
35
# Using get_issue here because get_pull returns a pull request which only
35
36
# allows you to *add* an assignee, not set the assignee.
36
37
pr = repo .get_issue (number )
37
- pr .edit (assignee = assignee ) # type: ignore
38
+ pr .edit (assignee = assignee )
38
39
39
40
40
41
def merge_pull_request (owner : str , repository : str , number : int , title : str , body : str ):
@@ -44,13 +45,15 @@ def merge_pull_request(owner: str, repository: str, number: int, title: str, bod
44
45
# which we rely on for code review tests.
45
46
title_with_number = f"{ title } (#{ number } )"
46
47
try :
47
- pr .enable_automerge (commit_headline = title_with_number , commit_body = body ) # type: ignore
48
+ pr .enable_automerge (commit_headline = title_with_number , commit_body = body )
48
49
except Exception as e :
49
50
logger .info (
50
51
f"Failed to enable automerge for PR { title_with_number } , with error { e } "
51
52
)
52
53
logger .info ("Merging PR manually" )
53
- pr .merge (commit_title = title_with_number , commit_message = body , merge_method = "squash" ) # type: ignore
54
+ pr .merge (
55
+ commit_title = title_with_number , commit_message = body , merge_method = "squash"
56
+ )
54
57
55
58
56
59
def rerequest_check_run (owner : str , repository : str , check_run_id : int ):
@@ -60,3 +63,28 @@ def rerequest_check_run(owner: str, repository: str, check_run_id: int):
60
63
)
61
64
# Some check runs cannot be rerequested. See https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#rerequest-a-check-run--status-codes
62
65
return requests .post (url , auth = auth ).status_code == 201
66
+
67
+
68
+ def delete_branch_if_exists (owner : str , repo_name : str , branch_name : str ):
69
+ """
70
+ Deletes a branch from a GitHub repository if it exists.
71
+
72
+ Args:
73
+ owner (str): The owner of the repository.
74
+ repo_name (str): The name of the repository.
75
+ branch_name (str): The name of the branch to delete.
76
+ """
77
+ try :
78
+ repo = gh_client .get_repo (f"{ owner } /{ repo_name } " )
79
+ # Attempt to get the branch, will raise a 404 error if not found
80
+ repo .get_branch (branch_name )
81
+ ref = f"heads/{ branch_name } "
82
+ git_ref = repo .get_git_ref (ref )
83
+ git_ref .delete ()
84
+ logger .info (f"Branch '{ branch_name } ' deleted successfully." )
85
+ except GithubException as e :
86
+ if e .status == 404 :
87
+ logger .info (f"Branch '{ branch_name } ' does not exist or is already deleted." )
88
+ else :
89
+ logger .error (f"Error deleting branch: { e } " )
90
+ raise
0 commit comments