Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f08664

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

File tree

2 files changed

+36
-0
lines changed
Filter options

2 files changed

+36
-0
lines changed

‎backport/delete_branch.py

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: backport/util.py
+15Lines changed: 15 additions & 0 deletions
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://api.github.com/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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.