Skip to content

Commit 1f08664

Browse files
authored
Delete a branch when PR is closed. (GH-24)
1 parent 4b8e9d7 commit 1f08664

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

backport/delete_branch.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import gidgethub.routing
2+
3+
from . import util
4+
5+
router = gidgethub.routing.Router()
6+
7+
8+
@router.register("pull_request", action="closed")
9+
async def delete_branch(event, gh, *args, **kwargs):
10+
"""
11+
Delete the branch once PR is closed.
12+
Say thanks if it's merged.
13+
"""
14+
if event.data["pull_request"]["merged"]:
15+
issue_number = event.data['pull_request']['number']
16+
merged_by = event.data['pull_request']['merged_by']['login']
17+
util.comment_on_pr(issue_number, f"Thanks, @{merged_by}!")
18+
19+
branch_name = event.data['pull_request']['head']['ref']
20+
util.delete_branch(branch_name)
21+

backport/util.py

+15
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,18 @@ def get_participants(created_by, merged_by):
4646
else:
4747
participants = f"@{created_by} and @{merged_by}"
4848
return participants
49+
50+
51+
def delete_branch(branch_name):
52+
"""
53+
Delete the branch on GitHub
54+
"""
55+
request_headers = sansio.create_headers(
56+
"miss-islington",
57+
oauth_token=os.environ.get('GH_AUTH'))
58+
url = f"https://github.com/api/repos/miss-islington/cpython/git/refs/heads/{branch_name}"
59+
response = requests.delete(url, headers=request_headers)
60+
if response.status_code == 204:
61+
print(f"{branch_name} branch deleted.")
62+
else:
63+
print(f"Couldn't delete the branch {branch_name}")

0 commit comments

Comments
 (0)