Skip to content

Commit 0f350ce

Browse files
authored
gh-554: Handle Draft PRs (#555)
Don't add "awaiting" labels to Draft PR, only add them when PR is published (not draft anymore) Remove "awaiting" labels again when PR becomes unpublished. Closes #554
1 parent be4fece commit 0f350ce

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ If no GitHub issue number is found the status fails and the
1111
"Details" link points to the relevant
1212
[section of the devguide](https://devguide.python.org/getting-started/pull-request-lifecycle.html#submitting).
1313
- ### Links to GitHub issues
14-
If an issue number is found then the "Details" link points to the relevant issue
14+
If an issue number is found then the "Details" link points to the relevant issue
1515
itself, making it easier to navigate from PR to issue.
1616
- ### Identifies missing news entry
17-
If no `Misc/NEWS.d` entry is found or the news entry is formatted incorrectly
18-
and the issue doesn't have the `skip news` label, the status fails and a relevant
17+
If no `Misc/NEWS.d` entry is found or the news entry is formatted incorrectly
18+
and the issue doesn't have the `skip news` label, the status fails and a relevant
1919
description label is added to it.
2020
- ### Closes invalid PRs
21-
Closes PRs that try to merge a maintenance branch into the main branch, adds
21+
Closes PRs that try to merge a maintenance branch into the main branch, adds
2222
`invalid` label, and posts a relevant message.
2323
- ### Labels PRs for docs
2424
Labels PRs for documentation as `type-documentation`
@@ -43,7 +43,9 @@ blocking the PR from moving forward:
4343

4444
```mermaid
4545
flowchart TD
46-
A([New PR]):::creator
46+
A([Published PR]):::creator
47+
A_draft([New Draft PR]):::creator
48+
A_draft -- publish draft by contributor --> A:::creator
4749
A -- by contributor --> B[Awaiting review]:::anyone
4850
A -- by core dev --> C[Awaiting core review]:::coredev
4951
B & C -- new review by\nanother contributor --> C

bedevere/stage.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ async def opened_pr(event, gh, *arg, **kwargs):
109109
"awaiting review".
110110
"""
111111
pull_request = event.data["pull_request"]
112+
if pull_request.get("draft"):
113+
return
112114
issue = await util.issue_for_PR(gh, pull_request)
113115
username = util.user_login(pull_request)
114116
if await util.is_core_dev(gh, username):
@@ -117,6 +119,18 @@ async def opened_pr(event, gh, *arg, **kwargs):
117119
await stage(gh, issue, Blocker.review)
118120

119121

122+
@router.register("pull_request", action="edited")
123+
async def edited_pr(event, gh, *arg, **kwargs):
124+
pull_request = event.data["pull_request"]
125+
issue = await util.issue_for_PR(gh, pull_request)
126+
username = util.user_login(pull_request)
127+
if pull_request.get("draft"):
128+
await _remove_stage_labels(gh, issue)
129+
else:
130+
blocked_on = Blocker.core_review if await util.is_core_dev(gh, username) else Blocker.review
131+
await stage(gh, issue, blocked_on)
132+
133+
120134
@router.register("push")
121135
async def new_commit_pushed(event, gh, *arg, **kwargs):
122136
"""If there is a new commit pushed to the PR branch that is in `awaiting merge` state,

tests/test_stage.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,74 @@ async def test_stage():
7272
assert post_[1] == [awaiting.Blocker.merge.value]
7373

7474

75+
async def test_opened_draft_pr():
76+
# New Draft PR from a core dev.
77+
username = "brettcannon"
78+
issue_url = "https://api.github.com/issue/42"
79+
data = {
80+
"action": "opened",
81+
"pull_request": {
82+
"user": {
83+
"login": username,
84+
},
85+
"issue_url": issue_url,
86+
"draft": True,
87+
},
88+
}
89+
event = sansio.Event(data, event="pull_request", delivery_id="12345")
90+
teams = [{"name": "python core", "id": 6}]
91+
items = {
92+
f"https://api.github.com/teams/6/memberships/{username}": "OK",
93+
issue_url: {"labels": [], "labels_url": "https://api.github.com/labels"},
94+
}
95+
gh = FakeGH(
96+
getiter={"https://api.github.com/orgs/python/teams": teams}, getitem=items
97+
)
98+
await awaiting.router.dispatch(event, gh)
99+
assert len(gh.post_) == 0
100+
101+
# Draft PR is published
102+
data["action"] = "edited"
103+
data["pull_request"]["draft"] = False
104+
event = sansio.Event(data, event="pull_request", delivery_id="12345")
105+
gh = FakeGH(
106+
getiter={"https://api.github.com/orgs/python/teams": teams}, getitem=items
107+
)
108+
await awaiting.router.dispatch(event, gh)
109+
assert len(gh.post_) == 1
110+
post_ = gh.post_[0]
111+
assert post_[0] == "https://api.github.com/labels"
112+
assert post_[1] == [awaiting.Blocker.core_review.value]
113+
114+
# Published PR is unpublished (set back to Draft)
115+
data["action"] = "edited"
116+
data["pull_request"]["draft"] = True
117+
encoded_label = "awaiting%20core%20review"
118+
items[issue_url] = {
119+
"labels": [
120+
{
121+
"url": f"https://api.github.com/repos/python/cpython/labels/{encoded_label}",
122+
"name": "awaiting core review",
123+
},
124+
{
125+
"url": "https://api.github.com/repos/python/cpython/labels/CLA%20signed",
126+
"name": "CLA signed",
127+
},
128+
],
129+
"labels_url": "https://api.github.com/repos/python/cpython/issues/12345/labels{/name}",
130+
}
131+
event = sansio.Event(data, event="pull_request", delivery_id="12345")
132+
gh = FakeGH(
133+
getiter={"https://api.github.com/orgs/python/teams": teams}, getitem=items
134+
)
135+
await awaiting.router.dispatch(event, gh)
136+
assert len(gh.post_) == 0
137+
assert (
138+
gh.delete_url ==
139+
f"https://api.github.com/repos/python/cpython/issues/12345/labels/{encoded_label}"
140+
)
141+
142+
75143
async def test_opened_pr():
76144
# New PR from a core dev.
77145
username = "brettcannon"
@@ -110,6 +178,7 @@ async def test_opened_pr():
110178
"login": username,
111179
},
112180
"issue_url": issue_url,
181+
"draft": False,
113182
},
114183
}
115184
event = sansio.Event(data, event="pull_request", delivery_id="12345")

0 commit comments

Comments
 (0)