Skip to content

Commit 2575804

Browse files
authored
fix(ci): a merge racing Conda Update's push leaves the run green (#1649) (#1669)
- Push Changes rebases onto the tip it fetched and judged, so a merge landing between that fetch and the push had the push refused as a non-fast-forward, and the step failed: a red run on main. A failed push now re-fetches main. Moved to a commit without the bump in its history, the step writes pushed=false, emits a notice and exits 0, as main moving before the fetch does. - Main still at the judged tip (a declined push), the bump in main's history (a push that landed though git reported failure), or a failed re-fetch still fail the step with the push's exit status. test_a_merge_landing_after_the_fetch_refuses_the_push now expects pushed=false, the notice and exit 0, and fails against the old step. Four new tests run the real step against throwaway repos: a pre-receive hook declining, the remote lost after the fetch, and a push that lands but reports failure, with and without a merge over it. Each fails when its check is dropped from the step. Closes #1649
1 parent 4673cff commit 2575804

2 files changed

Lines changed: 132 additions & 17 deletions

File tree

‎.github/workflows/cron-conda.yml‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,28 @@ jobs:
254254
;;
255255
1)
256256
commit="$(git rev-parse HEAD)"
257-
git push origin "${commit}:${GITHUB_REF}"
257+
status=0
258+
git push origin "${commit}:${GITHUB_REF}" || status=$?
259+
if [ "$status" -ne 0 ]; then
260+
# #1649: a merge landing after the fetch above has the push
261+
# refused as a non-fast-forward. That is main moving past this
262+
# checkout one step later than the rule can see, so it ends as
263+
# the rule's refusal does. Only main moved to a commit without
264+
# `$commit` in its history is that race: at `$tip` the push
265+
# failed for another reason, and over `$commit` it landed though
266+
# git says it did not. Those, and a fetch that fails, fail the
267+
# step.
268+
git fetch origin "${GITHUB_REF#refs/heads/}" || exit "$status"
269+
moved="$(git rev-parse FETCH_HEAD)"
270+
ancestry=0
271+
git merge-base --is-ancestor "$commit" "$moved" || ancestry=$?
272+
if [ "$moved" = "$tip" ] || [ "$ancestry" -ne 1 ]; then
273+
exit "$status"
274+
fi
275+
echo "::notice title=Not pushed as main moved during the push::main moved from $tip to $moved after the fetch, so $commit is not a fast-forward. The bump is left to the Conda Update that $moved's own Unit Tests run raises."
276+
echo "pushed=false" >> "$GITHUB_OUTPUT"
277+
exit 0
278+
fi
258279
echo "pushed=true" >> "$GITHUB_OUTPUT"
259280
echo "commit=${commit}" >> "$GITHUB_OUTPUT"
260281
;;

‎tests/project/test_release_gates.py‎

