fix(release): bind recovered artifacts to successful producer jobs
Gitea CI / Check code and tests (pull_request) Failing after 8m58s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 3s

This commit is contained in:
lingjhf committed 2026-10-12 04:19:44 +08:00
1 parent 06f1c26681
commit 3022b3a5e2
5 files changed
+105 -14

No files matched your search

+24 -5
View File
@@ -1,6 +1,7 @@
"""Upload platform builds to a commit-bound draft, then verify and publish it."""
import argparse
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
import hashlib
import importlib.util
import io
@@ -22,7 +23,7 @@ fingerprint = transport.fingerprint
def context():
base = f"{transport.SERVER}/api/v1/repos/{os.environ['GITEA_REPOSITORY']}"
sha, tag = os.environ['GITEA_SHA'], os.environ['RELEASE_TAG']
version, windows_version = app_versions('pubspec.yaml')
version, windows_version = app_versions(os.environ.get('RELEASE_PUBSPEC', 'pubspec.yaml'))
if tag != f'v{version}':
raise ValueError('Release tag must match pubspec version')
remote_tag = request(f'{base}/tags/{urllib.parse.quote(tag, safe="")}')
@@ -65,8 +66,25 @@ def ci_artifacts(base, run_id):
if not item.get('expired', False)]
def artifact_named(artifacts, name):
matches = [item for item in artifacts if item['name'] == name]
def artifact_named(artifacts, name, run, jobs):
producer = {'cutdex-android-build': 'Build Android APK',
'cutdex-windows-build': 'Audit and package Windows x64',
'cutdex-release-metadata': 'Assemble and validate CI artifacts'}[name]
producers = [job for job in jobs if job['name'] == producer
and job.get('conclusion') == 'success' and job.get('head_sha') == run['head_sha']]
if len(producers) != 1:
raise ValueError('Expected one successful artifact producer: ' + producer)
job = producers[0]
def timestamp(value):
return datetime.fromisoformat(value.replace('Z', '+00:00'))
started, completed = timestamp(job['started_at']), timestamp(job['completed_at'])
# Gitea retains old API rows after overwrite/rerun. Accept only the artifact
# uploaded within the current successful producer's interval; cloned jobs
# retain their original interval and therefore reuse their original bytes.
matches = [item for item in artifacts if item['name'] == name
and item.get('workflow_run', {}).get('id') == run['id']
and item['workflow_run'].get('head_sha') == run['head_sha']
and started <= timestamp(item['created_at']) <= completed]
if len(matches) != 1:
raise ValueError('Expected one unexpired CI artifact: ' + name)
return matches[0]
@@ -91,8 +109,9 @@ def prepare_ci(base, sha, tag, version, windows_version):
run_id = int(matches[0])
run = successful_ci(base, sha, run_id)
artifacts = ci_artifacts(base, run['id'])
verification = artifact_named(artifacts, 'cutdex-release-metadata')
selected = {platform: artifact_named(artifacts, f'cutdex-{platform}-build')
jobs = request(f"{base}/actions/runs/{run['id']}/jobs")['jobs']
verification = artifact_named(artifacts, 'cutdex-release-metadata', run, jobs)
selected = {platform: artifact_named(artifacts, f'cutdex-{platform}-build', run, jobs)
for platform in ('android', 'windows')}
release = prepare(base, sha, tag, version, windows_version, ci_run=run['id'])
if os.environ.get('GITHUB_OUTPUT'):
+33
View File
@@ -41,6 +41,14 @@ class StagedReleaseTests(unittest.TestCase):
{'id': 11, 'name': 'cutdex-android-build'},
{'id': 12, 'name': 'cutdex-windows-build'},
{'id': 13, 'name': 'cutdex-release-metadata'}]}
for artifact in self.run_artifacts[1]:
artifact.update(created_at='2026-10-11T10:01:00Z',
workflow_run={'id': 1, 'head_sha': self.sha})
self.ci_jobs = {1: [{'name': name, 'head_sha': self.sha, 'conclusion': 'success',
'started_at': '2026-10-11T10:00:00Z',
'completed_at': '2026-10-11T10:02:00Z'} for name in
['Build Android APK', 'Audit and package Windows x64',
'Assemble and validate CI artifacts']]}
self.artifact_contents = {10: self.contents,
11: {'app-production-release.apk': self.contents['cutdex-0.1.0-android.apk']},
12: {name: body for name, body in self.contents.items()
@@ -74,6 +82,8 @@ class StagedReleaseTests(unittest.TestCase):
self.calls.append((method, url))
if url.endswith('/actions/runs?limit=100'):
return {'workflow_runs': self.ci_runs}
if '/actions/runs/' in url and url.endswith('/jobs'):
return {'jobs': self.ci_jobs[int(url.split('/actions/runs/')[1].split('/')[0])]}
if '/actions/runs/' in url and url.endswith('/artifacts'):
return {'artifacts': self.run_artifacts[int(url.split('/actions/runs/')[1].split('/')[0])]}
if '/actions/artifacts/' in url and url.endswith('/zip'):
@@ -262,6 +272,29 @@ class StagedReleaseTests(unittest.TestCase):
self.assertIn(stage.ci_marker(1), self.release['body'])
self.assert_draft()
def test_independent_packaging_retry_uses_current_producers_and_original_android(self):
for job in self.ci_jobs[1][1:]:
job.update(started_at='2026-10-11T11:00:00Z', completed_at='2026-10-11T11:02:00Z')
for old_id, new_id in [(12, 22), (13, 23)]:
old = next(item for item in self.run_artifacts[1] if item['id'] == old_id)
self.run_artifacts[1].append(dict(old, id=new_id, created_at='2026-10-11T11:01:00Z'))
self.run_cli('prepare-ci')
output = (self.root / 'outputs').read_text()
for value in ['artifact-id=23', 'windows-artifact-id=22', 'android-artifact-id=11']:
self.assertIn(value, output)
def test_same_producer_duplicate_or_wrong_source_artifact_is_rejected(self):
current = self.run_artifacts[1][3]
self.run_artifacts[1].append(dict(current, id=99))
with self.assertRaisesRegex(ValueError, 'one unexpired CI artifact'):
self.run_cli('prepare-ci')
self.run_artifacts[1].pop()
for identity in [{'id': 2, 'head_sha': self.sha}, {'id': 1, 'head_sha': 'b' * 40}]:
current['workflow_run'] = identity
with self.assertRaisesRegex(ValueError, 'one unexpired CI artifact'):
self.run_cli('prepare-ci')
self.assertIsNone(self.release)
def test_ci_failure_or_expired_metadata_never_creates_draft(self):
self.ci_runs[0]['conclusion'] = 'failure'
with self.assertRaisesRegex(ValueError, 'No successful main CI'):