Skip to content

Commit

Permalink
Return error status if bedevere is not used as an app. (#598)
Browse files Browse the repository at this point in the history
Co-authored-by: Ezio Melotti <[email protected]>
  • Loading branch information
Mariatta and ezio-melotti authored Apr 17, 2024
1 parent c07fe33 commit 84a35ad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
21 changes: 10 additions & 11 deletions bedevere/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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']}"
)
Expand Down
5 changes: 3 additions & 2 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 84a35ad

Please sign in to comment.