Skip to content

Commit 1c0b1fc

Browse files
authored
Remove 'Automerge-Triggered-By' text when removing the automerge label (#360)
* Remove 'Automerge-Triggered-By' text when removing the automerge label * Add more tests * Add test case for the scenario that the trailer text does not exist
1 parent 5f46257 commit 1c0b1fc

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed

miss_islington/status_change.py

+19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ async def pr_reviewed(event, gh, *args, **kwargs):
6969
)
7070

7171

72+
@router.register("pull_request", action="unlabeled")
73+
async def pr_unlabeled(event, gh, *args, **kwargs):
74+
label = event.data["label"]["name"]
75+
if label != util.AUTOMERGE_LABEL:
76+
return
77+
78+
sender = event.data["sender"]["login"]
79+
if not await util.is_core_dev(gh, sender):
80+
# Apply the AUTOMERGE label back
81+
issue_url = event.data["pull_request"]["issue_url"]
82+
await gh.post(f"{issue_url}/labels", data={"labels": [util.AUTOMERGE_LABEL]})
83+
return
84+
85+
body = event.data["pull_request"]["body"]
86+
url = event.data["pull_request"]["url"]
87+
new_body = re.sub(rf"{AUTOMERGE_TRAILER}: @(\w|\-)+", "", body)
88+
await gh.patch(url, data={"body": new_body})
89+
90+
7291
async def check_ci_status_and_approval(
7392
gh, sha, pr_for_commit=None, leave_comment=False, is_automerge=False
7493
):

tests/test_status_change.py

