ci: deploy private build checks and macOS release publishers

This commit is contained in:
Kevin Yue committed 2026-10-22 23:04:52 +08:00
1 parent 69d67721a6
commit 9b297b14a6
15 files changed
+2330 -166

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Find the latest execution of build-macos, retaining builds across unrelated reruns.
set -euo pipefail
repository=$1
run_id=$2
latest_attempt=$3
[[ "$run_id" =~ ^[0-9]+$ && "$latest_attempt" =~ ^[1-9][0-9]*$ ]]
for ((attempt = latest_attempt; attempt >= 1; attempt--)); do
jobs=$(gh api --paginate "repos/$repository/actions/runs/$run_id/attempts/$attempt/jobs" \
--jq '.jobs[] | select(.name == "build-macos")' | jq -s .)
if [[ $(jq length <<<"$jobs") == 0 ]]; then
continue
fi
if ! jq -e 'length == 1 and .[0].status == "completed" and .[0].conclusion == "success"' <<<"$jobs" >/dev/null; then
echo "The latest macOS job must have completed successfully." >&2
exit 1
fi
artifact_name="macos-release-$run_id.$attempt.macos"
artifacts=$(gh api --paginate "repos/$repository/actions/runs/$run_id/artifacts" \
--jq '.artifacts[]' | jq -s --arg name "$artifact_name" '[.[] | select(.name == $name)]')
if ! jq -e 'length == 1 and .[0].expired == false' <<<"$artifacts" >/dev/null; then
echo "The latest macOS build must have one unexpired artifact." >&2
exit 1
fi
echo "$artifact_name"
exit 0
done
echo "No macOS job was found in this run." >&2
exit 1
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
# Emit only host-owned stage names; never forward private command output.
set -euo pipefail
source_directory=$1
container="gp-private-tests-$$"
trap 'timeout --kill-after=5s 30s docker rm --force "$container" >/dev/null 2>&1 || true' EXIT
run_stage() {
local name=$1 limit=$2 status started=$SECONDS
shift 2
echo "$name: started (limit $limit)."
if timeout --kill-after=30s "$limit" "$@" >/dev/null 2>&1; then
echo "$name: passed ($((SECONDS - started))s)."
else
status=$?
if [[ "$status" == 124 ]]; then
echo "::error::$name: timed out after $((SECONDS - started))s (limit $limit); diagnostics suppressed."
elif [[ "$status" == 137 ]]; then
echo "::error::$name: killed after $((SECONDS - started))s (possible timeout or memory pressure); diagnostics suppressed."
else
echo "::error::$name: failed after $((SECONDS - started))s (exit $status); diagnostics suppressed."
fi
exit "$status"
fi
}
run_stage 'Start test container' 5m docker run --detach --rm \
--name "$container" \
--user vscode \
-e HOME=/home/vscode \
-e CARGO_TARGET_DIR=/tmp/gpgui-test-target \
-e COREPACK_INTEGRITY_KEYS=0 \
-e LIBXML2_STATIC=1 \
-v "$source_directory:/source:ro" \
yuezk/gpdev:binary-builder-tauri2 sleep infinity
# Tauri writes schemas into the checkout; keep the host source read-only.
# shellcheck disable=SC2016 # Expand id inside the container, not on the host.
run_stage 'Prepare test workspace' 5m docker exec "$container" sh -ec '
test "$(id -u)" -ne 0
cp -R /source /tmp/gpgui-test-workspace
'
workspace=/tmp/gpgui-test-workspace/gpgui
run_stage 'Install frontend dependencies' 10m \
docker exec --workdir "$workspace" "$container" pnpm install --frozen-lockfile
run_stage 'Frontend tests' 5m \
docker exec --workdir "$workspace" "$container" pnpm --filter gpgui test
run_stage 'Frontend build' 10m \
docker exec --workdir "$workspace" "$container" pnpm --filter gpgui build
run_stage 'Compile Rust tests' 30m \
docker exec --workdir "$workspace" "$container" cargo test --workspace --locked --no-run
run_stage 'Rust tests' 10m \
docker exec --workdir "$workspace" "$container" cargo test --workspace --locked
+215
View File
@@ -0,0 +1,215 @@
import json
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
import textwrap
import unittest
ROOT = Path(__file__).resolve().parents[2]
def run_block(path, step=None):
source = (ROOT / path).read_text()
if step:
source = source.split('- name: ' + step + '\n', 1)[1]
match = re.search(r'(?m)^( +)run: \|\n', source)
indent = len(match[1]) + 2
lines = []
for line in source[match.end():].splitlines():
if line.strip() and len(line) - len(line.lstrip()) < indent:
break
lines.append(line[indent:])
return '\n'.join(lines)
class PrivateOutputTests(unittest.TestCase):
def test_private_checkout_supports_branch_tag_and_sha_without_retaining_token(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
origin = root / 'origin'
subprocess.run(['git', 'init', '-b', 'main', str(origin)], check=True, capture_output=True)
git_env = dict(os.environ, GIT_AUTHOR_NAME='Test', GIT_AUTHOR_EMAIL='[email protected]',
GIT_COMMITTER_NAME='Test', GIT_COMMITTER_EMAIL='[email protected]')
subprocess.run(['git', '-C', str(origin), 'commit', '--allow-empty', '-m', 'PRIVATE_SUBJECT'],
env=git_env, check=True, capture_output=True)
subprocess.run(['git', '-C', str(origin), 'tag', 'v1.0.0'], check=True)
sha = subprocess.check_output(['git', '-C', str(origin), 'rev-parse', 'HEAD'], text=True).strip()
config = root / 'gitconfig'
config.write_text(f'[url "{origin.as_uri()}"]\n insteadOf = https://github.com/yuezk/gpgui\n')
for index, ref in enumerate(('main', 'v1.0.0', sha)):
destination = root / f'checkout-{index}'
env = dict(os.environ, GIT_CONFIG_GLOBAL=str(config), PRIVATE_TOKEN='test-token',
PRIVATE_REF=ref, PRIVATE_PATH=str(destination))
result = subprocess.run(['bash', '-e', '-o', 'pipefail', '-c',
run_block('.github/actions/checkout-private/action.yaml')],
env=env, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertNotIn('PRIVATE_SUBJECT', result.stdout + result.stderr)
self.assertNotIn('test-token', (destination / '.git/config').read_text())
self.assertNotIn('extraheader', (destination / '.git/config').read_text())
actual = subprocess.check_output(['git', '-C', str(destination), 'rev-parse', 'HEAD'], text=True)
self.assertEqual(actual.strip(), sha)
def test_private_commands_hide_stdout_and_stderr_and_preserve_failure(self):
cases = [
('.github/workflows/build.yaml', 'Build macOS release artifacts'),
('.github/workflows/build.yaml', 'Build gpgui frontend'),
('.github/workflows/build-gpgui-linux.yaml', 'Build gpgui in Docker'),
('.github/workflows/publish-update-feeds.yaml', 'Install Worker tooling'),
('.github/workflows/publish-update-feeds.yaml', 'Deploy update feed worker'),
('.github/actions/checkout-private/action.yaml', None),
]
for path, step in cases:
for status in (0, 19):
with self.subTest(step=step, status=status), tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
stub = '#!/bin/sh\necho PRIVATE_STDOUT\necho PRIVATE_STDERR >&2\necho \"MACOS_STAGE: Signing\"\necho \"MACOS_STAGE: Signing PRIVATE_DETAIL\"\nexit "$TEST_STATUS"\n'
for command in ('git', 'docker', 'pnpm', 'source/gpgui/scripts/build-macos.sh'):
executable = root / command
executable.parent.mkdir(parents=True, exist_ok=True)
executable.write_text(stub)
executable.chmod(0o755)
(root / 'gpgui-frontend/gpgui/apps/desktop').mkdir(parents=True)
(root / 'gpgui/apps/update-feed-worker/public').mkdir(parents=True)
(root / 'updates').mkdir()
(root / 'updates/appcast.xml').write_text('fixture')
env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}',
TEST_STATUS=str(status), CHANNEL='snapshot', GPGUI_BUILDER='fixture',
RUNNER_TEMP=str(root), UPDATES_WORKER_NAME='fixture',
PRIVATE_TOKEN='test-token', PRIVATE_REF='test-ref', PRIVATE_PATH='private')
result = subprocess.run(['bash', '-e', '-c', run_block(path, step)],
cwd=root, env=env, text=True, capture_output=True)
self.assertEqual(result.returncode, status, result.stdout + result.stderr)
self.assertNotIn('PRIVATE_STD', result.stdout + result.stderr)
self.assertNotIn('PRIVATE_DETAIL', result.stdout + result.stderr)
if step == 'Build macOS release artifacts':
self.assertIn('::notice::MACOS_STAGE: Signing', result.stdout)
self.assertNotIn('test-token', result.stdout + result.stderr)
self.assertIn('passed' if status == 0 else 'failed', result.stdout)
class PrivateTestStageTests(unittest.TestCase):
def run_stages(self, mode):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
docker = root / 'docker'
docker.write_text(textwrap.dedent('''\
#!/usr/bin/env python3
import json, os, sys, time
with open(os.environ['CALLS'], 'a') as output:
output.write(json.dumps(sys.argv[1:]) + '\\n')
print('PRIVATE_STDOUT')
print('PRIVATE_STDERR', file=sys.stderr)
if 'cargo' in sys.argv and '--no-run' not in sys.argv:
if os.environ['MODE'] == 'hang':
time.sleep(60)
if os.environ['MODE'] == 'fail':
sys.exit(19)
'''))
timeout = root / 'timeout'
timeout.write_text(textwrap.dedent('''\
#!/usr/bin/env python3
import subprocess, sys
assert sys.argv[1].startswith('--kill-after=')
assert sys.argv[2].endswith(('m', 's'))
try:
result = subprocess.run(sys.argv[3:], timeout=2)
sys.exit(result.returncode)
except subprocess.TimeoutExpired:
sys.exit(124)
'''))
for executable in (docker, timeout):
executable.write_text(executable.read_text().replace("#!/usr/bin/env python3", f"#!{sys.executable}"))
executable.chmod(0o755)
calls = root / 'calls.jsonl'
env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', MODE=mode, CALLS=str(calls))
result = subprocess.run(['bash', str(ROOT / 'scripts/test-private-desktop.sh'), str(root)],
env=env, text=True, capture_output=True, timeout=10)
self.assertNotIn('PRIVATE_STD', result.stdout + result.stderr)
recorded = [json.loads(line) for line in calls.read_text().splitlines()]
self.assertEqual(recorded[-1][:2], ['rm', '--force'])
return result
def test_success_reports_each_stage(self):
result = self.run_stages('success')
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertEqual(result.stdout.count(': started'), 7)
self.assertEqual(result.stdout.count(': passed'), 7)
self.assertIn('Compile Rust tests: passed', result.stdout)
self.assertIn('Rust tests: passed', result.stdout)
def test_failure_names_stage_and_preserves_exit(self):
result = self.run_stages('fail')
self.assertEqual(result.returncode, 19)
self.assertIn('Rust tests: failed', result.stdout)
self.assertNotRegex(result.stdout, r'(?m)^Rust tests: passed')
def test_hung_tests_report_timeout_and_cleanup(self):
result = self.run_stages('hang')
self.assertEqual(result.returncode, 124)
self.assertIn('Rust tests: timed out', result.stdout)
self.assertNotRegex(result.stdout, r'(?m)^Rust tests: passed')
class MacOSArtifactSelectionTests(unittest.TestCase):
def select(self, jobs, artifacts=None):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
gh = root / 'gh'
gh.write_text(textwrap.dedent('''\
#!/usr/bin/env python3
import json, os, re, sys
endpoint = sys.argv[3]
if endpoint.endswith('/artifacts'):
records = json.loads(os.environ['ARTIFACTS'])
else:
attempt = re.search(r'/attempts/([0-9]+)/jobs', endpoint)[1]
records = json.loads(os.environ['JOBS']).get(attempt, [])
for record in records:
print(json.dumps(record))
'''))
gh.chmod(0o755)
env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', JOBS=json.dumps(jobs),
ARTIFACTS=json.dumps(artifacts if artifacts is not None else [
{'name': 'macos-release-42.1.macos', 'expired': False}]))
return subprocess.run(['bash', str(ROOT / 'scripts/select-macos-artifact.sh'), 'owner/repo', '42', '3'],
env=env, capture_output=True, text=True)
def job(self, conclusion='success', status='completed'):
return {'name': 'build-macos', 'status': status, 'conclusion': conclusion}
def test_unrelated_reruns_reuse_successful_macos_attempt(self):
result = self.select({'1': [self.job()]})
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.strip(), 'macos-release-42.1.macos')
def test_newer_macos_execution_wins(self):
result = self.select({'1': [self.job()], '3': [self.job()]}, [
{'name': f'macos-release-42.{attempt}.macos', 'expired': False} for attempt in (1, 3)])
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.strip(), 'macos-release-42.3.macos')
def test_never_falls_back_past_failed_cancelled_skipped_or_running_macos(self):
for conclusion, status in [('failure', 'completed'), ('cancelled', 'completed'),
('skipped', 'completed'), (None, 'in_progress')]:
with self.subTest(conclusion=conclusion):
result = self.select({'1': [self.job()], '2': [self.job(conclusion, status)]})
self.assertNotEqual(result.returncode, 0)
self.assertEqual(result.stdout, '')
def test_missing_expired_or_duplicate_artifacts_are_rejected(self):
valid = {'name': 'macos-release-42.1.macos', 'expired': False}
for artifacts in ([], [dict(valid, expired=True)], [valid, valid]):
with self.subTest(artifacts=artifacts):
self.assertNotEqual(self.select({'1': [self.job()]}, artifacts).returncode, 0)
def test_missing_macos_job_is_rejected(self):
self.assertNotEqual(self.select({}).returncode, 0)
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,116 @@
import subprocess
import tempfile
import unittest
from pathlib import Path
REPOSITORY = "yuezk/GlobalProtect-openconnect"
SCRIPT = Path(__file__).parents[1] / "validate-macos-appcast.py"
def item(version, short_version, archive, channel=None):
channel_xml = f"<sparkle:channel>{channel}</sparkle:channel>" if channel else ""
return f"""
<item>
<sparkle:version>{version}</sparkle:version>
<sparkle:shortVersionString>{short_version}</sparkle:shortVersionString>
{channel_xml}
<enclosure url="{archive}" length="100" sparkle:edSignature="signature" />
</item>
"""
def appcast(*items):
return f"""<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
<channel>{''.join(items)}</channel>
</rss>
"""
class ValidateMacosAppcastTests(unittest.TestCase):
def run_validator(self, content, *arguments):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "appcast.xml"
path.write_text(content, encoding="utf-8")
return subprocess.run(
["python3", str(SCRIPT), *arguments, str(path), "--repository", REPOSITORY],
check=False,
capture_output=True,
text=True,
)
def test_reports_candidate_status(self):
archive = f"https://github.com/{REPOSITORY}/releases/download/v2.6.4/GPConnect_2.6.4_arm64.zip"
content = appcast(item("806.1", "2.6.4", archive))
same = self.run_validator(
content, "status", "--build-version", "806.1"
)
newer = self.run_validator(
content, "status", "--build-version", "807.1"
)
older = self.run_validator(
content, "status", "--build-version", "805.1"
)
self.assertEqual((same.returncode, same.stdout.strip()), (0, "same"))
self.assertEqual((newer.returncode, newer.stdout.strip()), (0, "newer"))
self.assertEqual((older.returncode, older.stdout.strip()), (0, "older"))
def test_validates_stable_and_snapshot_items(self):
stable = f"https://github.com/{REPOSITORY}/releases/download/v2.6.4/GPConnect_2.6.4_arm64.zip"
snapshot = (
f"https://github.com/{REPOSITORY}/releases/download/snapshot/"
"GPConnect_2.6.4-snapshot.807.1_arm64.zip"
)
content = appcast(
item("806.1", "2.6.4", stable),
item("807.1", "2.6.4", snapshot, "snapshot"),
)
result = self.run_validator(
content,
"validate",
"--build-version",
"807.1",
"--short-version",
"2.6.4",
"--channel",
"snapshot",
"--archive-url",
snapshot,
)
self.assertEqual(result.returncode, 0, result.stderr)
def test_compares_builds_across_channels(self):
stable = f"https://github.com/{REPOSITORY}/releases/download/v2.6.4/GPConnect_2.6.4_arm64.zip"
snapshot = (
f"https://github.com/{REPOSITORY}/releases/download/snapshot/"
"GPConnect_2.6.4-snapshot.807.1_arm64.zip"
)
content = appcast(
item("2.6.4", "2.6.4", stable),
item("807.1", "2.6.4", snapshot, "snapshot"),
)
result = self.run_validator(
content, "status", "--build-version", "2.7.0"
)
self.assertEqual((result.returncode, result.stdout.strip()), (0, "older"))
def test_rejects_external_archive_urls(self):
content = appcast(item("806.1", "2.6.4", "https://example.com/update.zip"))
result = self.run_validator(
content, "status", "--build-version", "807.1"
)
self.assertNotEqual(result.returncode, 0)
self.assertIn("unexpected update archive URL", result.stderr)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,99 @@
import hashlib
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
spec = importlib.util.spec_from_file_location("macos_release", Path(__file__).parents[1] / "validate-macos-release.py")
release = importlib.util.module_from_spec(spec)
spec.loader.exec_module(release)
class MacOSReleaseTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.directory = Path(self.temp.name)
self.commit = "a" * 40
self.metadata = {"gp_ref": "b" * 40, "gpgui_ref": "d" * 40, "workflow_sha": self.commit, "channel": "stable",
"release_tag": "v3.0.1", "artifact_version": "3.0.1",
"request_id": "42.1.macos", "build_version": "903.1", "assets": {}}
for extension in ("dmg", "zip"):
name = f"GPConnect_3.0.1_arm64.{extension}"
content = b"signed artifact"
digest = hashlib.sha256(content).hexdigest()
(self.directory / name).write_bytes(content)
checksum = f"{digest} {name}\n".encode()
(self.directory / (name + ".sha256")).write_bytes(checksum)
self.metadata["assets"][name] = digest
self.metadata["assets"][name + ".sha256"] = hashlib.sha256(checksum).hexdigest()
self.save()
def save(self):
(self.directory / "release-build.json").write_text(json.dumps(self.metadata))
def test_matching_build_passes(self):
self.assertEqual(release.validate(self.directory, "v3.0.1", self.commit), self.metadata)
def test_another_release_cannot_use_this_build(self):
with self.assertRaisesRegex(ValueError, "release tag"):
release.validate(self.directory, "v3.0.2", self.commit)
def test_another_run_commit_cannot_use_this_build(self):
with self.assertRaisesRegex(ValueError, "public workflow"):
release.validate(self.directory, "v3.0.1", "c" * 40)
def test_source_commit_cannot_replace_workflow_identity(self):
with self.assertRaisesRegex(ValueError, "public workflow"):
release.validate(self.directory, "v3.0.1", self.metadata["gp_ref"])
def test_invalid_workflow_commit_is_rejected(self):
self.metadata["workflow_sha"] = "short"
self.save()
with self.assertRaisesRegex(ValueError, "Invalid workflow_sha"):
release.validate(self.directory, "v3.0.1", "short")
def test_corrupt_download_is_rejected(self):
(self.directory / "GPConnect_3.0.1_arm64.zip").write_bytes(b"changed")
with self.assertRaisesRegex(ValueError, "checksum mismatch"):
release.validate(self.directory, "v3.0.1", self.commit)
def test_unexpected_file_is_rejected(self):
(self.directory / "unexpected.dmg").write_bytes(b"extra")
with self.assertRaisesRegex(ValueError, "Unexpected"):
release.validate(self.directory, "v3.0.1", self.commit)
def test_missing_checksum_is_rejected(self):
self.metadata["assets"].pop("GPConnect_3.0.1_arm64.zip.sha256")
self.save()
with self.assertRaisesRegex(ValueError, "exactly"):
release.validate(self.directory, "v3.0.1", self.commit)
def test_snapshot_requires_matching_build_identity(self):
self.metadata.update(channel="snapshot", release_tag="snapshot",
artifact_version="3.0.1-snapshot.903.2")
self.save()
with self.assertRaisesRegex(ValueError, "snapshot artifact version"):
release.validate(self.directory, "snapshot", self.commit)
def test_matching_snapshot_passes(self):
version = "3.0.1-snapshot.903.1"
assets = {}
for name in self.metadata["assets"]:
file = self.directory / name
new_name = name.replace("3.0.1", version)
content = file.read_bytes()
if name.endswith(".sha256"):
content = content.replace(b"3.0.1", version.encode())
file.unlink()
(self.directory / new_name).write_bytes(content)
assets[new_name] = hashlib.sha256(content).hexdigest()
self.metadata.update(channel="snapshot", release_tag="snapshot",
artifact_version=version, assets=assets)
self.save()
self.assertEqual(release.validate(self.directory, "snapshot", self.commit), self.metadata)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,177 @@
import importlib.util
import json
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
MODULE_PATH = Path(__file__).parents[1] / "validate-update-manifest.py"
SPEC = importlib.util.spec_from_file_location("validate_update_manifest", MODULE_PATH)
MODULE = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(MODULE)
REPOSITORY = "yuezk/GlobalProtect-openconnect"
class UpdateManifestTests(unittest.TestCase):
def stable(self, version="2.6.5"):
return {
"version": version,
"releasedAt": "2026-08-01",
"releaseUrl": f"https://github.com/{REPOSITORY}/releases/tag/v{version}",
"platforms": ["linux", "freebsd", "openbsd"],
"highlights": ["Stable release."],
}
def snapshot(self, base_version="2.6.5", build="17.1"):
return {
"baseVersion": base_version,
"build": build,
"releasedAt": "2026-08-07",
"releaseUrl": f"https://github.com/{REPOSITORY}/releases/tag/snapshot",
"platforms": ["linux", "freebsd", "openbsd"],
"highlights": ["Development snapshot."],
}
def manifest(self):
return {"schemaVersion": 1, "stable": self.stable(), "snapshot": self.snapshot()}
def generate(self, directory, channel, version, build=None, input_manifest=None):
directory = Path(directory)
highlights = directory / f"{channel}-highlights.txt"
output = directory / "latest.json"
highlights.write_text(f"{channel.title()} change.\n", encoding="utf-8")
args = type(
"Args",
(),
{
"repository": REPOSITORY,
"channel": channel,
"version": version,
"build": build,
"platform": None,
"released_at": "2026-08-07",
"highlights_file": highlights,
"input": input_manifest,
"output": output,
},
)()
MODULE.generate(args)
return json.loads(output.read_text(encoding="utf-8"))
def test_validates_catalog_with_both_channels(self):
result = MODULE.validate_manifest(self.manifest(), REPOSITORY)
self.assertEqual(result["stable"]["version"], "2.6.5")
self.assertEqual(result["snapshot"]["build"], "17.1")
def test_accepts_catalog_with_one_channel(self):
manifest = self.manifest()
manifest["stable"] = None
MODULE.validate_manifest(manifest, REPOSITORY)
def test_rejects_empty_catalog_and_unknown_fields(self):
with self.assertRaisesRegex(ValueError, "stable or snapshot"):
MODULE.validate_manifest({"schemaVersion": 1, "stable": None, "snapshot": None}, REPOSITORY)
manifest = self.manifest()
manifest["extra"] = True
with self.assertRaisesRegex(ValueError, "top-level"):
MODULE.validate_manifest(manifest, REPOSITORY)
def test_rejects_invalid_channel_urls(self):
manifest = self.manifest()
manifest["stable"]["releaseUrl"] = f"https://github.com/{REPOSITORY}/releases/tag/snapshot"
with self.assertRaisesRegex(ValueError, "stable releaseUrl"):
MODULE.validate_manifest(manifest, REPOSITORY)
manifest = self.manifest()
manifest["snapshot"]["releaseUrl"] = f"https://github.com/{REPOSITORY}/releases/tag/v2.6.5"
with self.assertRaisesRegex(ValueError, "snapshot releaseUrl"):
MODULE.validate_manifest(manifest, REPOSITORY)
def test_rejects_invalid_snapshot_build(self):
for build in ("", "17.beta", "01.1", "1.2.3.4", f"{2**64}.1"):
with self.subTest(build=build):
manifest = self.manifest()
manifest["snapshot"]["build"] = build
with self.assertRaisesRegex(ValueError, "snapshot build"):
MODULE.validate_manifest(manifest, REPOSITORY)
def test_rejects_version_components_larger_than_the_client_supports(self):
manifest = self.manifest()
manifest["stable"] = self.stable(f"{2**64}.0.0")
with self.assertRaisesRegex(ValueError, "integer range"):
MODULE.validate_manifest(manifest, REPOSITORY)
def test_rejects_unknown_and_duplicate_platforms(self):
manifest = self.manifest()
manifest["stable"]["platforms"] = ["linux", "netbsd"]
with self.assertRaisesRegex(ValueError, "unsupported"):
MODULE.validate_manifest(manifest, REPOSITORY)
manifest["stable"]["platforms"] = ["linux", "linux"]
with self.assertRaisesRegex(ValueError, "duplicates"):
MODULE.validate_manifest(manifest, REPOSITORY)
def test_generates_and_merges_channels(self):
with tempfile.TemporaryDirectory() as directory:
generated = self.generate(directory, "stable", "2.6.5")
self.assertIsNone(generated["snapshot"])
self.assertEqual(generated["stable"]["platforms"], ["linux", "freebsd", "openbsd"])
current = Path(directory) / "current.json"
current.write_text(json.dumps(generated), encoding="utf-8")
generated = self.generate(directory, "snapshot", "2.6.5", "17.1", current)
self.assertEqual(generated["stable"]["version"], "2.6.5")
self.assertEqual(generated["snapshot"]["baseVersion"], "2.6.5")
self.assertEqual(generated["snapshot"]["build"], "17.1")
def test_cli_limits_stable_to_published_platforms_and_preserves_snapshot(self):
with tempfile.TemporaryDirectory() as directory:
directory = Path(directory)
current = directory / "current.json"
current.write_text(json.dumps(self.manifest()), encoding="utf-8")
highlights = directory / "highlights.txt"
highlights.write_text("Refreshed client icons.\n", encoding="utf-8")
output = directory / "latest.json"
result = subprocess.run(
[sys.executable, str(MODULE_PATH), "generate", "--repository", REPOSITORY,
"--channel", "stable", "--version", "3.0.5", "--platform", "linux",
"--released-at", "2026-10-13", "--highlights-file", str(highlights),
"--input", str(current), "--output", str(output)],
capture_output=True, text=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
generated = json.loads(output.read_text(encoding="utf-8"))
self.assertEqual(generated["stable"]["platforms"], ["linux"])
self.assertEqual(generated["snapshot"], self.snapshot())
def test_rejects_stable_and_snapshot_rollbacks(self):
with tempfile.TemporaryDirectory() as directory:
current = Path(directory) / "current.json"
current.write_text(json.dumps(self.manifest()), encoding="utf-8")
with self.assertRaisesRegex(ValueError, "stable version"):
self.generate(directory, "stable", "2.6.4", input_manifest=current)
with self.assertRaisesRegex(ValueError, "snapshot release"):
self.generate(directory, "snapshot", "2.6.5", "16.1", current)
def test_allows_new_stable_to_supersede_snapshot_base(self):
with tempfile.TemporaryDirectory() as directory:
current = Path(directory) / "current.json"
current.write_text(json.dumps(self.manifest()), encoding="utf-8")
generated = self.generate(directory, "stable", "2.6.6", input_manifest=current)
self.assertEqual(generated["stable"]["version"], "2.6.6")
self.assertEqual(generated["snapshot"]["baseVersion"], "2.6.5")
if __name__ == "__main__":
unittest.main()
+142
View File
@@ -0,0 +1,142 @@
#!/usr/bin/env python3
import argparse
import xml.etree.ElementTree as ET
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
SPARKLE_NAMESPACE = "http://www.andymatuschak.org/xml-namespaces/sparkle"
SUPPORTED_CHANNELS = {None, "snapshot"}
@dataclass(frozen=True)
class AppcastItem:
version: str
short_version: str
channel: Optional[str]
archive_url: str
signature: str
def parse_version(value: str) -> tuple[int, int, int]:
parts = value.split(".")
if not 1 <= len(parts) <= 3 or any(not part.isdigit() for part in parts):
raise ValueError(f"invalid build version: {value}")
numbers = tuple(map(int, parts))
return numbers + (0,) * (3 - len(numbers))
def parse_appcast(path: Path, repository: str) -> list[AppcastItem]:
try:
root = ET.parse(path)
except (ET.ParseError, OSError) as error:
raise ValueError(f"invalid appcast XML: {error}") from error
download_prefix = f"https://github.com/{repository}/releases/download/"
items = []
channel_counts: dict[Optional[str], int] = {}
versions = set()
for element in root.findall("./channel/item"):
version = element.findtext(f"{{{SPARKLE_NAMESPACE}}}version") or ""
short_version = element.findtext(f"{{{SPARKLE_NAMESPACE}}}shortVersionString") or ""
channel = element.findtext(f"{{{SPARKLE_NAMESPACE}}}channel")
enclosure = element.find("enclosure")
if not version or not short_version or enclosure is None:
raise ValueError("appcast item is missing required version or enclosure metadata")
parse_version(version)
if channel not in SUPPORTED_CHANNELS:
raise ValueError(f"unsupported Sparkle channel: {channel}")
archive_url = enclosure.get("url", "")
signature = enclosure.get(f"{{{SPARKLE_NAMESPACE}}}edSignature", "")
length = enclosure.get("length", "")
if not archive_url.startswith(download_prefix):
raise ValueError(f"unexpected update archive URL: {archive_url}")
if not signature:
raise ValueError(f"update archive {archive_url} has no EdDSA signature")
if not length.isdigit() or int(length) <= 0:
raise ValueError(f"update archive {archive_url} has an invalid length")
if version in versions:
raise ValueError(f"duplicate build version in appcast: {version}")
versions.add(version)
channel_counts[channel] = channel_counts.get(channel, 0) + 1
items.append(AppcastItem(version, short_version, channel, archive_url, signature))
if not items:
raise ValueError("appcast contains no update items")
for channel, count in channel_counts.items():
if count > 1:
name = channel or "stable"
raise ValueError(f"appcast contains more than one {name} update")
return items
def command_status(args: argparse.Namespace) -> None:
candidate = parse_version(args.build_version)
published = max(
parse_version(item.version)
for item in parse_appcast(args.appcast, args.repository)
)
if candidate < published:
print("older")
elif candidate == published:
print("same")
else:
print("newer")
def command_validate(args: argparse.Namespace) -> None:
expected_channel = None if args.channel == "stable" else args.channel
matches = [
item
for item in parse_appcast(args.appcast, args.repository)
if parse_version(item.version) == parse_version(args.build_version)
]
if len(matches) != 1:
raise ValueError(f"expected one appcast item for build {args.build_version}")
item = matches[0]
if item.version != args.build_version:
raise ValueError(f"appcast uses a non-canonical build version: {item.version}")
if item.short_version != args.short_version:
raise ValueError(f"unexpected short version: {item.short_version}")
if item.channel != expected_channel:
raise ValueError(f"unexpected Sparkle channel: {item.channel}")
if item.archive_url != args.archive_url:
raise ValueError(f"unexpected update archive URL: {item.archive_url}")
def parser() -> argparse.ArgumentParser:
root = argparse.ArgumentParser(description="Validate the GP Connect Sparkle appcast")
subcommands = root.add_subparsers(dest="command", required=True)
status = subcommands.add_parser("status", help="compare a candidate build with the published feed")
status.add_argument("appcast", type=Path)
status.add_argument("--repository", required=True)
status.add_argument("--build-version", required=True)
status.set_defaults(handler=command_status)
validate = subcommands.add_parser("validate", help="validate a specific update item and the complete feed")
validate.add_argument("appcast", type=Path)
validate.add_argument("--repository", required=True)
validate.add_argument("--build-version", required=True)
validate.add_argument("--short-version", required=True)
validate.add_argument("--channel", choices=("stable", "snapshot"), required=True)
validate.add_argument("--archive-url", required=True)
validate.set_defaults(handler=command_validate)
return root
def main() -> None:
args = parser().parse_args()
try:
args.handler(args)
except ValueError as error:
raise SystemExit(str(error)) from error
if __name__ == "__main__":
main()
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
"""Validate the identity and checksums of a completed public macOS build."""
import argparse
import hashlib
import json
from pathlib import Path
import re
def validate(directory, release_tag, workflow_commit):
metadata = json.loads((directory / "release-build.json").read_text())
for name in ("gp_ref", "gpgui_ref", "workflow_sha"):
if not re.fullmatch(r"[0-9a-f]{40}", metadata[name]):
raise ValueError(f"Invalid {name}")
if metadata["workflow_sha"] != workflow_commit:
raise ValueError("Artifact identity does not match its public workflow run")
if metadata["release_tag"] != release_tag:
raise ValueError("Artifact release tag does not match the requested release")
if not re.fullmatch(r"[0-9]+\.[0-9]+", metadata["build_version"]):
raise ValueError("Invalid build version")
if not re.fullmatch(r"[0-9]+\.[0-9]+\.macos", metadata["request_id"]):
raise ValueError("Invalid build request ID")
version = metadata["artifact_version"]
if metadata["channel"] == "stable":
if not re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+", release_tag):
raise ValueError("Invalid stable tag")
if version != release_tag[1:]:
raise ValueError("Artifact version does not match the stable tag")
elif metadata["channel"] == "snapshot":
if release_tag != "snapshot" or not re.fullmatch(
r"[0-9]+\.[0-9]+\.[0-9]+-snapshot\." + re.escape(metadata["build_version"]), version
):
raise ValueError("Invalid snapshot artifact version")
else:
raise ValueError("Invalid channel")
expected = {f"GPConnect_{version}_arm64.{extension}" for extension in ("dmg", "zip")}
expected |= {name + ".sha256" for name in expected}
if set(metadata["assets"]) != expected:
raise ValueError("Build must contain exactly the DMG, ZIP, and their checksums")
if {file.name for file in directory.iterdir()} != expected | {"release-build.json"}:
raise ValueError("Unexpected or missing artifact files")
for name, digest in metadata["assets"].items():
file = directory / name
if file.is_symlink() or hashlib.sha256(file.read_bytes()).hexdigest() != digest:
raise ValueError(f"Artifact checksum mismatch: {name}")
for name in expected:
if name.endswith(".sha256"):
fields = (directory / name).read_text().split()
if fields != [metadata["assets"][name[:-7]], name[:-7]]:
raise ValueError(f"Invalid checksum file: {name}")
return metadata
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("directory", type=Path)
parser.add_argument("--release-tag", required=True)
parser.add_argument("--workflow-commit", required=True)
args = parser.parse_args()
try:
result = validate(args.directory, args.release_tag, args.workflow_commit)
except (ValueError, KeyError, OSError) as error:
parser.exit(1, f"{error}\n")
print(json.dumps(result))
+219
View File
@@ -0,0 +1,219 @@
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import datetime
import json
import re
import unicodedata
from pathlib import Path
from urllib.parse import urlparse
ALLOWED_PLATFORMS = {"linux", "freebsd", "openbsd"}
SEMVER = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$")
BUILD = re.compile(r"^(0|[1-9][0-9]*)(?:\.(0|[1-9][0-9]*)){0,2}$")
EXPECTED_TOP_LEVEL_KEYS = {"schemaVersion", "stable", "snapshot"}
EXPECTED_STABLE_KEYS = {"version", "releasedAt", "releaseUrl", "platforms", "highlights"}
EXPECTED_SNAPSHOT_KEYS = {"baseVersion", "build", "releasedAt", "releaseUrl", "platforms", "highlights"}
MAX_U64 = 2**64 - 1
def version_tuple(version: str) -> tuple[int, int, int]:
match = SEMVER.fullmatch(version)
if not match:
raise ValueError("version must be stable semantic versioning")
parts = tuple(int(part) for part in match.groups())
if any(part > MAX_U64 for part in parts):
raise ValueError("version component exceeds the supported integer range")
return parts
def build_tuple(build: str) -> tuple[int, int, int]:
if not BUILD.fullmatch(build):
raise ValueError("snapshot build must contain one to three numeric components")
parts = [int(part) for part in build.split(".")]
if any(part > MAX_U64 for part in parts):
raise ValueError("snapshot build component exceeds the supported integer range")
parts.extend([0] * (3 - len(parts)))
return parts[0], parts[1], parts[2]
def validate_release_fields(release: dict) -> None:
try:
datetime.date.fromisoformat(release["releasedAt"])
except (TypeError, ValueError) as error:
raise ValueError("releasedAt must be an ISO calendar date") from error
parsed_url = urlparse(release["releaseUrl"])
if parsed_url.scheme != "https" or parsed_url.hostname != "github.com":
raise ValueError("releaseUrl must use GitHub HTTPS")
platforms = release["platforms"]
if not isinstance(platforms, list) or not platforms:
raise ValueError("platforms must be a non-empty list")
if any(not isinstance(platform, str) or platform not in ALLOWED_PLATFORMS for platform in platforms):
raise ValueError("platforms contains an unsupported target")
if len(set(platforms)) != len(platforms):
raise ValueError("platforms contains duplicates")
highlights = release["highlights"]
if not isinstance(highlights, list) or not 1 <= len(highlights) <= 6:
raise ValueError("highlights must contain between one and six entries")
for highlight in highlights:
if not isinstance(highlight, str) or not highlight.strip():
raise ValueError("highlights must be non-empty strings")
if len(highlight) > 160:
raise ValueError("highlight exceeds 160 characters")
if any(unicodedata.category(character) == "Cc" for character in highlight):
raise ValueError("highlight contains control characters")
def validate_stable(release: object, repository: str) -> dict | None:
if release is None:
return None
if not isinstance(release, dict) or set(release) != EXPECTED_STABLE_KEYS:
raise ValueError("stable release has unexpected fields")
version = release["version"]
if not isinstance(version, str):
raise ValueError("stable version must be a string")
version_tuple(version)
expected_url = f"https://github.com/{repository}/releases/tag/v{version}"
if release["releaseUrl"] != expected_url:
raise ValueError(f"stable releaseUrl must be {expected_url}")
validate_release_fields(release)
return release
def validate_snapshot(release: object, repository: str) -> dict | None:
if release is None:
return None
if not isinstance(release, dict) or set(release) != EXPECTED_SNAPSHOT_KEYS:
raise ValueError("snapshot release has unexpected fields")
base_version = release["baseVersion"]
if not isinstance(base_version, str):
raise ValueError("snapshot baseVersion must be a string")
version_tuple(base_version)
build = release["build"]
if not isinstance(build, str):
raise ValueError("snapshot build must be a string")
build_tuple(build)
expected_url = f"https://github.com/{repository}/releases/tag/snapshot"
if release["releaseUrl"] != expected_url:
raise ValueError(f"snapshot releaseUrl must be {expected_url}")
validate_release_fields(release)
return release
def validate_manifest(data: object, repository: str) -> dict:
if not isinstance(data, dict) or set(data) != EXPECTED_TOP_LEVEL_KEYS:
raise ValueError("manifest has unexpected top-level fields")
if type(data["schemaVersion"]) is not int or data["schemaVersion"] != 1:
raise ValueError("unsupported schemaVersion")
stable = validate_stable(data["stable"], repository)
snapshot = validate_snapshot(data["snapshot"], repository)
if stable is None and snapshot is None:
raise ValueError("manifest must contain a stable or snapshot release")
return data
def load_highlights(path: Path) -> list[str]:
highlights = [line.strip() for line in path.read_text(encoding="utf-8").splitlines() if line.strip()]
if not highlights:
raise ValueError("at least one release highlight is required")
return highlights
def generate(args: argparse.Namespace) -> None:
if args.input is None:
data = {"schemaVersion": 1, "stable": None, "snapshot": None}
else:
data = validate_manifest(json.loads(args.input.read_text(encoding="utf-8")), args.repository)
common = {
"releasedAt": args.released_at,
"platforms": args.platform if args.platform is not None else ["linux", "freebsd", "openbsd"],
"highlights": load_highlights(args.highlights_file),
}
if args.channel == "stable":
if args.build is not None:
raise ValueError("stable releases must not specify a snapshot build")
current = data["stable"]
if current is not None and version_tuple(args.version) < version_tuple(current["version"]):
raise ValueError(f"stable version {args.version} is older than {current['version']}")
data["stable"] = {
"version": args.version,
"releaseUrl": f"https://github.com/{args.repository}/releases/tag/v{args.version}",
**common,
}
else:
if args.build is None:
raise ValueError("snapshot releases require a build")
candidate = (version_tuple(args.version), build_tuple(args.build))
current = data["snapshot"]
if current is not None:
published = (version_tuple(current["baseVersion"]), build_tuple(current["build"]))
if candidate < published:
raise ValueError("snapshot release is older than the published snapshot")
data["snapshot"] = {
"baseVersion": args.version,
"build": args.build,
"releaseUrl": f"https://github.com/{args.repository}/releases/tag/snapshot",
**common,
}
validate_manifest(data, args.repository)
args.output.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
def validate(args: argparse.Namespace) -> None:
data = validate_manifest(json.loads(args.manifest.read_text(encoding="utf-8")), args.repository)
if args.channel is None:
return
release = data[args.channel]
if release is None:
raise ValueError(f"manifest does not contain a {args.channel} release")
version_key = "version" if args.channel == "stable" else "baseVersion"
if args.version is not None and release[version_key] != args.version:
raise ValueError(f"{args.channel} version {release[version_key]} does not match {args.version}")
if args.build is not None:
if args.channel != "snapshot":
raise ValueError("only snapshot releases have a build")
if release["build"] != args.build:
raise ValueError(f"snapshot build {release['build']} does not match {args.build}")
def main() -> None:
parser = argparse.ArgumentParser(description="Generate and validate the GP Connect update catalog")
subcommands = parser.add_subparsers(required=True)
generate_parser = subcommands.add_parser("generate")
generate_parser.add_argument("--repository", required=True)
generate_parser.add_argument("--channel", choices=("stable", "snapshot"), required=True)
generate_parser.add_argument("--version", required=True)
generate_parser.add_argument("--platform", action="append", choices=sorted(ALLOWED_PLATFORMS),
help="Published Unix target; repeat for multiple targets (default: all)")
generate_parser.add_argument("--build")
generate_parser.add_argument("--released-at", required=True)
generate_parser.add_argument("--highlights-file", required=True, type=Path)
generate_parser.add_argument("--input", type=Path)
generate_parser.add_argument("--output", required=True, type=Path)
generate_parser.set_defaults(handler=generate)
validate_parser = subcommands.add_parser("validate")
validate_parser.add_argument("manifest", type=Path)
validate_parser.add_argument("--repository", required=True)
validate_parser.add_argument("--channel", choices=("stable", "snapshot"))
validate_parser.add_argument("--version")
validate_parser.add_argument("--build")
validate_parser.set_defaults(handler=validate)
args = parser.parse_args()
try:
args.handler(args)
except (OSError, ValueError, json.JSONDecodeError) as error:
parser.error(str(error))
if __name__ == "__main__":
main()