Skip to content

Commit 5fc67ce

Browse files
authored
Leave a comment if failed to merge PR. (#191)
Closes python/miss-islington#188
1 parent d5c2f8f commit 5fc67ce

File tree

2 files changed

+97
-21
lines changed

2 files changed

+97
-21
lines changed

miss_islington/status_change.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import re
22

3+
import gidgethub
4+
35
from gidgethub import routing
46

57
from . import util
@@ -107,29 +109,28 @@ async def merge_pr(gh, pr, sha, is_automerge=False):
107109
pr_number = pr["number"]
108110
async for commit in gh.getiter(f"/repos/python/cpython/pulls/{pr_number}/commits"):
109111
if commit["sha"] == sha:
112+
pr_title = ""
113+
pr_commit_msg = ""
114+
110115
if is_automerge:
111116
pr_commit_msg = util.normalize_message(pr["body"])
112117
pr_title = f"{pr['title']} (GH-{pr_number})"
113-
await gh.put(
114-
f"/repos/python/cpython/pulls/{pr_number}/merge",
115-
data={
116-
"commit_title": pr_title,
117-
"commit_message": pr_commit_msg,
118-
"sha": sha,
119-
"merge_method": "squash",
120-
},
121-
)
122118
else:
123119
pr_commit_msg = commit["commit"]["message"].split("\n")
124-
125-
cleaned_up_title = f"{pr_commit_msg[0]}"
120+
pr_title = f"{pr_commit_msg[0]}"
121+
122+
data = {
123+
"commit_title": pr_title,
124+
"commit_message": pr_commit_msg,
125+
"sha": sha,
126+
"merge_method": "squash",
127+
}
128+
try:
126129
await gh.put(
127-
f"/repos/python/cpython/pulls/{pr_number}/merge",
128-
data={
129-
"commit_title": cleaned_up_title,
130-
"commit_message": "\n".join(pr_commit_msg[1:]),
131-
"sha": sha,
132-
"merge_method": "squash",
133-
},
130+
f"/repos/python/cpython/pulls/{pr_number}/merge", data=data
131+
)
132+
except gidgethub.BadRequest as err:
133+
await util.comment_on_pr(
134+
gh, pr_number, f"Sorry, I can't merge this PR. Reason: `{err}`."
134135
)
135136
break

tests/test_status_change.py

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99

1010
class FakeGH:
11-
def __init__(self, *, getitem=None, getiter=None, put=None):
11+
def __init__(self, *, getitem=None, getiter=None, put=None, post=None):
1212
self._getitem_return = getitem
1313
self._getiter_return = getiter
1414
self.getitem_url = None
1515
self.getiter_url = None
1616
self._put_return = put
17-
self._post_return = put
17+
self._post_return = post
1818

1919
async def getitem(self, url):
2020
self.getitem_url = url
@@ -30,7 +30,11 @@ async def getiter(self, url):
3030
async def put(self, url, *, data):
3131
self.put_url = url
3232
self.put_data = data
33-
return self._put_return
33+
to_return = self._put_return
34+
if isinstance(to_return, Exception):
35+
raise to_return
36+
else:
37+
return to_return
3438

3539
async def post(self, url, *, data):
3640
self.post_url = url
@@ -1217,3 +1221,74 @@ async def test_automerge_commit_not_found():
12171221
await status_change.router.dispatch(event, gh)
12181222
assert not hasattr(gh, "post_data") # does not leave a comment
12191223
assert not hasattr(gh, "put_data") # does not merge
1224+
1225+
1226+
async def test_automerge_failed():
1227+
sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
1228+
data = {
1229+
"action": "labeled",
1230+
"pull_request": {
1231+
"user": {"login": "Mariatta"},
1232+
"labels": [
1233+
{"name": "awaiting merge"},
1234+
{"name": AUTOMERGE_LABEL},
1235+
{"name": "CLA signed"},
1236+
],
1237+
"head": {"sha": sha},
1238+
"number": 5547,
1239+
"title": "bpo-32720: Fixed the replacement field grammar documentation.",
1240+
"body": "\n\n`arg_name` and `element_index` are defined as `digit`+ instead of `integer`.",
1241+
},
1242+
}
1243+
1244+
event = sansio.Event(data, event="pull_request", delivery_id="1")
1245+
1246+
getitem = {
1247+
f"/repos/python/cpython/commits/{sha}/status": {
1248+
"state": "success",
1249+
"statuses": [
1250+
{
1251+
"state": "success",
1252+
"description": "Issue report skipped",
1253+
"context": "bedevere/issue-number",
1254+
},
1255+
{
1256+
"state": "success",
1257+
"description": "The Travis CI build passed",
1258+
"target_url": "https://travis-ci.org/python/cpython/builds/340259685?utm_source=github_status&utm_medium=notification",
1259+
"context": "continuous-integration/travis-ci/pr",
1260+
},
1261+
],
1262+
}
1263+
}
1264+
1265+
getiter = {
1266+
"/repos/python/cpython/pulls/5547/commits": [
1267+
{"sha": "5f007046b5d4766f971272a0cc99f8461215c1ec"},
1268+
{"sha": sha},
1269+
]
1270+
}
1271+
1272+
gh = FakeGH(
1273+
getitem=getitem,
1274+
getiter=getiter,
1275+
put=gidgethub.BadRequest(status_code=http.HTTPStatus(400)),
1276+
post={
1277+
"html_url": f"https://github.com/python/cpython/pull/5547#issuecomment-401309376"
1278+
},
1279+
)
1280+
1281+
await status_change.router.dispatch(event, gh)
1282+
1283+
assert gh.put_data["sha"] == sha
1284+
assert gh.put_data["merge_method"] == "squash"
1285+
assert (
1286+
gh.put_data["commit_title"]
1287+
== "bpo-32720: Fixed the replacement field grammar documentation. (GH-5547)"
1288+
)
1289+
assert (
1290+
gh.put_data["commit_message"]
1291+
== "\n\n`arg_name` and `element_index` are defined as `digit`+ instead of `integer`."
1292+
)
1293+
1294+
assert "Sorry, I can't merge this PR" in gh.post_data["body"]

0 commit comments

Comments
 (0)