Lines changed: 110 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,28 +3937,37 @@ def _land(self, name: 'str', files: 'dict[str, str]', message: 'str',
39373937
def _land_after_fetch(self, name: 'str', files: 'dict[str, str]', message: 'str',
39383938
identity: 'Optional[tuple[tuple[str, str], tuple[str, str]]]' = None,
39393939
) -> 'tuple[str, dict[str, str]]':
3940-
""":meth:`_land`, but the push is left to a ``git`` shim that makes it
3941-
once, the moment a ``git fetch`` run through it returns: the commit's
3942-
sha, and the ``PATH`` that puts the shim first. It marks having fired
3943-
by creating ``landed`` in the temporary directory.
3940+
""":meth:`_land`, but the push is left to :meth:`_after_first`, the
3941+
moment a ``git fetch`` returns: the commit's sha, and the ``PATH``.
39443942
39453943
"""
39463944
clone = self._stage(name, files, message, identity)
3945+
env = self._after_first(
3946+
'fetch', f'exec "$git" -C {shlex.quote(str(clone))} push -q origin HEAD:refs/heads/main')
3947+
return self._git(clone, 'rev-parse', 'HEAD'), env
3948+
3949+
def _after_first(self, command: 'str', action: 'str') -> 'dict[str, str]':
3950+
"""The ``PATH`` that puts first a ``git`` shim running the real one,
3951+
``$git``, and then, once, the ``sh`` line ``action``, the moment a
3952+
``git <command>`` run through it succeeds. It marks having fired by
3953+
creating ``fired`` in the temporary directory.
3954+
3955+
"""
39473956
real = shutil.which('git', path=self.env['PATH'])
39483957
assert real is not None # setUpClass skips without it
3949-
git, marker = shlex.quote(real), shlex.quote(str(self.root / 'landed'))
3958+
marker = shlex.quote(str(self.root / 'fired'))
39503959
shim = self.root / 'shim'
39513960
shim.mkdir()
39523961
(shim / 'git').write_text(
39533962
'#!/bin/sh\n'
3954-
f'{git} "$@" || exit\n'
3955-
f'if [ "$1" = fetch ] && [ ! -e {marker} ]; then\n'
3963+
f'git={shlex.quote(real)}\n'
3964+
'"$git" "$@" || exit\n'
3965+
f'if [ "$1" = {shlex.quote(command)} ] && [ ! -e {marker} ]; then\n'
39563966
f' : > {marker}\n'
3957-
f' exec {git} -C {shlex.quote(str(clone))} push -q origin HEAD:refs/heads/main\n'
3967+
f' {action}\n'
39583968
'fi\n', encoding='utf-8')
39593969
(shim / 'git').chmod(0o755)
3960-
return (self._git(clone, 'rev-parse', 'HEAD'),
3961-
{'PATH': f'{shim}{os.pathsep}{self.env["PATH"]}'})
3970+
return {'PATH': f'{shim}{os.pathsep}{self.env["PATH"]}'}
39623971

39633972
def _bash(self, script: 'str', cwd: 'pathlib.Path', env: 'dict[str, str]',
39643973
) -> 'tuple[subprocess.CompletedProcess[str], dict[str, str]]':
@@ -4121,20 +4130,24 @@ def test_the_rule_is_run_from_the_checkout_not_from_the_tip_it_judges(self) -> N
41214130
def test_a_merge_landing_after_the_fetch_refuses_the_push(self) -> None:
41224131
"""The rebase is onto the tip the rule judged, ``$tip``, so a merge that
41234132
lands after the fetch is never under the bump: ``origin`` has moved past
4124-
what is pushed, the push is not a fast-forward, and it is refused. The
4125-
step fails having written nothing, so nothing is tagged or uploaded,
4126-
and the merge's own ``Unit Tests`` run raises the next bump.
4133+
what is pushed, the push is not a fast-forward, and it is refused. That
4134+
is :meth:`test_a_bump_is_not_pushed_over_a_commit_with_no_verdict` one
4135+
step later, and it ends the same way (#1649): ``pushed=false``, a
4136+
notice, and a green job, and the merge's own ``Unit Tests`` run raises
4137+
the next bump.
41274138
41284139
"""
41294140
clone = self._clone('first')
41304141
merge, env = self._land_after_fetch('merge', {'pcapkit/toolkit/scapy.py': 'fix\n'},
41314142
'fix(toolkit): a merge', (self.HUMAN, self.GITHUB))
41324143
self._bump(clone, {0: 'package0==2.0'})
41334144
result, outputs = self._run(clone, **env)
4134-
self.assertTrue((self.root / 'landed').exists(), 'the merge never raced the fetch')
4135-
self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
4145+
self.assertTrue((self.root / 'fired').exists(), 'the merge never raced the fetch')
4146+
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
41364147
self.assertIn('[rejected]', result.stderr)
4137-
self.assertEqual(outputs, {}, 'the step reported a push that did not land')
4148+
self.assertEqual(outputs, {'pushed': 'false'}, 'the step reported a push that did not land')
4149+
self.assertIn(f'::notice title=Not pushed as main moved during the push::main moved '
4150+
f'from {self.base} to {merge} after the fetch', result.stdout)
41384151
self.assertEqual(self._origin('rev-parse', 'main'), merge)
41394152
self.assertEqual(self._git(clone, 'rev-parse', 'HEAD^'), self.base,
41404153
'the bump was not rebased onto the tip the rule judged')
@@ -4160,6 +4173,87 @@ def test_a_rebase_that_fetches_again_pushes_the_merge_under_the_bump(self) -> No
41604173
self.assertEqual(outputs, {'pushed': 'true', 'commit': landed})
41614174
self.assertEqual(self._origin('rev-parse', 'main^'), merge)
41624175

4176+
def test_a_push_the_remote_declines_fails_the_step(self) -> None:
4177+
"""A refusal with ``main`` where it was fetched -- a hook here, a ruleset
4178+
on GitHub -- is no race, and must not pass as one: the step fails, as
4179+
every push failure did before #1649.
4180+
4181+
"""
4182+
clone = self._clone('first')
4183+
hook = self.origin / 'hooks' / 'pre-receive'
4184+
hook.write_text('#!/bin/sh\necho declined >&2\nexit 1\n', encoding='utf-8')
4185+
hook.chmod(0o755)
4186+
self._bump(clone, {0: 'package0==2.0'})
4187+
result, outputs = self._run(clone)
4188+
self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
4189+
self.assertIn('[remote rejected]', result.stderr)
4190+
self.assertEqual(outputs, {})
4191+
self.assertNotIn('::notice', result.stdout)
4192+
self.assertEqual(self._origin('rev-parse', 'main'), self.base)
4193+
4194+
def test_a_remote_lost_after_the_fetch_fails_the_step(self) -> None:
4195+
"""A broken remote: the push fails, and so does the fetch that would ask
4196+
where ``main`` is now. With no answer the step fails rather than
4197+
guessing at a race.
4198+
4199+
"""
4200+
clone = self._clone('first')
4201+
gone = self.root / 'origin.gone'
4202+
env = self._after_first('fetch', f'mv {shlex.quote(str(self.origin))} '
4203+
f'{shlex.quote(str(gone))}')
4204+
self._bump(clone, {0: 'package0==2.0'})
4205+
result, outputs = self._run(clone, **env)
4206+
self.assertTrue((self.root / 'fired').exists(), 'the remote was never lost')
4207+
self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
4208+
self.assertEqual(outputs, {})
4209+
self.assertNotIn('::notice', result.stdout)
4210+
gone.rename(self.origin)
4211+
self.assertEqual(self._origin('rev-parse', 'main'), self.base)
4212+
4213+
def test_a_push_that_lands_but_reports_failure_fails_the_step(self) -> None:
4214+
"""``main`` moved, but to the bump itself: the push landed though git
4215+
said it did not, as when the connection drops after the remote updates
4216+
the ref. Calling that a race would report ``pushed=false`` and green
4217+
for a bump nothing then tags, so it fails, as git reported.
4218+
4219+
"""
4220+
clone = self._clone('first')
4221+
env = self._after_first('push', "echo 'fatal: the remote end hung up unexpectedly' >&2"
4222+
'; exit 128')
4223+
self._bump(clone, {0: 'package0==2.0'})
4224+
result, outputs = self._run(clone, **env)
4225+
self.assertTrue((self.root / 'fired').exists(), 'the push never ran')
4226+
self.assertEqual(result.returncode, 128, result.stdout + result.stderr)
4227+
self.assertEqual(outputs, {})
4228+
self.assertNotIn('::notice', result.stdout)
4229+
self.assertEqual(self._origin('rev-parse', 'main'), self._git(clone, 'rev-parse', 'HEAD'))
4230+
self.assertEqual(self._origin('rev-parse', 'main^'), self.base)
4231+
4232+
def test_a_push_that_lands_under_a_later_merge_but_reports_failure_fails_the_step(
4233+
self) -> None:
4234+
"""The test above with a merge landing over the bump before the step
4235+
looks: ``main`` is at a third commit, but one with the bump in its
4236+
history, so this is no race either.
4237+
4238+
"""
4239+
clone = self._clone('first')
4240+
merger = self._stage('merge', {'pcapkit/toolkit/scapy.py': 'fix\n'},
4241+
'fix(toolkit): a merge', (self.HUMAN, self.GITHUB))
4242+
git = f'"$git" -C {shlex.quote(str(merger))}'
4243+
env = self._after_first('push', f'{git} pull -q --rebase origin main'
4244+
f' && {git} push -q origin HEAD:refs/heads/main'
4245+
"; echo 'fatal: the remote end hung up unexpectedly' >&2"
4246+
'; exit 128')
4247+
self._bump(clone, {0: 'package0==2.0'})
4248+
result, outputs = self._run(clone, **env)
4249+
self.assertTrue((self.root / 'fired').exists(), 'the push never ran')
4250+
self.assertEqual(result.returncode, 128, result.stdout + result.stderr)
4251+
self.assertEqual(outputs, {})
4252+
self.assertNotIn('::notice', result.stdout)
4253+
self.assertEqual(self._origin('rev-parse', 'main'), self._git(merger, 'rev-parse', 'HEAD'))
4254+
self.assertEqual(self._origin('rev-parse', 'main^'), self._git(clone, 'rev-parse', 'HEAD'),
4255+
'the merge did not land over the bump')
4256+
41634257
def test_a_rule_that_cannot_answer_fails_the_step(self) -> None:
41644258
"""No sha to compare is a broken run, not a stale one: the step fails,
41654259
writes nothing, and pushes nothing.

0 commit comments

Comments
 (0)