From 5da855c4e606d2f5a1fa79de9fe7f5bb9bb7c9c7 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 08:19:26 -0800 Subject: [PATCH 01/12] Allow adding blurb for anyone, if logged in user is core-dev. --- blurb_it/__main__.py | 8 +++++ blurb_it/util.py | 25 +++++++++++++++ tests/test_util.py | 75 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 9587a5d..d6a556a 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -159,6 +159,14 @@ async def handle_add_blurb_post(request): "path": path, "message": "📜🤖 Added by blurb_it.", } + if pr["user"]["login"] != session_context["username"]: + is_core_dev = util.is_core_dev(gh, session_context["username"]) + if is_core_dev: + gh = GitHubAPI( + session, + "miss-islington", + oauth_token=os.getenv("MI_GH_AUTH"), + ) try: response = await gh.put( f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data diff --git a/blurb_it/util.py b/blurb_it/util.py index 6d82959..15ef0ef 100644 --- a/blurb_it/util.py +++ b/blurb_it/util.py @@ -3,6 +3,8 @@ import time import jwt +import gidgethub + from aiohttp_session import get_session from blurb_it import error @@ -80,3 +82,26 @@ async def get_installation_access_token(gh, jwt, installation_id): # } return response + + +async def is_core_dev(gh, username): + """Check if the user is a CPython core developer.""" + org_teams = "/orgs/python/teams" + team_name = "python core" + async for team in gh.getiter(org_teams): + if team["name"].lower() == team_name: + break + else: + raise ValueError(f"{team_name!r} not found at {org_teams!r}") + # The 'teams' object only provides a URL to a deprecated endpoint, + # so manually construct the URL to the non-deprecated team membership + # endpoint. + membership_url = f"/teams/{team['id']}/memberships/{username}" + try: + await gh.getitem(membership_url) + except gidgethub.BadRequest as exc: + if exc.status_code == 404: + return False + raise + else: + return True \ No newline at end of file diff --git a/tests/test_util.py b/tests/test_util.py index 95a52cb..220b533 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,5 +1,48 @@ +import http +import pytest +import gidgethub + + from blurb_it import util +class FakeGH: + def __init__(self, *, getiter=None, getitem=None, post=None, patch=None): + self._getitem_return = getitem + self._getiter_return = getiter + self._post_return = post + self._patch_return = patch + self.getitem_url = None + self.getiter_url = None + self.patch_url = self.patch_data = None + self.post_url = self.post_data = None + + async def getitem(self, url): + self.getitem_url = url + to_return = self._getitem_return[self.getitem_url] + if isinstance(to_return, Exception): + raise to_return + else: + return to_return + + async def getiter(self, url): + self.getiter_url = url + to_iterate = self._getiter_return[url] + for item in to_iterate: + yield item + + async def patch(self, url, *, data): + self.patch_url = url + self.patch_data = data + return self._patch_return + + async def post(self, url, *, data): + self.post_url = url + self.post_data = data + if isinstance(self._post_return, Exception): + raise self._post_return + else: + return self._post_return + async def test_nonceify(): body = "Lorem ipsum dolor amet flannel squid normcore tbh raclette enim" "pabst tumblr wolf farm-to-table bitters. Bitters keffiyeh next" "level proident normcore, et all of +1 90's in blue bottle" "chillwave lorem. Id keffiyeh microdosing cupidatat pour-over" "paleo farm-to-table tumeric sriracha +1. Raclette in poutine," "bushwick kitsch id pariatur hexagon. Thundercats shaman beard," "nulla swag echo park organic microdosing. Hot chicken tbh pop-up" "tacos, asymmetrical tilde veniam bespoke reprehenderit ut do." @@ -24,3 +67,35 @@ async def test_get_misc_news_filename(): assert path.startswith("Misc/NEWS.d/next/Library/") assert path.endswith(".bpo-123.Ps4kgC.rst") + + +async def test_is_core_dev(): + teams = [{"name": "not Python core"}] + gh = FakeGH(getiter={"/orgs/python/teams": teams}) + with pytest.raises(ValueError): + await util.is_core_dev(gh, "mariatta") + + teams = [{"name": "python core", "id": 42}] + getitem = {"/teams/42/memberships/mariatta": True} + gh = FakeGH(getiter={"/orgs/python/teams": teams}, getitem=getitem) + assert await util.is_core_dev(gh, "mariatta") + assert gh.getiter_url == "/orgs/python/teams" + + teams = [{"name": "python core", "id": 42}] + getitem = { + "/teams/42/memberships/miss-islington": gidgethub.BadRequest( + status_code=http.HTTPStatus(404) + ) + } + gh = FakeGH(getiter={"/orgs/python/teams": teams}, getitem=getitem) + assert not await util.is_core_dev(gh, "miss-islington") + + teams = [{"name": "python core", "id": 42}] + getitem = { + "/teams/42/memberships/miss-islington": gidgethub.BadRequest( + status_code=http.HTTPStatus(400) + ) + } + gh = FakeGH(getiter={"/orgs/python/teams": teams}, getitem=getitem) + with pytest.raises(gidgethub.BadRequest): + await util.is_core_dev(gh, "miss-islington") \ No newline at end of file From 9e74ea02679738278bcf79b6a52d3a30c0fb6d5b Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 16:21:14 +0000 Subject: [PATCH 02/12] =?UTF-8?q?=F0=9F=90=8D=F0=9F=8C=9A=F0=9F=A4=96=20Fo?= =?UTF-8?q?rmatted=20using=20`black`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blurb_it/__main__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index d6a556a..88d8f68 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -163,10 +163,10 @@ async def handle_add_blurb_post(request): is_core_dev = util.is_core_dev(gh, session_context["username"]) if is_core_dev: gh = GitHubAPI( - session, - "miss-islington", - oauth_token=os.getenv("MI_GH_AUTH"), - ) + session, + "miss-islington", + oauth_token=os.getenv("MI_GH_AUTH"), + ) try: response = await gh.put( f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data From 516f99ca883ebf04c2d537a3c4ed36bb311553b9 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 16:21:19 +0000 Subject: [PATCH 03/12] =?UTF-8?q?=F0=9F=90=8D=F0=9F=8C=9A=F0=9F=A4=96=20Fo?= =?UTF-8?q?rmatted=20using=20`black`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blurb_it/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blurb_it/util.py b/blurb_it/util.py index 15ef0ef..8d259b4 100644 --- a/blurb_it/util.py +++ b/blurb_it/util.py @@ -104,4 +104,4 @@ async def is_core_dev(gh, username): return False raise else: - return True \ No newline at end of file + return True From 143ce38d763870651f4f3bd37f5a9326f3ff4c44 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 16:21:23 +0000 Subject: [PATCH 04/12] =?UTF-8?q?=F0=9F=90=8D=F0=9F=8C=9A=F0=9F=A4=96=20Fo?= =?UTF-8?q?rmatted=20using=20`black`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_util.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 220b533..04f11e5 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,6 +5,7 @@ from blurb_it import util + class FakeGH: def __init__(self, *, getiter=None, getitem=None, post=None, patch=None): self._getitem_return = getitem @@ -45,7 +46,16 @@ async def post(self, url, *, data): async def test_nonceify(): - body = "Lorem ipsum dolor amet flannel squid normcore tbh raclette enim" "pabst tumblr wolf farm-to-table bitters. Bitters keffiyeh next" "level proident normcore, et all of +1 90's in blue bottle" "chillwave lorem. Id keffiyeh microdosing cupidatat pour-over" "paleo farm-to-table tumeric sriracha +1. Raclette in poutine," "bushwick kitsch id pariatur hexagon. Thundercats shaman beard," "nulla swag echo park organic microdosing. Hot chicken tbh pop-up" "tacos, asymmetrical tilde veniam bespoke reprehenderit ut do." + body = ( + "Lorem ipsum dolor amet flannel squid normcore tbh raclette enim" + "pabst tumblr wolf farm-to-table bitters. Bitters keffiyeh next" + "level proident normcore, et all of +1 90's in blue bottle" + "chillwave lorem. Id keffiyeh microdosing cupidatat pour-over" + "paleo farm-to-table tumeric sriracha +1. Raclette in poutine," + "bushwick kitsch id pariatur hexagon. Thundercats shaman beard," + "nulla swag echo park organic microdosing. Hot chicken tbh pop-up" + "tacos, asymmetrical tilde veniam bespoke reprehenderit ut do." + ) nonce = await util.nonceify(body) assert nonce == "Ps4kgC" @@ -98,4 +108,4 @@ async def test_is_core_dev(): } gh = FakeGH(getiter={"/orgs/python/teams": teams}, getitem=getitem) with pytest.raises(gidgethub.BadRequest): - await util.is_core_dev(gh, "miss-islington") \ No newline at end of file + await util.is_core_dev(gh, "miss-islington") From 0ab6c482d4eb5014cefe69255902ccc51c891bec Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 08:28:49 -0800 Subject: [PATCH 05/12] await --- blurb_it/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 88d8f68..9ed5d02 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -160,7 +160,7 @@ async def handle_add_blurb_post(request): "message": "📜🤖 Added by blurb_it.", } if pr["user"]["login"] != session_context["username"]: - is_core_dev = util.is_core_dev(gh, session_context["username"]) + is_core_dev = await util.is_core_dev(gh, session_context["username"]) if is_core_dev: gh = GitHubAPI( session, From 8ab2a0b8e2067a5c87b88dd75b1db3f61dd5ff7c Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 16:30:39 +0000 Subject: [PATCH 06/12] =?UTF-8?q?=F0=9F=90=8D=F0=9F=8C=9A=F0=9F=A4=96=20Fo?= =?UTF-8?q?rmatted=20using=20`black`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blurb_it/__main__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 9ed5d02..40ef1ad 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -160,7 +160,9 @@ async def handle_add_blurb_post(request): "message": "📜🤖 Added by blurb_it.", } if pr["user"]["login"] != session_context["username"]: - is_core_dev = await util.is_core_dev(gh, session_context["username"]) + is_core_dev = await util.is_core_dev( + gh, session_context["username"] + ) if is_core_dev: gh = GitHubAPI( session, From 7d7e4619e884d614bc57b0de18aace0d4f1e02f2 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 08:46:25 -0800 Subject: [PATCH 07/12] Create a new session. --- blurb_it/__main__.py | 84 ++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 40ef1ad..ae7398c 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -160,37 +160,61 @@ async def handle_add_blurb_post(request): "message": "📜🤖 Added by blurb_it.", } if pr["user"]["login"] != session_context["username"]: - is_core_dev = await util.is_core_dev( - gh, session_context["username"] - ) - if is_core_dev: - gh = GitHubAPI( - session, - "miss-islington", - oauth_token=os.getenv("MI_GH_AUTH"), - ) - try: - response = await gh.put( - f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data - ) - except gidgethub.BadRequest as bac: - print("BadRequest") - print(int(bac.status_code)) - print(bac) - context[ - "pr_url" - ] = f"https://github.com/python/cpython/pull/{pr_number}" - context["pr_number"] = pr_number - context["status"] = "failure" + async with aiohttp.ClientSession() as session: + mi_gh = GitHubAPI(session, "miss-islington", oauth_token=os.getenv("MI_GH_AUTH")) + is_core_dev = await util.is_core_dev(mi_gh, session_context["username"]) + if is_core_dev: + try: + + response = await mi_gh.put( + f"/repos/{pr_repo_full_name}/contents/{path}", + data=put_data + ) + except gidgethub.BadRequest as bac: + print("BadRequest") + print(int(bac.status_code)) + print(bac) + context[ + "pr_url" + ] = f"https://github.com/python/cpython/pull/{pr_number}" + context["pr_number"] = pr_number + context["status"] = "failure" + else: + print("response") + print(response) + commit_url = response["commit"]["html_url"] + context["commit_url"] = commit_url + context["path"] = response["content"]["path"] + context[ + "pr_url" + ] = f"https://github.com/python/cpython/pull/{pr_number}" + context["pr_number"] = pr_number + context["status"] = "success" else: - commit_url = response["commit"]["html_url"] - context["commit_url"] = commit_url - context["path"] = response["content"]["path"] - context[ - "pr_url" - ] = f"https://github.com/python/cpython/pull/{pr_number}" - context["pr_number"] = pr_number - context["status"] = "success" + try: + response = await gh.put( + f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data + ) + except gidgethub.BadRequest as bac: + print("BadRequest") + print(int(bac.status_code)) + print(bac) + context[ + "pr_url" + ] = f"https://github.com/python/cpython/pull/{pr_number}" + context["pr_number"] = pr_number + context["status"] = "failure" + else: + print("response") + print(response) + commit_url = response["commit"]["html_url"] + context["commit_url"] = commit_url + context["path"] = response["content"]["path"] + context[ + "pr_url" + ] = f"https://github.com/python/cpython/pull/{pr_number}" + context["pr_number"] = pr_number + context["status"] = "success" template = "add_blurb.html" response = aiohttp_jinja2.render_template(template, request, context=context) From 3e3cef54edb25060a40247c6302624dc955b1227 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 16:48:20 +0000 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=90=8D=F0=9F=8C=9A=F0=9F=A4=96=20Fo?= =?UTF-8?q?rmatted=20using=20`black`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blurb_it/__main__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index ae7398c..53e5755 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -161,14 +161,20 @@ async def handle_add_blurb_post(request): } if pr["user"]["login"] != session_context["username"]: async with aiohttp.ClientSession() as session: - mi_gh = GitHubAPI(session, "miss-islington", oauth_token=os.getenv("MI_GH_AUTH")) - is_core_dev = await util.is_core_dev(mi_gh, session_context["username"]) + mi_gh = GitHubAPI( + session, + "miss-islington", + oauth_token=os.getenv("MI_GH_AUTH"), + ) + is_core_dev = await util.is_core_dev( + mi_gh, session_context["username"] + ) if is_core_dev: try: response = await mi_gh.put( f"/repos/{pr_repo_full_name}/contents/{path}", - data=put_data + data=put_data, ) except gidgethub.BadRequest as bac: print("BadRequest") From 560e16979fdde334e105bfff963e6c3f41c3ed38 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 09:03:01 -0800 Subject: [PATCH 09/12] Set committer info. --- blurb_it/__main__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 53e5755..4432553 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -169,15 +169,17 @@ async def handle_add_blurb_post(request): is_core_dev = await util.is_core_dev( mi_gh, session_context["username"] ) + print(f"{session_context['username']} is core dev {is_core_dev} ") if is_core_dev: try: - + put_data["author"] = {"name": "Miss Islington (bot)", "email": "mariatta.wijaya+miss-islington@gmail.com"} + put_data["committer"] = {"name": "Miss Islington (bot)", "email": "mariatta.wijaya+miss-islington@gmail.com"} response = await mi_gh.put( f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data, ) except gidgethub.BadRequest as bac: - print("BadRequest") + print("BadRequest, error using miss-islington's oauth token") print(int(bac.status_code)) print(bac) context[ @@ -202,7 +204,7 @@ async def handle_add_blurb_post(request): f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data ) except gidgethub.BadRequest as bac: - print("BadRequest") + print("BadRequest, using blurb-it") print(int(bac.status_code)) print(bac) context[ From 9b4b507c8ffe656b2ff70c09853ce316df5b2596 Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 09:12:24 -0800 Subject: [PATCH 10/12] Debugging --- blurb_it/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 4432553..2a15723 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -163,7 +163,7 @@ async def handle_add_blurb_post(request): async with aiohttp.ClientSession() as session: mi_gh = GitHubAPI( session, - "miss-islington", + "python/cpython", oauth_token=os.getenv("MI_GH_AUTH"), ) is_core_dev = await util.is_core_dev( @@ -187,6 +187,7 @@ async def handle_add_blurb_post(request): ] = f"https://github.com/python/cpython/pull/{pr_number}" context["pr_number"] = pr_number context["status"] = "failure" + await gh.post(f"/repos/python/cpython/issues/{pr_number}/comments", data={"body": "Ping!"}) else: print("response") print(response) From 51b49b1e0bc16e0ded617c22f7bea61180f6d8eb Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 09:14:41 -0800 Subject: [PATCH 11/12] Debugging --- blurb_it/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 2a15723..ef18c64 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -187,7 +187,7 @@ async def handle_add_blurb_post(request): ] = f"https://github.com/python/cpython/pull/{pr_number}" context["pr_number"] = pr_number context["status"] = "failure" - await gh.post(f"/repos/python/cpython/issues/{pr_number}/comments", data={"body": "Ping!"}) + await mi_gh.post(f"/repos/python/cpython/issues/{pr_number}/comments", data={"body": "Ping!"}) else: print("response") print(response) From c20e8e969071b4e9f5c729a9500c508d46dda62a Mon Sep 17 00:00:00 2001 From: Mariatta Wijaya Date: Sun, 30 Dec 2018 12:19:38 -0800 Subject: [PATCH 12/12] Remove author info --- blurb_it/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index ef18c64..be780eb 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -172,8 +172,8 @@ async def handle_add_blurb_post(request): print(f"{session_context['username']} is core dev {is_core_dev} ") if is_core_dev: try: - put_data["author"] = {"name": "Miss Islington (bot)", "email": "mariatta.wijaya+miss-islington@gmail.com"} - put_data["committer"] = {"name": "Miss Islington (bot)", "email": "mariatta.wijaya+miss-islington@gmail.com"} + # put_data["author"] = {"name": "Miss Islington (bot)", "email": "mariatta.wijaya+miss-islington@gmail.com"} + # put_data["committer"] = {"name": "Miss Islington (bot)", "email": "mariatta.wijaya+miss-islington@gmail.com"} response = await mi_gh.put( f"/repos/{pr_repo_full_name}/contents/{path}", data=put_data,