Skip to content

Commit f996fd2

Browse files
committed
Fix parsing of organization/repository
1 parent 36eefdf commit f996fd2

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

test_webhook.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ async def test_github_webhook_errors(aiohttp_client, monkeypatch):
125125

126126
resp = await client.post(
127127
'/gh/non-existent-repo', headers=valid_headers,
128-
data='{"sender": "QuLogic", "organization": "foo",'
129-
' "repository": "foo"}')
128+
data='{"sender": {"login": "QuLogic"},'
129+
' "repository": {"name": "foo", "owner": {"login": "foo"}}}')
130130
assert resp.status == 400
131131
assert 'incorrect organization' in await resp.text()
132132

133133
resp = await client.post(
134134
'/gh/non-existent-repo', headers=valid_headers,
135-
data='{"sender": "QuLogic", "organization": "matplotlib",'
136-
' "repository": "foo"}')
135+
data='{"sender": {"login": "QuLogic"}, "repository":'
136+
' {"name": "foo", "owner": {"login": "matplotlib"}}}')
137137
assert resp.status == 400
138138
assert 'incorrect repository' in await resp.text()
139139

@@ -158,19 +158,20 @@ async def test_github_webhook_valid(aiohttp_client, monkeypatch):
158158
resp = await client.post(
159159
'/gh/non-existent-repo',
160160
headers={**valid_headers, 'X-GitHub-Event': 'ping'},
161-
data='{"sender": "QuLogic", "hook_id": 1234,'
161+
data='{"sender": {"login": "QuLogic"}, "hook_id": 1234,'
162162
' "zen": "Beautiful is better than ugly.",'
163-
' "organization": "matplotlib",'
164-
' "repository": "non-existent-repo"}')
163+
' "repository": {"name": "non-existent-repo",'
164+
' "owner": {"login": "matplotlib"}}}')
165165
assert resp.status == 200
166166
ur_mock.assert_not_called()
167167

168168
# Push event should run an update.
169169
resp = await client.post(
170170
'/gh/non-existent-repo',
171171
headers={**valid_headers, 'X-GitHub-Event': 'push'},
172-
data='{"sender": "QuLogic", "organization": "matplotlib",'
173-
' "repository": "non-existent-repo"}')
172+
data='{"sender": {"login": "QuLogic"},'
173+
' "repository": {"name": "non-existent-repo",'
174+
' "owner": {"login": "matplotlib"}}}')
174175
assert resp.status == 200
175176
ur_mock.assert_called_once_with(
176177
Path('non-existent-site-dir/non-existent-repo'), 'foo',

webhook.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ async def github_webhook(request: web.Request):
100100

101101
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-object-common-properties
102102
try:
103-
sender = data['sender']
104-
organization = data['organization']
103+
sender = data['sender']['login']
105104
repository = data['repository']
105+
except (KeyError, TypeError):
106+
raise web.HTTPBadRequest(reason='Missing required fields')
107+
try:
108+
organization = repository['owner']['login']
109+
repository = repository['name']
106110
except KeyError:
107111
raise web.HTTPBadRequest(reason='Missing required fields')
108112
log.info('%s: Received %s event from %s on %s/%s',

0 commit comments

Comments
 (0)