Skip to content

Commit cf15581

Browse files
committed
Only react to pushes on gh-pages branch
1 parent b34f46b commit cf15581

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

test_webhook.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,23 @@ async def test_github_webhook_valid(aiohttp_client, monkeypatch):
165165
assert resp.status == 200
166166
ur_mock.assert_not_called()
167167

168-
# Push event should run an update.
168+
# Push event to main branch should do nothing.
169169
resp = await client.post(
170170
'/gh/non-existent-repo',
171171
headers={**valid_headers, 'X-GitHub-Event': 'push'},
172172
data='{"sender": {"login": "QuLogic"},'
173+
' "ref": "refs/heads/main",'
174+
' "repository": {"name": "non-existent-repo",'
175+
' "owner": {"login": "matplotlib"}}}')
176+
assert resp.status == 200
177+
ur_mock.assert_not_called()
178+
179+
# Push event to gh-pages branch should run an update.
180+
resp = await client.post(
181+
'/gh/non-existent-repo',
182+
headers={**valid_headers, 'X-GitHub-Event': 'push'},
183+
data='{"sender": {"login": "QuLogic"},'
184+
' "ref": "refs/heads/gh-pages",'
173185
' "repository": {"name": "non-existent-repo",'
174186
' "owner": {"login": "matplotlib"}}}')
175187
assert resp.status == 200

webhook.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async def update_repo(repo: Path, delivery: str, name: str):
3535
raise web.HTTPServerError(
3636
reason=f'{delivery}: Checkout for {name} does not exist')
3737

38+
log.info('%s: Running git pull for %s at %s', delivery, name, repo)
3839
proc = await asyncio.create_subprocess_exec('git', 'pull', cwd=repo)
3940
try:
4041
await asyncio.wait_for(proc.wait(), timeout=60)
@@ -117,14 +118,24 @@ async def github_webhook(request: web.Request):
117118
if repository != repo:
118119
raise web.HTTPBadRequest(reason=f'{delivery}: incorrect repository')
119120

121+
# Ping event
122+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#ping
120123
if event == 'ping':
121124
log.info('%s: Ping %s: %s', delivery, data['hook_id'], data['zen'])
122125
return web.Response(status=200)
123126

127+
# Only allow push events otherwise
128+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push
124129
if event != 'push':
125130
log.info('%s: Ignoring webhook for unused event %s', delivery, event)
126131
return web.Response(status=200)
127132

133+
ref = data.get('ref', '')
134+
if ref != 'refs/heads/gh-pages':
135+
log.info('%s: Ignoring push event on branch %s other than gh-pages',
136+
delivery, ref)
137+
return web.Response(status=200)
138+
128139
checkout = Path(os.environ.get('SITE_DIR', 'sites'), repository)
129140
await update_repo(checkout, delivery, f'{organization}/{repository}')
130141

0 commit comments

Comments
 (0)