From 84a35ad582fa619250613cdd1edc317dd3591b90 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Wed, 17 Apr 2024 08:37:11 -0700 Subject: [PATCH] Return error status if bedevere is not used as an app. (#598) Co-authored-by: Ezio Melotti --- bedevere/__main__.py | 21 ++++++++++----------- tests/test___main__.py | 5 +++-- tests/test_stage.py | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bedevere/__main__.py b/bedevere/__main__.py index a50855f9..4bb678e8 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -37,17 +37,17 @@ async def main(request): async with aiohttp.ClientSession() as session: gh = gh_aiohttp.GitHubAPI(session, "python/bedevere", cache=cache) + if not event.data.get("installation"): + return web.Response(text="Must be installed as an App.", status=400) + installation_id = event.data["installation"]["id"] + installation_access_token = await apps.get_installation_access_token( + gh, + installation_id=installation_id, + app_id=os.environ.get("GH_APP_ID"), + private_key=os.environ.get("GH_PRIVATE_KEY"), + ) + gh.oauth_token = installation_access_token["token"] - if event.data.get("installation"): - # This path only works on GitHub App - installation_id = event.data["installation"]["id"] - installation_access_token = await apps.get_installation_access_token( - gh, - installation_id=installation_id, - app_id=os.environ.get("GH_APP_ID"), - private_key=os.environ.get("GH_PRIVATE_KEY"), - ) - gh.oauth_token = installation_access_token["token"] # Give GitHub some time to reach internal consistency. await asyncio.sleep(1) await router.dispatch(event, gh, session=session) @@ -63,7 +63,6 @@ async def main(request): @router.register("installation", action="created") async def repo_installation_added(event, gh, *args, **kwargs): - # installation_id = event.data["installation"]["id"] print( f"App installed by {event.data['installation']['account']['login']}, installation_id: {event.data['installation']['id']}" ) diff --git a/tests/test___main__.py b/tests/test___main__.py index 92962c92..397d1339 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -23,7 +23,7 @@ async def test_ping(aiohttp_client): assert response.status == 200 -async def test_success(aiohttp_client): +async def test_bad_request_if_no_installation(aiohttp_client): app = web.Application() app.router.add_post("/", main.main) client = await aiohttp_client(app) @@ -32,7 +32,8 @@ async def test_success(aiohttp_client): # either. data = {"action": "created"} response = await client.post("/", headers=headers, json=data) - assert response.status == 200 + assert response.status == 400 + assert await response.text() == "Must be installed as an App." async def test_failure(aiohttp_client): diff --git a/tests/test_stage.py b/tests/test_stage.py index 5b635b13..4a1551ec 100644 --- a/tests/test_stage.py +++ b/tests/test_stage.py @@ -1217,7 +1217,7 @@ async def test_new_commit_pushed_to_not_approved_pr(issue_url_key, repo_full_nam async def test_pushed_without_commits(): # There is new commit on approved PR - sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9" + data = {"commits": []} event = sansio.Event(data, event="push", delivery_id="12345") gh = FakeGH()