diff --git a/scripts/publish-candidates.py b/scripts/publish-candidates.py index 29282dc0..7f432f15 100644 --- a/scripts/publish-candidates.py +++ b/scripts/publish-candidates.py @@ -129,6 +129,23 @@ def wait_public(name, version, timeout=VISIBILITY_TIMEOUT, sleep=time.sleep, clo attempt += 1 +def preflight(items): + """Read-only: fail before any upload if a candidate's version is already public with other bytes.""" + states, drifted = [], [] + for archive, data, digest in items: + existing = downloaded(data['name'], data['vers']) + if existing is None: + states.append('verified-candidate') + elif sha(existing) == digest: + states.append('already-verified') + else: + states.append('published-with-different-bytes') + drifted.append(f"{data['name']} {data['vers']}") + if drifted: + raise ValueError('Published versions exist with different bytes (bump them before releasing; nothing was uploaded): ' + ', '.join(drifted)) + return states + + def publish(archive, data, digest, token): existing = downloaded(data['name'], data['vers']) if existing is not None: @@ -176,9 +193,10 @@ def main(): raise ValueError('Missing Cargo registry token') report = {'sourceSha': head, 'publicationRequested': args.publish, 'crates': []} args.output.parent.mkdir(parents=True, exist_ok=True) + states = None if args.publish else preflight(items) with args.output.open('x') as stream: - for archive, data, digest in items: - state = publish(archive, data, digest, token) if args.publish else 'verified-candidate' + for index, (archive, data, digest) in enumerate(items): + state = publish(archive, data, digest, token) if args.publish else states[index] row = {'name': data['name'], 'version': data['vers'], 'sha256': digest, 'status': state} print(json.dumps(row), flush=True) report['crates'].append(row) diff --git a/scripts/tests/test_publish_candidates.py b/scripts/tests/test_publish_candidates.py index c652b507..44f1e713 100644 --- a/scripts/tests/test_publish_candidates.py +++ b/scripts/tests/test_publish_candidates.py @@ -116,6 +116,23 @@ def test_upload_timeout_reports_visibility_not_byte_mismatch(self): publisher.publish(archive, publisher.metadata(archive), publisher.sha(archive.read_bytes()), 'test-only') + def test_preflight_rejects_every_drifted_published_version_before_upload(self): + with tempfile.TemporaryDirectory() as directory: + archive = self.archive(directory, '[package]\nname="fixture"\nversion="1.0.0"') + data = publisher.metadata(archive) + digest = publisher.sha(archive.read_bytes()) + other = dict(data, name='other') + public = {'fixture': archive.read_bytes(), 'other': b'old bytes'} + with patch.object(publisher, 'downloaded', side_effect=lambda name, version: public.get(name)), \ + patch.object(publisher.urllib.request, 'urlopen') as upload: + self.assertEqual(publisher.preflight([(archive, data, digest)]), ['already-verified']) + with self.assertRaisesRegex(ValueError, 'nothing was uploaded.*other 1.0.0'): + publisher.preflight([(archive, data, digest), (archive, other, digest)]) + upload.assert_not_called() + with patch.object(publisher, 'downloaded', return_value=None): + self.assertEqual(publisher.preflight([(archive, data, digest)]), ['verified-candidate']) + + class Clock: def __init__(self): self.now = 0