+217
Original file line numberDiff line numberDiff line change
@@ -1500,3 +1500,220 @@ async def test_automerge_label_triggered_by_added_to_pr():
15001500
assert gh.patch_data == {
15011501
"body": f"{data['pull_request']['body']}\n\nAutomerge-Triggered-By: @Mariatta"
15021502
}
1503+
1504+
1505+
async def test_automerge_label_removed_by_core_dev():
1506+
sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
1507+
data = {
1508+
"action": "unlabeled",
1509+
"pull_request": {
1510+
"user": {"login": "miss-islington"},
1511+
"labels": [
1512+
{"name": "awaiting merge"},
1513+
{"name": "CLA signed"},
1514+
],
1515+
"head": {"sha": sha},
1516+
"number": 5547,
1517+
"title": "bpo-32720: Fixed the replacement field grammar documentation.",
1518+
"body": "\n\n`arg_name` and `element_index` are defined as `digit`+ instead of `integer`.\n\nAutomerge-Triggered-By: @miss-islington",
1519+
"url": "https://api.github.com/repos/python/cpython/pulls/5547",
1520+
"issue_url": "https://api.github.com/repos/python/cpython/issues/5547",
1521+
},
1522+
"sender": {"login": "miss-islington"},
1523+
"label": {"name": AUTOMERGE_LABEL},
1524+
}
1525+
1526+
event = sansio.Event(data, event="pull_request", delivery_id="1")
1527+
1528+
getitem = {
1529+
f"/repos/python/cpython/commits/{sha}/status": {
1530+
"state": "success",
1531+
"statuses": [
1532+
{
1533+
"state": "success",
1534+
"description": "Issue report skipped",
1535+
"context": "bedevere/issue-number",
1536+
},
1537+
{
1538+
"state": "success",
1539+
"description": "The Travis CI build passed",
1540+
"target_url": "https://travis-ci.org/python/cpython/builds/340259685?utm_source=github_status&utm_medium=notification",
1541+
"context": "continuous-integration/travis-ci/pr",
1542+
},
1543+
],
1544+
},
1545+
"/teams/42/memberships/miss-islington": True,
1546+
}
1547+
1548+
getiter = {
1549+
"/repos/python/cpython/pulls/5547/commits": [{"sha": sha}],
1550+
"/orgs/python/teams": [{"name": "python core", "id": 42}],
1551+
}
1552+
1553+
gh = FakeGH(getitem=getitem, getiter=getiter)
1554+
await status_change.router.dispatch(event, gh)
1555+
assert 'body' in gh.patch_data
1556+
assert 'Automerge-Triggered-By: @miss-islington' not in gh.patch_data['body']
1557+
1558+
1559+
async def test_automerge_label_removed_by_non_core_dev():
1560+
sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
1561+
data = {
1562+
"action": "unlabeled",
1563+
"pull_request": {
1564+
"user": {"login": "miss-islington"},
1565+
"labels": [
1566+
{"name": "awaiting merge"},
1567+
{"name": "CLA signed"},
1568+
],
1569+
"head": {"sha": sha},
1570+
"number": 5547,
1571+
"title": "bpo-32720: Fixed the replacement field grammar documentation.",
1572+
"body": "\n\n`arg_name` and `element_index` are defined as `digit`+ instead of `integer`.\n\nAutomerge-Triggered-By: @miss-islington",
1573+
"url": "https://api.github.com/repos/python/cpython/pulls/5547",
1574+
"issue_url": "https://api.github.com/repos/python/cpython/issues/5547",
1575+
},
1576+
"sender": {"login": "miss-islington"},
1577+
"label": {"name": AUTOMERGE_LABEL},
1578+
}
1579+
1580+
event = sansio.Event(data, event="pull_request", delivery_id="1")
1581+
1582+
getitem = {
1583+
f"/repos/python/cpython/commits/{sha}/status": {
1584+
"state": "success",
1585+
"statuses": [
1586+
{
1587+
"state": "success",
1588+
"description": "Issue report skipped",
1589+
"context": "bedevere/issue-number",
1590+
},
1591+
{
1592+
"state": "success",
1593+
"description": "The Travis CI build passed",
1594+
"target_url": "https://travis-ci.org/python/cpython/builds/340259685?utm_source=github_status&utm_medium=notification",
1595+
"context": "continuous-integration/travis-ci/pr",
1596+
},
1597+
],
1598+
},
1599+
"/teams/42/memberships/miss-islington": gidgethub.BadRequest(
1600+
status_code=http.HTTPStatus(404)
1601+
),
1602+
}
1603+
1604+
getiter = {
1605+
"/repos/python/cpython/pulls/5547/commits": [{"sha": sha}],
1606+
"/orgs/python/teams": [{"name": "python core", "id": 42}],
1607+
}
1608+
1609+
gh = FakeGH(getitem=getitem, getiter=getiter)
1610+
await status_change.router.dispatch(event, gh)
1611+
assert 'labels' in gh.post_data
1612+
assert AUTOMERGE_LABEL in gh.post_data['labels']
1613+
1614+
1615+
async def test_label_other_than_automerge_removed():
1616+
sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
1617+
data = {
1618+
"action": "unlabeled",
1619+
"pull_request": {
1620+
"user": {"login": "miss-islington"},
1621+
"labels": [
1622+
{"name": "awaiting merge"},
1623+
{"name": "CLA signed"},
1624+
],
1625+
"head": {"sha": sha},
1626+
"number": 5547,
1627+
"title": "bpo-32720: Fixed the replacement field grammar documentation.",
1628+
"body": "\n\n`arg_name` and `element_index` are defined as `digit`+ instead of `integer`.\n\nAutomerge-Triggered-By: @miss-islington",
1629+
"url": "https://api.github.com/repos/python/cpython/pulls/5547",
1630+
"issue_url": "https://api.github.com/repos/python/cpython/issues/5547",
1631+
},
1632+
"sender": {"login": "miss-islington"},
1633+
"label": {"name": "needs backport to 3.9"},
1634+
}
1635+
1636+
event = sansio.Event(data, event="pull_request", delivery_id="1")
1637+
1638+
getitem = {
1639+
f"/repos/python/cpython/commits/{sha}/status": {
1640+
"state": "success",
1641+
"statuses": [
1642+
{
1643+
"state": "success",
1644+
"description": "Issue report skipped",
1645+
"context": "bedevere/issue-number",
1646+
},
1647+
{
1648+
"state": "success",
1649+
"description": "The Travis CI build passed",
1650+
"target_url": "https://travis-ci.org/python/cpython/builds/340259685?utm_source=github_status&utm_medium=notification",
1651+
"context": "continuous-integration/travis-ci/pr",
1652+
},
1653+
],
1654+
},
1655+
"/teams/42/memberships/miss-islington": True,
1656+
}
1657+
1658+
getiter = {
1659+
"/repos/python/cpython/pulls/5547/commits": [{"sha": sha}],
1660+
"/orgs/python/teams": [{"name": "python core", "id": 42}],
1661+
}
1662+
1663+
gh = FakeGH(getitem=getitem, getiter=getiter)
1664+
await status_change.router.dispatch(event, gh)
1665+
assert not hasattr(gh, 'put_data')
1666+
assert not hasattr(gh, 'post_data')
1667+
1668+
1669+
async def test_automerge_removed_but_trailer_text_edited_out():
1670+
sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
1671+
data = {
1672+
"action": "unlabeled",
1673+
"pull_request": {
1674+
"user": {"login": "miss-islington"},
1675+
"labels": [
1676+
{"name": "awaiting merge"},
1677+
{"name": "CLA signed"},
1678+
],
1679+
"head": {"sha": sha},
1680+
"number": 5547,
1681+
"title": "bpo-32720: Fixed the replacement field grammar documentation.",
1682+
"body": "\n\n`arg_name` and `element_index` are defined as `digit`+ instead of `integer`.",
1683+
"url": "https://api.github.com/repos/python/cpython/pulls/5547",
1684+
"issue_url": "https://api.github.com/repos/python/cpython/issues/5547",
1685+
},
1686+
"sender": {"login": "miss-islington"},
1687+
"label": {"name": AUTOMERGE_LABEL},
1688+
}
1689+
1690+
event = sansio.Event(data, event="pull_request", delivery_id="1")
1691+
1692+
getitem = {
1693+
f"/repos/python/cpython/commits/{sha}/status": {
1694+
"state": "success",
1695+
"statuses": [
1696+
{
1697+
"state": "success",
1698+
"description": "Issue report skipped",
1699+
"context": "bedevere/issue-number",
1700+
},
1701+
{
1702+
"state": "success",
1703+
"description": "The Travis CI build passed",
1704+
"target_url": "https://travis-ci.org/python/cpython/builds/340259685?utm_source=github_status&utm_medium=notification",
1705+
"context": "continuous-integration/travis-ci/pr",
1706+
},
1707+
],
1708+
},
1709+
"/teams/42/memberships/miss-islington": True,
1710+
}
1711+
1712+
getiter = {
1713+
"/repos/python/cpython/pulls/5547/commits": [{"sha": sha}],
1714+
"/orgs/python/teams": [{"name": "python core", "id": 42}],
1715+
}
1716+
1717+
gh = FakeGH(getitem=getitem, getiter=getiter)
1718+
await status_change.router.dispatch(event, gh)
1719+
assert gh.patch_data["body"] == data["pull_request"]["body"]

0 commit comments

Comments
 (0)