Skip to content

Commit 84a35ad

Browse files
Return error status if bedevere is not used as an app. (#598)
Co-authored-by: Ezio Melotti <[email protected]>
1 parent c07fe33 commit 84a35ad

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

bedevere/__main__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ async def main(request):
3737

3838
async with aiohttp.ClientSession() as session:
3939
gh = gh_aiohttp.GitHubAPI(session, "python/bedevere", cache=cache)
40+
if not event.data.get("installation"):
41+
return web.Response(text="Must be installed as an App.", status=400)
42+
installation_id = event.data["installation"]["id"]
43+
installation_access_token = await apps.get_installation_access_token(
44+
gh,
45+
installation_id=installation_id,
46+
app_id=os.environ.get("GH_APP_ID"),
47+
private_key=os.environ.get("GH_PRIVATE_KEY"),
48+
)
49+
gh.oauth_token = installation_access_token["token"]
4050

41-
if event.data.get("installation"):
42-
# This path only works on GitHub App
43-
installation_id = event.data["installation"]["id"]
44-
installation_access_token = await apps.get_installation_access_token(
45-
gh,
46-
installation_id=installation_id,
47-
app_id=os.environ.get("GH_APP_ID"),
48-
private_key=os.environ.get("GH_PRIVATE_KEY"),
49-
)
50-
gh.oauth_token = installation_access_token["token"]
5151
# Give GitHub some time to reach internal consistency.
5252
await asyncio.sleep(1)
5353
await router.dispatch(event, gh, session=session)
@@ -63,7 +63,6 @@ async def main(request):
6363

6464
@router.register("installation", action="created")
6565
async def repo_installation_added(event, gh, *args, **kwargs):
66-
# installation_id = event.data["installation"]["id"]
6766
print(
6867
f"App installed by {event.data['installation']['account']['login']}, installation_id: {event.data['installation']['id']}"
6968
)

tests/test___main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def test_ping(aiohttp_client):
2323
assert response.status == 200
2424

2525

26-
async def test_success(aiohttp_client):
26+
async def test_bad_request_if_no_installation(aiohttp_client):
2727
app = web.Application()
2828
app.router.add_post("/", main.main)
2929
client = await aiohttp_client(app)
@@ -32,7 +32,8 @@ async def test_success(aiohttp_client):
3232
# either.
3333
data = {"action": "created"}
3434
response = await client.post("/", headers=headers, json=data)
35-
assert response.status == 200
35+
assert response.status == 400
36+
assert await response.text() == "Must be installed as an App."
3637

3738

3839
async def test_failure(aiohttp_client):

tests/test_stage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ async def test_new_commit_pushed_to_not_approved_pr(issue_url_key, repo_full_nam
12171217

12181218
async def test_pushed_without_commits():
12191219
# There is new commit on approved PR
1220-
sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
1220+
12211221
data = {"commits": []}
12221222
event = sansio.Event(data, event="push", delivery_id="12345")
12231223
gh = FakeGH()

0 commit comments

Comments
 (0)