Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions scripts/publish-candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions scripts/tests/test_publish_candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down