Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions bedevere/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,25 @@ class Blocker(enum.Enum):
changes = f"{LABEL_PREFIX} changes"
change_review = f"{LABEL_PREFIX} change review"
merge = f"{LABEL_PREFIX} merge"


async def stage(gh, issue, blocked_on):
"""Remove any "awaiting" labels and apply the specified one."""
label_name = blocked_on.value
if any(label_name == label["name"] for label in issue["labels"]):
return


async def _remove_stage_labels(gh, issue):
"""Remove all "awaiting" labels."""
# There's no reason to expect there to be multiple "awaiting" labels on a
# single pull request, but just in case there are we might as well clean
# up the situation when we come across it.
for label in issue["labels"]:
stale_name = label["name"]
if stale_name.startswith(LABEL_PREFIX + " "):
await gh.delete(issue["labels_url"], {"name": stale_name})


async def stage(gh, issue, blocked_on):
"""Remove any "awaiting" labels and apply the specified one."""
label_name = blocked_on.value
if any(label_name == label["name"] for label in issue["labels"]):
return
await _remove_stage_labels(gh, issue)
await gh.post(issue["labels_url"], data=[label_name])


Expand Down Expand Up @@ -196,3 +201,11 @@ async def new_comment(event, gh, *args, **kwargs):
async for core_dev in core_dev_reviewers(gh, pr_url)})
comment = CHANGE_REVIEW_REQUESTED.format(core_devs=core_devs)
await gh.post(issue["comments_url"], data={"body": comment})


@router.register("pull_request", action="closed")
async def closed_pr(event, gh, *args, **kwargs):
"""Remove all `awaiting ... ` labels when a PR is merged."""
if event.data["pull_request"]["merged"]:
issue = await util.issue_for_PR(gh, event.data["pull_request"])
await _remove_stage_labels(gh, issue)
4 changes: 2 additions & 2 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ async def test_success(test_client):
app = web.Application()
app.router.add_post("/", main.main)
client = await test_client(app)
headers = {"x-github-event": "pull_request",
headers = {"x-github-event": "project",
"x-github-delivery": "1234"}
# Sending a payload that shouldn't trigger any networking, but no errors
# either.
data = {"action": "closed"}
data = {"action": "created"}
response = await client.post("/", headers=headers, json=data)
assert response.status == 200

Expand Down
64 changes: 64 additions & 0 deletions tests/test_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,67 @@ async def test_new_comment():
assert "@brettcannon" in comment_body
assert "@gvanrossum" in comment_body
assert "not-core-dev" not in comment_body


async def test_awaiting_labels_removed_when_pr_merged():
issue_url = "https://api.github.com/repos/org/proj/issues/3749"
data = {
"action": "closed",
"pull_request": {
"merged": True,
"issue_url": issue_url,
}
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")

issue_data = {
issue_url: {
"labels": [
{"url": "https://api.github.com/repos/python/cpython/labels/awaiting%20merge",
"name": "awaiting merge",
},
{
"url": "https://api.github.com/repos/python/cpython/labels/CLA%20signed",
"name": "CLA signed",
},
],
"labels_url": "https://api.github.com/repos/python/cpython/issues/12345/labels{/name}",
},
}

gh = FakeGH(getitem=issue_data)

await awaiting.router.dispatch(event, gh)
assert gh.delete_url == "https://api.github.com/repos/python/cpython/issues/12345/labels/awaiting%20merge"


async def test_awaiting_labels_not_removed_when_pr_not_merged():
issue_url = "https://api.github.com/repos/org/proj/issues/3749"
data = {
"action": "closed",
"pull_request": {
"merged": False,
"issue_url": issue_url,
}
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")

issue_data = {
issue_url: {
"labels": [
{"url": "https://api.github.com/repos/python/cpython/labels/awaiting%20merge",
"name": "awaiting merge",
},
{
"url": "https://api.github.com/repos/python/cpython/labels/CLA%20signed",
"name": "CLA signed",
},
],
"labels_url": "https://api.github.com/repos/python/cpython/issues/12345/labels{/name}",
},
}

gh = FakeGH(getitem=issue_data)

await awaiting.router.dispatch(event, gh)
assert gh.delete_url is None