Skip to content
Next Next commit
Allow adding blurb for anyone, if logged in user is core-dev.
  • Loading branch information
Mariatta committed Dec 30, 2018
commit 5da855c4e606d2f5a1fa79de9fe7f5bb9bb7c9c7
8 changes: 8 additions & 0 deletions blurb_it/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions blurb_it/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import time

import jwt
import gidgethub

from aiohttp_session import get_session

from blurb_it import error
Expand Down Expand Up @@ -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
75 changes: 75 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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")