Skip to content
Open
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
46 changes: 33 additions & 13 deletions scripts/release/ci_release_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import tempfile

from ci_release_artifacts import BUNDLE_FILES, digest, identity, verify_bundle
from release_advertise import AdvertisementError, GitHub, advertise, expected_bundle
from release_advertise import (AdvertisementError, GitHub, advertise, advertisement_resume_available,
expected_bundle, is_untagged_draft)
from release_homebrew import HomebrewError, RECIPES, publish_tap, retain_recipes
from verify_desktop_signing import DesktopSigningError, verify_archive
from release_gateway import GatewayError, ensure_gateway
Expand Down Expand Up @@ -151,7 +152,7 @@ def revalidate_refs(root, source, commands):
validate_topology(root, source, runner=runner)


def release_for_tag(commands, version):
def release_for_tag(commands, version, *, release_id=None):
# A paginated listing distinguishes an absent tag from authentication/API errors.
pages = commands.api(f"{API}/releases?per_page=100", method="GET")
if not isinstance(pages, list) or any(not isinstance(item, dict) for item in pages):
Expand All @@ -166,10 +167,17 @@ def release_for_tag(commands, version):
matches += [item for item in pages if item.get("tag_name") == version]
if len(matches) > 1:
raise PublishError("ambiguous GitHub release state")
return matches[0] if matches else None
tagged = matches[0] if matches else None
if release_id is None:
return tagged
if tagged is not None and tagged.get("id") != release_id:
raise PublishError("ambiguous GitHub release state")
if tagged is not None:
return tagged
return commands.api(f"{API}/releases/{release_id}", method="GET")


