Add trusted PyPI publishing for approved releases (#6642)
Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
1 parent
4e36dcc7c6
commit
96c48c96bf
9 files changed
+646
-16
No files matched your search
@@ -0,0 +1,151 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
WORKFLOWS = Path(__file__).resolve().parents[2] / ".github/workflows"
|
||||
COMMIT = "a" * 40
|
||||
|
||||
|
||||
def workflow(name):
|
||||
return yaml.load((WORKFLOWS / name).read_text(encoding="utf-8"), Loader=yaml.BaseLoader)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def shell_env(tmp_path):
|
||||
if os.name == "nt":
|
||||
pytest.skip("Release workflows run their Bash steps on Ubuntu")
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
gh = bin_dir / "gh"
|
||||
gh.write_text(
|
||||
"#!/usr/bin/env python3\n"
|
||||
"import json, os, sys\n"
|
||||
"args = sys.argv[1:]\n"
|
||||
"if args[:2] == ['release', 'view']:\n"
|
||||
" print(json.dumps({'tagName': 'v1.2.3', 'isDraft': False, 'isPrerelease': False}))\n"
|
||||
"elif args[:2] == ['release', 'download']:\n"
|
||||
" pass\n"
|
||||
"elif args[0] == 'api':\n"
|
||||
" print(open(os.environ['MOCK_APPROVALS']).read())\n"
|
||||
"else:\n"
|
||||
" sys.exit(2)\n"
|
||||
)
|
||||
gh.chmod(0o755)
|
||||
git = bin_dir / "git"
|
||||
git.write_text(f"#!/bin/bash\n[[ $1 != rev-parse ]] || echo {COMMIT}\n")
|
||||
git.chmod(0o755)
|
||||
python = bin_dir / "python"
|
||||
python.write_text("#!/bin/bash\nexit 0\n")
|
||||
python.chmod(0o755)
|
||||
return {
|
||||
"PATH": f"{bin_dir}:{os.environ.get('PATH', '/usr/bin:/bin')}", "HOME": str(tmp_path), "GH_TOKEN": "test",
|
||||
"GITHUB_RUN_ID": "987", "GITHUB_RUN_ATTEMPT": "1", "RUNNER_TEMP": str(tmp_path),
|
||||
"GITHUB_OUTPUT": str(tmp_path / "outputs"), "GITHUB_STEP_SUMMARY": str(tmp_path / "summary"),
|
||||
"MOCK_APPROVALS": str(tmp_path / "approvals.json"),
|
||||
"GITHUB_EVENT_NAME": "workflow_run", "UPSTREAM_TAG": "v1.2.3", "UPSTREAM_SHA": COMMIT,
|
||||
"UPSTREAM_ACTOR": "publisher", "VERSION_INPUT": "", "DESTINATION_INPUT": "",
|
||||
"GITHUB_ACTOR": "github-actions[bot]",
|
||||
}
|
||||
|
||||
|
||||
def run(step, env):
|
||||
return subprocess.run(["bash", "-c", step["run"]], env=env, capture_output=True, text=True)
|
||||
|
||||
|
||||
def release_step():
|
||||
return next(step for step in workflow("publish-pypi.yml")["jobs"]["verify"]["steps"]
|
||||
if step.get("id") == "release")
|
||||
|
||||
|
||||
def publish_gate():
|
||||
return workflow("publish-pypi.yml")["jobs"]["publish"]["steps"][0]
|
||||
|
||||
|
||||
def test_only_stable_release_completion_can_start_the_main_publisher(shell_env):
|
||||
trigger = workflow("trigger-pypi.yml")
|
||||
assert trigger["on"]["release"]["types"] == ["released"]
|
||||
job = trigger["jobs"]["release"]
|
||||
assert "!github.event.release.prerelease" in job["if"]
|
||||
assert trigger["permissions"] == {}
|
||||
result = run(job["steps"][0], {**shell_env, "RELEASE_TAG": "v1.2.3"})
|
||||
assert result.returncode == 0, result.stderr
|
||||
publisher = workflow("publish-pypi.yml")
|
||||
assert publisher["on"]["workflow_run"]["workflows"] == ["Validate published release for PyPI"]
|
||||
assert publisher["on"]["workflow_run"]["types"] == ["completed"]
|
||||
assert "github.event.workflow_run.head_branch" in publisher["concurrency"]["group"]
|
||||
assert "format('v{0}', inputs.version)" in publisher["concurrency"]["group"]
|
||||
guard = publisher["jobs"]["verify"]["if"]
|
||||
assert all(value in guard for value in ("refs/heads/main", "conclusion == 'success'", "event == 'release'",
|
||||
".github/workflows/trigger-pypi.yml"))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tag", ["v1.2.3-rc1", "v1.2.3;echo BAD", "1.2.3"])
|
||||
def test_invalid_release_tag_fails_before_publisher(shell_env, tag):
|
||||
step = workflow("trigger-pypi.yml")["jobs"]["release"]["steps"][0]
|
||||
assert run(step, {**shell_env, "RELEASE_TAG": tag}).returncode != 0
|
||||
|
||||
|
||||
def test_release_origin_preserves_tag_and_original_actor(shell_env):
|
||||
result = run(release_step(), shell_env)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert set(Path(shell_env["GITHUB_OUTPUT"]).read_text().splitlines()) == {
|
||||
"release_actor=publisher", "release_version=1.2.3", "destination=pypi"
|
||||
}
|
||||
|
||||
|
||||
def test_simulated_release_reaches_independently_approved_publisher(shell_env):
|
||||
trigger = workflow("trigger-pypi.yml")["jobs"]["release"]["steps"][0]
|
||||
assert run(trigger, {**shell_env, "RELEASE_TAG": "v1.2.3"}).returncode == 0
|
||||
assert run(release_step(), shell_env).returncode == 0
|
||||
outputs = dict(line.split("=", 1) for line in Path(shell_env["GITHUB_OUTPUT"]).read_text().splitlines())
|
||||
assert outputs["release_version"] == "1.2.3" and outputs["destination"] == "pypi"
|
||||
Path(shell_env["MOCK_APPROVALS"]).write_text(json.dumps([
|
||||
{"state": "approved", "user": {"login": "reviewer"}, "environments": [{"name": "pypi"}]}
|
||||
]))
|
||||
assert run(publish_gate(), {**shell_env, "DESTINATION": "pypi",
|
||||
"RELEASE_ACTOR": outputs["release_actor"]}).returncode == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", [{"UPSTREAM_SHA": "b" * 40}, {"UPSTREAM_TAG": "v1.2.3-rc1"},
|
||||
{"UPSTREAM_ACTOR": "bad\nactor"}, {"UPSTREAM_ACTOR": "publisher[bot]"}])
|
||||
def test_release_origin_rejects_changed_tag_or_invalid_actor(shell_env, change):
|
||||
assert run(release_step(), {**shell_env, **change}).returncode != 0
|
||||
|
||||
|
||||
def test_manual_testpypi_run_uses_its_real_actor_not_upstream_input(shell_env):
|
||||
result = run(release_step(), {**shell_env, "GITHUB_EVENT_NAME": "workflow_dispatch", "GITHUB_ACTOR": "maintainer",
|
||||
"VERSION_INPUT": "1.2.3", "DESTINATION_INPUT": "testpypi"})
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert set(Path(shell_env["GITHUB_OUTPUT"]).read_text().splitlines()) == {
|
||||
"release_actor=maintainer", "release_version=1.2.3", "destination=testpypi"
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("reviewer", "environment", "state", "allowed"), [
|
||||
("reviewer", "pypi", "approved", True),
|
||||
("publisher", "pypi", "approved", False),
|
||||
("PUBLISHER", "pypi", "approved", False),
|
||||
("reviewer", "testpypi", "approved", False),
|
||||
("reviewer", "pypi", "rejected", False),
|
||||
])
|
||||
def test_production_approval_must_be_independent(shell_env, reviewer, environment, state, allowed):
|
||||
payload = [{"state": state, "user": {"login": reviewer}, "environments": [{"name": environment}]}]
|
||||
Path(shell_env["MOCK_APPROVALS"]).write_text(json.dumps(payload))
|
||||
result = run(publish_gate(), {**shell_env, "DESTINATION": "pypi", "RELEASE_ACTOR": "publisher"})
|
||||
assert (result.returncode == 0) == allowed
|
||||
|
||||
|
||||
@pytest.mark.parametrize("destination", ["pypi", "testpypi"])
|
||||
def test_rerun_cannot_use_prior_approval_or_staged_files(shell_env, destination):
|
||||
if destination == "pypi":
|
||||
Path(shell_env["MOCK_APPROVALS"]).write_text(json.dumps([
|
||||
{"state": "approved", "user": {"login": "reviewer"}, "environments": [{"name": "pypi"}]}
|
||||
]))
|
||||
env = {**shell_env, "DESTINATION": destination, "RELEASE_ACTOR": "publisher"}
|
||||
assert run(publish_gate(), env).returncode == 0
|
||||
result = run(publish_gate(), {**env, "GITHUB_RUN_ATTEMPT": "2"})
|
||||
assert result.returncode != 0 and "start a new workflow run" in result.stderr
|
||||
@@ -0,0 +1,55 @@
|
||||
import hashlib
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.releases import ReleaseArtifact, ReleaseManifest, release_filenames
|
||||
from scripts import prepare_pypi_release
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def approved_release(tmp_path, monkeypatch):
|
||||
source = tmp_path / "release"
|
||||
source.mkdir()
|
||||
artifacts = []
|
||||
for filename in sorted(release_filenames("1.2.3")):
|
||||
data = filename.encode()
|
||||
(source / filename).write_bytes(data)
|
||||
artifacts.append(ReleaseArtifact(filename=filename, size=len(data), sha256=hashlib.sha256(data).hexdigest()))
|
||||
manifest = ReleaseManifest(version="1.2.3", source_commit="a" * 40, candidate_commit="b" * 40,
|
||||
requires_python=">=3.11", artifacts=artifacts)
|
||||
(source / "release-manifest.json").write_text(manifest.model_dump_json())
|
||||
(source / "release-manifest.json.sigstore.json").write_bytes(b"proof")
|
||||
monkeypatch.setattr(prepare_pypi_release, "verify_release_approval", lambda payload, proof: None)
|
||||
return source, tmp_path / "output"
|
||||
|
||||
|
||||
def test_stages_only_matching_pypi_distributions(approved_release):
|
||||
source, output = approved_release
|
||||
prepare_pypi_release.stage_release(source, output, version="1.2.3", tag_commit="b" * 40)
|
||||
assert {path.name for path in output.iterdir()} == {
|
||||
name for name in release_filenames("1.2.3") if name.endswith((".whl", ".tar.gz"))
|
||||
}
|
||||
assert all(path.read_bytes() == (source / path.name).read_bytes() for path in output.iterdir())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("failure", ["changed", "missing_archive", "wrong_tag", "invalid_signature", "preview"])
|
||||
def test_refuses_unapproved_or_incomplete_release(approved_release, monkeypatch, failure):
|
||||
source, output = approved_release
|
||||
version, commit = "1.2.3", "b" * 40
|
||||
archive = next(source.glob("nanobot-tui-*.zip"))
|
||||
if failure == "changed":
|
||||
data = archive.read_bytes()
|
||||
archive.write_bytes(b"x" * len(data))
|
||||
elif failure == "missing_archive":
|
||||
archive.unlink()
|
||||
elif failure == "wrong_tag":
|
||||
commit = "c" * 40
|
||||
elif failure == "invalid_signature":
|
||||
def reject(payload, proof):
|
||||
raise ValueError("invalid signature")
|
||||
monkeypatch.setattr(prepare_pypi_release, "verify_release_approval", reject)
|
||||
else:
|
||||
version = "1.2.3.dev1"
|
||||
with pytest.raises(ValueError):
|
||||
prepare_pypi_release.stage_release(source, output, version=version, tag_commit=commit)
|
||||
assert not output.exists()
|
||||
@@ -0,0 +1,87 @@
|
||||
import hashlib
|
||||
import json
|
||||
from urllib.error import HTTPError
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.releases import PLATFORMS
|
||||
from scripts import reconcile_pypi_release as reconcile
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def staged(tmp_path):
|
||||
names = {f"nanobot_ai-1.2.3-py3-none-{platform}.whl" for platform in PLATFORMS.values()}
|
||||
names.add("nanobot_ai-1.2.3.tar.gz")
|
||||
for name in names:
|
||||
(tmp_path / name).write_bytes(name.encode())
|
||||
return tmp_path
|
||||
|
||||
|
||||
def index_entry(path):
|
||||
content = path.read_bytes()
|
||||
return {"filename": path.name, "size": len(content), "yanked": False,
|
||||
"digests": {"sha256": hashlib.sha256(content).hexdigest()}}
|
||||
|
||||
|
||||
def serve(monkeypatch, entries):
|
||||
payload = json.dumps({"info": {"name": "nanobot-ai", "version": "1.2.3"}, "urls": entries}).encode()
|
||||
class Response:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
def read(self, size):
|
||||
return payload[:size]
|
||||
|
||||
monkeypatch.setattr(reconcile, "urlopen", lambda url, timeout: Response())
|
||||
|
||||
|
||||
def test_new_release_uploads_all_files(staged, monkeypatch):
|
||||
def missing(url, timeout):
|
||||
raise HTTPError(url, 404, "Not Found", {}, None)
|
||||
monkeypatch.setattr(reconcile, "urlopen", missing)
|
||||
assert reconcile.remove_uploaded_files(staged, version="1.2.3", destination="pypi") == 6
|
||||
assert len(list(staged.iterdir())) == 6
|
||||
|
||||
|
||||
def test_index_failure_never_becomes_a_new_release(staged, monkeypatch):
|
||||
def unavailable(url, timeout):
|
||||
raise HTTPError(url, 503, "Unavailable", {}, None)
|
||||
monkeypatch.setattr(reconcile, "urlopen", unavailable)
|
||||
with pytest.raises(HTTPError):
|
||||
reconcile.remove_uploaded_files(staged, version="1.2.3", destination="pypi")
|
||||
assert len(list(staged.iterdir())) == 6
|
||||
|
||||
|
||||
def test_retry_uploads_only_missing_files(staged, monkeypatch):
|
||||
already_uploaded = sorted(staged.iterdir())[:2]
|
||||
serve(monkeypatch, [index_entry(path) for path in already_uploaded])
|
||||
assert reconcile.remove_uploaded_files(staged, version="1.2.3", destination="testpypi") == 4
|
||||
assert all(not path.exists() for path in already_uploaded)
|
||||
assert len(list(staged.iterdir())) == 4
|
||||
|
||||
|
||||
def test_complete_release_requires_no_upload(staged, monkeypatch):
|
||||
serve(monkeypatch, [index_entry(path) for path in staged.iterdir()])
|
||||
assert reconcile.remove_uploaded_files(staged, version="1.2.3", destination="pypi") == 0
|
||||
assert not list(staged.iterdir())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["digest", "unexpected", "yanked", "duplicate"])
|
||||
def test_refuses_inconsistent_index_without_removing_files(staged, monkeypatch, change):
|
||||
paths = sorted(staged.iterdir())
|
||||
entries = [index_entry(path) for path in paths[:2]]
|
||||
if change == "digest":
|
||||
entries[1]["digests"]["sha256"] = "0" * 64
|
||||
elif change == "unexpected":
|
||||
entries[1]["filename"] = "other.whl"
|
||||
elif change == "yanked":
|
||||
entries[1]["yanked"] = True
|
||||
else:
|
||||
entries[1] = entries[0]
|
||||
serve(monkeypatch, entries)
|
||||
with pytest.raises(ValueError):
|
||||
reconcile.remove_uploaded_files(staged, version="1.2.3", destination="pypi")
|
||||
assert len(list(staged.iterdir())) == 6
|
||||
Reference in new issue
Block a user