Skip to content

Commit 78c96f2

Browse files
Auto add the skip news label to docs PRs (#485)
* Remove unused import * Refactor ``Category`` to be ``Labels`` * Rename from ``documentation`` to ``docs`` * Rename label * Update comment * Auto add the ``skip news`` label to ``docs`` PRs Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com> * Apply suggestions from code review Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com> * Fix test * Update tests and coverage * Remove auto-formatting by black * Update bedevere/util.py * Update bedevere/util.py Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com> Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
1 parent 83a2d22 commit 78c96f2

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

bedevere/prtype.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ class Labels(enum.Enum):
1717
performance = "performance"
1818
type_security = f"{TYPE_LABEL_PREFIX}-security"
1919
tests = "tests"
20+
skip_news = "skip news"
2021

2122

22-
async def add_label(gh, issue, label):
23-
"""Apply this type label if there aren't any type labels on the PR."""
24-
if any(label.startswith("type") for label in util.labels(issue)):
25-
return
26-
await gh.post(issue["labels_url"], data=[label.value])
23+
async def add_labels(gh, issue, labels):
24+
"""Add the specified labels to the PR."""
25+
current_labels = util.labels(issue)
26+
label_names = [c.value for c in labels if c.value not in current_labels]
27+
if label_names:
28+
await gh.post(issue["labels_url"], data=label_names)
2729

2830

2931
async def classify_by_filepaths(gh, pull_request, filenames):
@@ -35,10 +37,10 @@ async def classify_by_filepaths(gh, pull_request, filenames):
3537
The routing is handled by the filepaths module.
3638
"""
3739
issue = await util.issue_for_PR(gh, pull_request)
38-
docs = tests = False
40+
news = docs = tests = False
3941
for filename in filenames:
4042
if util.is_news_dir(filename):
41-
continue
43+
news = True
4244
filepath = pathlib.PurePath(filename)
4345
if filepath.suffix == '.rst':
4446
docs = True
@@ -47,7 +49,10 @@ async def classify_by_filepaths(gh, pull_request, filenames):
4749
else:
4850
return
4951
if tests:
50-
await add_label(gh, issue, Labels.tests)
52+
await add_labels(gh, issue, [Labels.tests])
5153
elif docs:
52-
await add_label(gh, issue, Labels.docs)
54+
if news:
55+
await add_labels(gh, issue, [Labels.docs])
56+
else:
57+
await add_labels(gh, issue, [Labels.docs, Labels.skip_news])
5358
return

bedevere/util.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ async def files_for_PR(gh, pull_request):
8888

8989

9090
async def issue_for_PR(gh, pull_request):
91-
"""Get the issue data for a pull request."""
91+
"""Return a dict with data about the given PR."""
92+
# "issue_url" is the API endpoint for the given pull_request (despite the name)
9293
return await gh.getitem(pull_request["issue_url"])
9394

9495

tests/test_filepaths.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def test_docs_only(author_association):
8888
check_n_pop_nonews_events(gh, author_association == 'NONE')
8989
assert len(gh.post_url) == 1
9090
assert gh.post_url[0] == 'https://api.github.com/some/label'
91-
assert gh.post_data[0] == [Labels.docs.value]
91+
assert gh.post_data[0] == [Labels.docs.value, Labels.skip_news.value]
9292

9393

9494
@pytest.mark.parametrize('author_association', ['OWNER', 'MEMBER', 'CONTRIBUTOR', 'NONE'])

tests/test_prtype.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async def test_docs_no_news():
8585
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
8686
assert len(gh.post_url) == 1
8787
assert gh.post_url[0] == 'https://api.github.com/some/label'
88-
assert gh.post_data[0] == [Labels.docs.value]
88+
assert gh.post_data[0] == [Labels.docs.value, Labels.skip_news.value]
8989

9090

9191
async def test_docs_and_news():
@@ -154,7 +154,7 @@ async def test_docs_and_tests():
154154

155155
async def test_leave_existing_type_labels():
156156
filenames = {'/path/to/docs.rst', 'test_docs2.py'}
157-
issue = {'labels': [{'name': 'skip news'}, {'name': 'type-documentation'}],
157+
issue = {'labels': [{'name': 'skip news'}, {'name': 'docs'}],
158158
'labels_url': 'https://api.github.com/some/label'}
159159
gh = FakeGH(getiter=filenames, getitem=issue)
160160
event_data = {
@@ -168,7 +168,29 @@ async def test_leave_existing_type_labels():
168168
}
169169
await prtype.classify_by_filepaths(gh, event_data['pull_request'], filenames)
170170
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
171-
# Only creates type-tests label.
171+
assert len(gh.post_url) == 1
172+
assert gh.post_url[0] == "https://api.github.com/some/label"
173+
# This should only add the tests label as the docs label is already applied
174+
assert gh.post_data[0] == [Labels.tests.value]
175+
176+
177+
async def test_do_not_post_if_nothing_to_apply():
178+
filenames = {'/path/to/docs.rst'}
179+
issue = {'labels': [{'name': 'skip news'}, {'name': 'docs'}],
180+
'labels_url': 'https://api.github.com/some/label'}
181+
gh = FakeGH(getiter=filenames, getitem=issue)
182+
event_data = {
183+
'action': 'opened',
184+
'number': 1234,
185+
'pull_request': {
186+
'url': 'https://api.github.com/repos/cpython/python/pulls/1234',
187+
'statuses_url': 'https://api.github.com/some/status',
188+
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
189+
},
190+
}
191+
await prtype.classify_by_filepaths(gh, event_data['pull_request'], filenames)
192+
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
193+
# This should not post anything as docs is already applied
172194
assert len(gh.post_url) == 0
173195

174196

0 commit comments

Comments
 (0)