Skip to content

Commit 30c4a00

Browse files
JackenmenJelleZijlstraMariatta
authored
Don't immediately delete PR's branch in case it's close&reopen (#483)
* Don't immediately delete PR's branch in case it's close&reopen * Add missing import * Add test Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Mariatta Wijaya <[email protected]>
1 parent 8569d62 commit 30c4a00

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

miss_islington/delete_branch.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
13
import gidgethub.routing
24

35
router = gidgethub.routing.Router()
@@ -11,4 +13,12 @@ async def delete_branch(event, gh, *args, **kwargs):
1113
if event.data["pull_request"]["user"]["login"] == "miss-islington":
1214
branch_name = event.data["pull_request"]["head"]["ref"]
1315
url = f"/repos/miss-islington/cpython/git/refs/heads/{branch_name}"
14-
await gh.delete(url)
16+
if event.data["pull_request"]["merged"]:
17+
await gh.delete(url)
18+
else:
19+
# this is delayed to ensure that the bot doesn't remove the branch
20+
# if PR was closed and reopened to rerun checks (or similar)
21+
await asyncio.sleep(60)
22+
updated_data = await gh.getitem(event.data["pull_request"]["url"])
23+
if updated_data["state"] == "closed":
24+
await gh.delete(url)

tests/test_delete_branch.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1+
import asyncio
2+
13
from gidgethub import sansio
24

35
from miss_islington import delete_branch
46

57

68
class FakeGH:
7-
def __init__(self):
9+
def __init__(self, *, getitem=None):
10+
self._getitem_return = getitem
11+
self.getitem_url = None
812
self.post_data = None
913

14+
async def getitem(self, url):
15+
self.getitem_url = url
16+
to_return = self._getitem_return[self.getitem_url]
17+
return to_return
18+
1019
async def delete(self, url):
1120
self.delete_url = url
1221

1322

23+
async def noop_sleep(delay, result=None):
24+
pass
25+
26+
1427
async def test_branch_deleted_when_pr_merged():
1528
data = {
1629
"action": "closed",
@@ -77,7 +90,7 @@ async def test_branch_deleted_and_thanks():
7790
)
7891

7992

80-
async def test_branch_deleted_when_pr_closed():
93+
async def test_branch_deleted_when_pr_closed(monkeypatch):
8194
data = {
8295
"action": "closed",
8396
"pull_request": {
@@ -86,11 +99,16 @@ async def test_branch_deleted_when_pr_closed():
8699
"merged": False,
87100
"merged_by": {"login": None},
88101
"head": {"ref": "backport-17ab8f0-3.7"},
102+
"url": "https://api.github.com/repos/python/cpython/pulls/5722",
89103
},
90104
}
91105
event = sansio.Event(data, event="pull_request", delivery_id="1")
106+
getitem = {
107+
"https://api.github.com/repos/python/cpython/pulls/5722": {"state": "closed"},
108+
}
92109

93-
gh = FakeGH()
110+
monkeypatch.setattr(asyncio, "sleep", noop_sleep)
111+
gh = FakeGH(getitem=getitem)
94112
await delete_branch.router.dispatch(event, gh)
95113
assert gh.post_data is None # does not leave a comment
96114
assert (
@@ -99,6 +117,30 @@ async def test_branch_deleted_when_pr_closed():
99117
)
100118

101119

120+
async def test_branch_not_deleted_when_pr_closed_and_reopened(monkeypatch):
121+
data = {
122+
"action": "closed",
123+
"pull_request": {
124+
"number": 5722,
125+
"user": {"login": "miss-islington"},
126+
"merged": False,
127+
"merged_by": {"login": None},
128+
"head": {"ref": "backport-17ab8f0-3.7"},
129+
"url": "https://api.github.com/repos/python/cpython/pulls/5722",
130+
},
131+
}
132+
event = sansio.Event(data, event="pull_request", delivery_id="1")
133+
getitem = {
134+
"https://api.github.com/repos/python/cpython/pulls/5722": {"state": "opened"},
135+
}
136+
137+
monkeypatch.setattr(asyncio, "sleep", noop_sleep)
138+
gh = FakeGH(getitem=getitem)
139+
await delete_branch.router.dispatch(event, gh)
140+
assert gh.post_data is None # does not leave a comment
141+
assert not hasattr(gh, "delete_url")
142+
143+
102144
async def test_ignore_non_miss_islingtons_prs():
103145
data = {
104146
"action": "closed",

0 commit comments

Comments
 (0)