def asset_matches(commands, asset, path, version):
def asset_matches(commands, asset, path, tag):
if asset.get("size") != path.stat().st_size:
return False
expected = "sha256:" + digest(path)
Expand All @@ -182,13 +190,17 @@ def asset_matches(commands, asset, path, version):
# Older GitHub responses omit digest; verify downloaded bytes rather than size alone.
with tempfile.TemporaryDirectory(prefix="rzm-release-asset-") as temporary:
destination = Path(temporary) / path.name
commands.run(("gh", "release", "download", version, "--repo", REPOSITORY,
commands.run(("gh", "release", "download", tag, "--repo", REPOSITORY,
"--pattern", path.name, "--output", str(destination)))
return destination.is_file() and digest(destination) == expected.removeprefix("sha256:")


def checked_assets(commands, release, source, version, notes, assets, *, complete=False):
if (not isinstance(release, dict) or release.get("tag_name") != version
def checked_assets(commands, release, source, version, notes, assets, *, complete=False, allow_untagged_draft=False):
tag_ok = isinstance(release, dict) and (
release.get("tag_name") == version
or (allow_untagged_draft and is_untagged_draft(release, version))
)
if (not tag_ok
or release.get("target_commitish") != source or release.get("body") != notes
or type(release.get("draft")) is not bool or release.get("prerelease") is not False
or type(release.get("id")) is not int or release["id"] <= 0):
Expand All @@ -201,7 +213,7 @@ def checked_assets(commands, release, source, version, notes, assets, *, complet
if len(names) != len(set(names)) or not set(names) <= BUNDLE_FILES or (complete and set(names) != BUNDLE_FILES):
raise PublishError("release contains conflicting or missing assets")
for item in listed:
if not asset_matches(commands, item, assets / item["name"], version):
if not asset_matches(commands, item, assets / item["name"], release["tag_name"]):
raise PublishError("existing release asset differs from the verified bundle")
return set(names)

Expand Down Expand Up @@ -266,9 +278,16 @@ def publish(root, source, assets, receipt, *, environment=None, runner=subproces
except (AdvertisementError, HomebrewError) as error:
raise PublishError(str(error)) from error
expected["releaseNotesSha256"] = hashlib.sha256(notes.encode()).hexdigest()
existing = release_for_tag(commands, version)
resume_id = None
if advertisement_path.exists():
if not advertisement_resume_available(advertisement_path, expected_commit=source, expected_version=version):
raise PublishError("invalid advertisement receipt; manual inspection required")
resume_id = json.loads(advertisement_path.read_text())["releaseId"]
existing = release_for_tag(commands, version, release_id=resume_id)
if existing:
checked_assets(commands, existing, source, version, notes, assets, complete=existing.get("draft") is False)
checked_assets(commands, existing, source, version, notes, assets,
complete=resume_id is not None or existing.get("draft") is False,
allow_untagged_draft=resume_id is not None)
advance_refs(commands, source, intent)
gateway_ready = False
def ready():
Expand All @@ -280,17 +299,18 @@ def ready():
bundle_verifier(source, version, assets)
if not existing or existing["draft"]:
ready()
release = release_for_tag(commands, version)
release = release_for_tag(commands, version, release_id=resume_id)
if release is None:
release = commands.api(f"{API}/releases", method="POST", payload={"tag_name": version,
"target_commitish": source, "name": version, "body": notes, "draft": True,
"prerelease": False, "make_latest": "false"})
if release.get("draft"):
if release.get("draft") and resume_id is None:
present = checked_assets(commands, release, source, version, notes, assets)
for name in sorted(BUNDLE_FILES - present):
commands.run(("gh", "release", "upload", version, str(assets / name), "--repo", REPOSITORY), timeout=600)
bundle_verifier(source, version, assets)
checked_assets(commands, release, source, version, notes, assets, complete=True)
checked_assets(commands, release, source, version, notes, assets, complete=True,
allow_untagged_draft=resume_id is not None)
revalidate_refs(root, source, commands)
try:
advertise(root, advertisement_path, expected=expected, api=api_factory(commands), env=commands.environment,
Expand Down
38 changes: 38 additions & 0 deletions scripts/release/ci_release_publish_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, assets, notes):
self.tap_files = {"rhizome.rb": self.old_recipes["rhizome.rb"]}
self.latest_id = None
self.fail_anonymous = False
self.bad_latest = False
self.fail_upload = None
self.fail_brew = False
self.fail_brew_path = None
Expand Down Expand Up @@ -121,6 +122,8 @@ def run(self, argv, **kwargs):
self.events.append("create")
self.release = {**payload, "id": 1}
value = self.release
elif endpoint.endswith("/releases/1") and method == "GET":
value = self.release
elif "/assets?" in endpoint:
value = list(self.uploaded.values())
elif method == "PATCH":
Expand Down Expand Up @@ -166,6 +169,9 @@ def download(self, url, path, *, size=None, authenticated=False, limit=None):
elif "/latest/download/" in url:
if not self.latest_id:
raise AdvertisementError("no latest release")
if self.bad_latest:
path.write_bytes(b"{}")
return
name = "latest.json"
else:
if self.fail_anonymous:
Expand Down Expand Up @@ -254,6 +260,38 @@ def test_anonymous_asset_failure_defers_both_recipes_and_retry_does_not_reupload
self.assertLess(self.host.events.index("promote"), self.host.events.index("brew"))
self.assertIn("Casks/rhizome-desktop.rb", self.host.tap_files)

def test_recovered_untagged_draft_resumes_without_duplicate_release_or_reupload(self):
self.host.bad_latest = True
with unittest.mock.patch("release_advertise.time.sleep"):
with self.assertRaisesRegex(PublishError, "advertisement failed"):
self.publish()
self.assertTrue(self.host.release["draft"])
self.assertIsNone(self.host.latest_id)
self.host.release["tag_name"] = "untagged-072ac595e1bcffa8331c"
self.host.bad_latest = False
self.host.events.clear()
self.publish()
self.assertNotIn("create", self.host.events)
self.assertNotIn("upload", self.host.events)
self.assertNotIn("recover", self.host.events)
self.assertEqual(self.host.release["tag_name"], VERSION)
self.assertFalse(self.host.release["draft"])
self.assertEqual(self.host.latest_id, 1)
self.assertIn("brew", self.host.events)

def test_receipt_pinned_untagged_draft_missing_asset_fails_without_patch_or_upload(self):
self.host.bad_latest = True
with unittest.mock.patch("release_advertise.time.sleep"):
with self.assertRaisesRegex(PublishError, "advertisement failed"):
self.publish()
self.host.release["tag_name"] = "untagged-072ac595e1bcffa8331c"
self.host.uploaded.pop(next(iter(BUNDLE_FILES)))
self.host.bad_latest = False
self.host.events.clear()
with self.assertRaisesRegex(PublishError, "missing assets"):
self.publish()
self.assertEqual(self.host.events, [])

def test_conflicting_refs_prevent_gateway_and_upload(self):
self.host.remote_release = "b" * 40
with self.assertRaisesRegex(PublishError, "conflicting"): self.publish()
Expand Down
Loading
Loading