Skip to content

Commit ad03d53

Browse files
ryanlong1004JelleZijlstraezio-melotti
authored
gh-428: Add GitHub issue to description of PR (#430)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
1 parent aba911f commit ad03d53

File tree

4 files changed

+192
-22
lines changed

4 files changed

+192
-22
lines changed

bedevere/util.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33

44
import gidgethub
55

6+
7+
DEFAULT_BODY = ""
8+
TAG_NAME = "gh-issue-number"
69
NEWS_NEXT_DIR = "Misc/NEWS.d/next/"
10+
CLOSING_TAG = f"<!-- /{TAG_NAME} -->"
11+
BODY = f"""\
12+
{{body}}
13+
14+
<!-- {TAG_NAME}: gh-{{issue_number}} -->
15+
* gh-{{issue_number}}
16+
{CLOSING_TAG}
17+
"""
718

819

920
@enum.unique
@@ -67,7 +78,10 @@ async def files_for_PR(gh, pull_request):
6778
data = []
6879
async for filedata in gh.getiter(files_url): # pragma: no branch
6980
data.append(
70-
{"file_name": filedata["filename"], "patch": filedata.get("patch", ""),}
81+
{
82+
"file_name": filedata["filename"],
83+
"patch": filedata.get("patch", ""),
84+
}
7185
)
7286
return data
7387

@@ -77,6 +91,24 @@ async def issue_for_PR(gh, pull_request):
7791
return await gh.getitem(pull_request["issue_url"])
7892

7993

94+
async def patch_body(gh, pull_request, issue_number):
95+
"""Updates the description of a PR with the gh issue number if it exists.
96+
97+
returns if body exists with issue_number
98+
"""
99+
if "body" not in pull_request or pull_request["body"] is None:
100+
return await gh.patch(
101+
pull_request["url"],
102+
data=BODY.format(body=DEFAULT_BODY, issue_number=issue_number),
103+
)
104+
if f"GH-{issue_number}\n" not in pull_request["body"]:
105+
return await gh.patch(
106+
pull_request["url"],
107+
data=BODY.format(body=pull_request["body"], issue_number=issue_number),
108+
)
109+
return
110+
111+
80112
async def is_core_dev(gh, username):
81113
"""Check if the user is a CPython core developer."""
82114
org_teams = "/orgs/python/teams"

tests/test_gh_issue.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
class FakeGH:
1414

15-
def __init__(self, *, getitem=None, post=None):
15+
def __init__(self, *, getitem=None, post=None, patch=None):
1616
self._getitem_return = getitem
1717
self._post_return = post
18+
self._patch_return = patch
1819
self.post_url = []
1920
self.post_data = []
21+
self.patch_url = []
22+
self.patch_data = []
2023

2124
async def getitem(self, url):
2225
if isinstance(self._getitem_return, Exception):
@@ -28,7 +31,6 @@ async def post(self, url, *, data):
2831
self.post_data.append(data)
2932
return self._post_return
3033

31-
3234
@pytest.mark.asyncio
3335
@pytest.mark.parametrize("action", ["opened", "synchronize", "reopened"])
3436
async def test_set_status_failure(action, monkeypatch):
@@ -40,6 +42,7 @@ async def test_set_status_failure(action, monkeypatch):
4042
"statuses_url": "https://api.github.com/blah/blah/git-sha",
4143
"title": "No issue in title",
4244
"issue_url": "issue URL",
45+
"url": "url",
4346
},
4447
}
4548
issue_data = {
@@ -68,6 +71,8 @@ async def test_set_status_failure_via_issue_not_found_on_github(action, monkeypa
6871
"pull_request": {
6972
"statuses_url": "https://api.github.com/blah/blah/git-sha",
7073
"title": "gh-123: Invalid issue number",
74+
"issue_url": "issue URL",
75+
"url": "url",
7176
},
7277
}
7378
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -88,7 +93,9 @@ async def test_set_status_success_issue_found_on_bpo(action):
8893
"action": action,
8994
"pull_request": {
9095
"statuses_url": "https://api.github.com/blah/blah/git-sha",
91-
"title": "bpo-12345: an issue!",
96+
"title": "bpo-12345: An issue on b.p.o",
97+
"issue_url": "issue URL",
98+
"url": "url",
9299
},
93100
}
94101
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -112,6 +119,7 @@ async def test_set_status_success(action, monkeypatch):
112119
"action": action,
113120
"pull_request": {
114121
"statuses_url": "https://api.github.com/blah/blah/git-sha",
122+
"url": "",
115123
"title": "[3.6] gh-1234: an issue!",
116124
},
117125
}
@@ -137,6 +145,7 @@ async def test_set_status_success_issue_found_on_gh(action, monkeypatch):
137145
"pull_request": {
138146
"statuses_url": "https://api.github.com/blah/blah/git-sha",
139147
"title": "gh-12345: an issue!",
148+
"url": "url",
140149
},
141150
}
142151
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -161,6 +170,7 @@ async def test_set_status_success_issue_found_on_gh_ignore_case(action, monkeypa
161170
"pull_request": {
162171
"statuses_url": "https://api.github.com/blah/blah/git-sha",
163172
"title": "GH-12345: an issue!",
173+
"url": "url",
164174
},
165175
}
166176
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -186,6 +196,7 @@ async def test_set_status_success_via_skip_issue_label(action, monkeypatch):
186196
"statuses_url": "https://api.github.com/blah/blah/git-sha",
187197
"title": "No issue in title",
188198
"issue_url": "issue URL",
199+
"url": "url",
189200
},
190201
}
191202
issue_data = {
@@ -211,6 +222,7 @@ async def test_edit_title(monkeypatch):
211222
"pull_request": {
212223
"statuses_url": "https://api.github.com/blah/blah/git-sha",
213224
"title": "gh-1234: an issue!",
225+
"url": "url",
214226
},
215227
"action": "edited",
216228
"changes": {"title": "thingy"},
@@ -251,6 +263,7 @@ async def test_edit_other_than_title(monkeypatch):
251263
"pull_request": {
252264
"statuses_url": "https://api.github.com/blah/blah/git-sha",
253265
"title": "bpo-1234: an issue!",
266+
"url": "url",
254267
},
255268
"action": "edited",
256269
"changes": {"stuff": "thingy"},
@@ -270,6 +283,7 @@ async def test_new_label_skip_issue_no_issue():
270283
"pull_request": {
271284
"statuses_url": "https://api.github.com/blah/blah/git-sha",
272285
"title": "An easy fix",
286+
"url": "url",
273287
},
274288
}
275289
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -287,6 +301,7 @@ async def test_new_label_skip_issue_with_issue_number():
287301
"pull_request": {
288302
"statuses_url": "https://api.github.com/blah/blah/git-sha",
289303
"title": "Revert gh-1234: revert an easy fix",
304+
"url": "url",
290305
},
291306
}
292307
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -308,6 +323,7 @@ async def test_new_label_skip_issue_with_issue_number_ignore_case():
308323
"pull_request": {
309324
"statuses_url": "https://api.github.com/blah/blah/git-sha",
310325
"title": "Revert Gh-1234: revert an easy fix",
326+
"url": "url",
311327
},
312328
}
313329
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -327,6 +343,7 @@ async def test_new_label_not_skip_issue():
327343
"label": {"name": "non-trivial"},
328344
"pull_request": {
329345
"statuses_url": "https://api.github.com/blah/blah/git-sha",
346+
"url": "url",
330347
},
331348
}
332349
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -347,6 +364,7 @@ async def test_removed_label_from_label_deletion(monkeypatch):
347364
"pull_request": {
348365
"statuses_url": "https://api.github.com/blah/blah/git-sha",
349366
"title": "gh-1234: an issue!",
367+
"url": "url",
350368
},
351369
}
352370
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -366,6 +384,7 @@ async def test_removed_label_skip_issue(monkeypatch):
366384
"pull_request": {
367385
"statuses_url": "https://api.github.com/blah/blah/git-sha",
368386
"title": "gh-1234: an issue!",
387+
"url": "url",
369388
},
370389
}
371390
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -389,6 +408,7 @@ async def test_removed_label_non_skip_issue(monkeypatch):
389408
"label": {"name": "non-trivial"},
390409
"pull_request": {
391410
"statuses_url": "https://api.github.com/blah/blah/git-sha",
411+
"url": "url",
392412
},
393413
}
394414
event = sansio.Event(data, event="pull_request", delivery_id="12345")
@@ -422,7 +442,7 @@ async def test_validate_issue_number_is_pr_on_github():
422442

423443
gh = FakeGH(getitem={
424444
"number": 123,
425-
"pull_request": {"html_url": "https://github.com/python/cpython/pull/123"}
445+
"pull_request": {"html_url": "https://github.com/python/cpython/pull/123", "url": "url",}
426446
})
427447
async with aiohttp.ClientSession() as session:
428448
response = await gh_issue._validate_issue_number(gh, 123, session=session)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy