Skip to content

Commit 1dd267e

Browse files
committed
Fix updating of repos other than matplotlib.github.com
On those repos, the `gh-pages` branch is force pushed to prevent the repository from ballooning in size. This means we have to do a hard reset when pulling it.
1 parent f2781f0 commit 1dd267e

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

test_webhook.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,11 @@ async def test_signature(monkeypatch):
3535
'unused')
3636

3737

38-
async def test_update_repo_empty(tmp_path_factory):
39-
"""Test that updating an empty repository works as expected."""
40-
repo1 = tmp_path_factory.mktemp('repo1')
41-
run_check(['git', 'init', '-b', 'main', repo1])
42-
await update_repo(repo1, 'unused', 'matplotlib/repo1')
43-
44-
4538
async def test_update_repo(tmp_path_factory):
4639
"""Test that updating a repository works as expected."""
4740
# Set up a source repository.
4841
src = tmp_path_factory.mktemp('src')
49-
run_check(['git', 'init', '-b', 'main', src])
42+
run_check(['git', 'init', '-b', 'gh-pages', src])
5043
(src / 'readme.txt').write_text('Test repo information')
5144
run_check(['git', 'add', 'readme.txt'], cwd=src)
5245
run_check(['git', 'commit', '-m', 'Initial commit'], cwd=src)
@@ -65,7 +58,7 @@ async def test_update_repo(tmp_path_factory):
6558
(line for line in src_stdout if line.split()[-1] == 'HEAD'), '')
6659

6760
# Now this should correctly update the first repository.
68-
await update_repo(dest, 'unused', 'matplotlib/dest')
61+
assert await update_repo(dest, 'unused', 'matplotlib/dest')
6962
dest_stdout = run_check(['git', 'show-ref', '--head', 'HEAD'], cwd=dest,
7063
capture_output=True).stdout.splitlines()
7164
dest_commit = next(

webhook.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,25 @@
3232
async def update_repo(repo: Path, delivery: str, name: str):
3333
"""Update a git repo at the given path."""
3434
log.info('%s: Running git pull for %s at %s', delivery, name, repo)
35-
proc = await asyncio.create_subprocess_exec('git', 'pull', cwd=repo)
35+
36+
proc = await asyncio.create_subprocess_exec('git', 'fetch', cwd=repo)
37+
await proc.wait()
38+
if proc.returncode != 0:
39+
log.error('%s: Running git fetch for %s at %s failed: %d',
40+
delivery, name, repo, proc.returncode)
41+
return False
42+
43+
# Updates must be done this way because we force push away previous
44+
# gh-pages commits to keep the repository from ballooning in size.
45+
proc = await asyncio.create_subprocess_exec(
46+
'git', 'reset', '--hard', '@{upstream}', cwd=repo)
3647
await proc.wait()
3748
if proc.returncode != 0:
38-
log.error('%s: Running git pull for %s at %s failed: %d',
49+
log.error('%s: Running git reset for %s at %s failed: %d',
3950
delivery, name, repo, proc.returncode)
51+
return False
52+
53+
return True
4054

4155

4256
def handle_update_repo_result(task: asyncio.Task):
@@ -134,10 +148,12 @@ async def github_webhook(request: web.Request):
134148
log.info('%s: Ignoring webhook for unused event %s', delivery, event)
135149
return web.Response(status=200)
136150

151+
expected_branch = ('main' if repository == 'matplotlib.github.com'
152+
else 'gh-pages')
137153
ref = data.get('ref', '')
138-
if ref != 'refs/heads/gh-pages':
139-
log.info('%s: Ignoring push event on branch %s other than gh-pages',
140-
delivery, ref)
154+
if ref != f'refs/heads/{expected_branch}':
155+
log.info('%s: Ignoring push event on branch %s other than %s',
156+
delivery, ref, expected_branch)
141157
return web.Response(status=200)
142158

143159
checkout = Path(os.environ.get('SITE_DIR', 'sites'), repository)

0 commit comments

Comments
 (0)