diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9c2fe79c..67f7c4a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,22 +98,50 @@ jobs: with: name: pd-vm-cli-${{ runner.os }}-${{ runner.arch }} path: rustscript/dist/* - - name: Check release draft state - id: pd_vm_release_state + - name: Ensure draft release exists + id: pd_vm_release if: startsWith(github.ref, 'refs/tags/') + shell: bash env: GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + TAG: ${{ github.ref_name }} run: | - if gh release view "${{ github.ref_name }}" --json isDraft --jq '.isDraft' >/tmp/release_is_draft; then - echo "is_draft=$(cat /tmp/release_is_draft)" >> "$GITHUB_OUTPUT" + set -euo pipefail + # Published releases are immutable, so assets may only be attached + # while the release is still a draft. Never publish it from CI, and + # tolerate other matrix jobs creating the same draft concurrently. + if is_draft=$(gh release view "$TAG" --json isDraft --jq '.isDraft' 2>/dev/null); then + echo "release $TAG already exists (isDraft=$is_draft)" + elif gh release create "$TAG" --draft --generate-notes --title "$TAG"; then + is_draft=true + echo "created draft release $TAG" + elif is_draft=$(gh release view "$TAG" --json isDraft --jq '.isDraft' 2>/dev/null); then + echo "release $TAG was created concurrently (isDraft=$is_draft)" else - echo "is_draft=true" >> "$GITHUB_OUTPUT" + echo "could not create or find release $TAG" >&2 + exit 1 + fi + if [[ "$is_draft" != "true" ]]; then + echo "::warning::release $TAG is already published and immutable; skipping pd-vm CLI asset upload" fi + echo "is_draft=$is_draft" >> "$GITHUB_OUTPUT" - name: Upload pd-vm CLI release asset - if: startsWith(github.ref, 'refs/tags/') && steps.pd_vm_release_state.outputs.is_draft == 'true' - uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 - with: - files: rustscript/dist/* + if: startsWith(github.ref, 'refs/tags/') && steps.pd_vm_release.outputs.is_draft == 'true' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + TAG: ${{ github.ref_name }} + run: | + set -euo pipefail + shopt -s nullglob + assets=(rustscript/dist/*) + if (( ${#assets[@]} == 0 )); then + echo "no pd-vm CLI assets staged in rustscript/dist" >&2 + exit 1 + fi + gh release upload "$TAG" "${assets[@]}" --clobber vscode-extension: name: VS Code extension @@ -146,19 +174,46 @@ jobs: with: name: rustscript-vscode-extension path: vscode-rustscript/dist/*.vsix - - name: Check VS Code release draft state - id: vscode_release_state + - name: Ensure draft release exists + id: vscode_release if: startsWith(github.ref, 'refs/tags/') + shell: bash env: GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + TAG: ${{ github.ref_name }} run: | - if gh release view "${{ github.ref_name }}" --json isDraft --jq '.isDraft' >/tmp/release_is_draft; then - echo "is_draft=$(cat /tmp/release_is_draft)" >> "$GITHUB_OUTPUT" + set -euo pipefail + # Published releases are immutable, so assets may only be attached + # while the release is still a draft. Never publish it from CI. + if is_draft=$(gh release view "$TAG" --json isDraft --jq '.isDraft' 2>/dev/null); then + echo "release $TAG already exists (isDraft=$is_draft)" + elif gh release create "$TAG" --draft --generate-notes --title "$TAG"; then + is_draft=true + echo "created draft release $TAG" + elif is_draft=$(gh release view "$TAG" --json isDraft --jq '.isDraft' 2>/dev/null); then + echo "release $TAG was created concurrently (isDraft=$is_draft)" else - echo "is_draft=true" >> "$GITHUB_OUTPUT" + echo "could not create or find release $TAG" >&2 + exit 1 + fi + if [[ "$is_draft" != "true" ]]; then + echo "::warning::release $TAG is already published and immutable; skipping VS Code extension asset upload" fi + echo "is_draft=$is_draft" >> "$GITHUB_OUTPUT" - name: Upload VS Code extension release asset - if: startsWith(github.ref, 'refs/tags/') && steps.vscode_release_state.outputs.is_draft == 'true' - uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 - with: - files: vscode-rustscript/dist/*.vsix + if: startsWith(github.ref, 'refs/tags/') && steps.vscode_release.outputs.is_draft == 'true' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + TAG: ${{ github.ref_name }} + run: | + set -euo pipefail + shopt -s nullglob + assets=(vscode-rustscript/dist/*.vsix) + if (( ${#assets[@]} == 0 )); then + echo "no VS Code extension assets staged in vscode-rustscript/dist" >&2 + exit 1 + fi + gh release upload "$TAG" "${assets[@]}" --clobber diff --git a/scripts/test_verify_publish_order.py b/scripts/test_verify_publish_order.py new file mode 100644 index 00000000..dcb77a06 --- /dev/null +++ b/scripts/test_verify_publish_order.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +import unittest +from pathlib import Path + +from scripts import verify_publish_order + + +def workspace(*packages: tuple[str, dict[str, tuple[str, ...]]]) -> dict[str, dict]: + """Build cargo-metadata-like packages: (name, {"normal"/"build"/"dev": deps}).""" + result = {} + for name, dependencies in packages: + entries = [] + for kind, dependency_names in dependencies.items(): + for dependency_name in dependency_names: + # cargo metadata reports normal dependencies with a null kind. + entries.append( + { + "name": dependency_name, + "kind": None if kind == "normal" else kind, + } + ) + result[name] = {"name": name, "dependencies": entries} + return result + + +ORDER = ["pd-host-function", "pd-vm", "pd-vm-nostd", "rustscript"] + + +class VerifyPublishOrderTests(unittest.TestCase): + def test_dev_dependency_does_not_constrain_publish_order(self) -> None: + packages = workspace( + ("pd-host-function", {"normal": ("pd-host-schema",), "dev": ("pd-vm",)}), + ("pd-vm", {}), + ("pd-vm-nostd", {"normal": ("pd-vm",)}), + ("rustscript", {"normal": ("pd-vm",)}), + ) + + self.assertEqual( + verify_publish_order.publish_order_errors(packages, ORDER), + [], + ) + + def test_normal_dependency_must_be_published_first(self) -> None: + packages = workspace( + ("pd-host-function", {"normal": ("pd-vm",)}), + ("pd-vm", {}), + ) + + errors = verify_publish_order.publish_order_errors( + packages, + ["pd-host-function", "pd-vm"], + ) + + self.assertEqual(len(errors), 1) + self.assertIn("pd-host-function has a normal dependency on pd-vm", errors[0]) + + def test_build_dependency_must_be_published_first(self) -> None: + packages = workspace( + ("pd-host-function", {"build": ("pd-vm",)}), + ("pd-vm", {}), + ) + + errors = verify_publish_order.publish_order_errors( + packages, + ["pd-host-function", "pd-vm"], + ) + + self.assertEqual(len(errors), 1) + self.assertIn("pd-host-function has a build dependency on pd-vm", errors[0]) + + def test_duplicate_package_names_are_rejected(self) -> None: + packages = workspace(("pd-vm", {})) + + self.assertEqual( + verify_publish_order.publish_order_errors(packages, ["pd-vm", "pd-vm"]), + ["publish order contains duplicate package name: pd-vm"], + ) + + def test_unknown_package_names_are_rejected(self) -> None: + packages = workspace(("pd-vm", {})) + + self.assertEqual( + verify_publish_order.publish_order_errors(packages, ["pd-vm", "missing"]), + ["publish order contains unknown workspace packages: ['missing']"], + ) + + +class ReleaseUploadWorkflowTests(unittest.TestCase): + def test_release_assets_are_uploaded_to_a_draft_without_finalizing(self) -> None: + workflow = ( + Path(__file__).parents[1] / ".github" / "workflows" / "ci.yml" + ).read_text() + + # softprops/action-gh-release publishes the release it touches, which + # fails once the release is immutable. + self.assertNotIn("softprops/action-gh-release", workflow) + self.assertEqual(workflow.count("gh release upload"), 2) + self.assertEqual(workflow.count("--clobber"), 2) + self.assertEqual(workflow.count("gh release create"), 2) + self.assertNotIn("gh release edit", workflow) + self.assertNotIn("--draft=false", workflow) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/verify_publish_order.py b/scripts/verify_publish_order.py index 3078ff1f..36a0e5b8 100644 --- a/scripts/verify_publish_order.py +++ b/scripts/verify_publish_order.py @@ -3,6 +3,12 @@ import json import subprocess +# Only these dependency kinds constrain the crate publish order. Dev +# dependencies are not part of the published package, so a crate may depend on +# a crate that appears only in its dev-dependencies even when it is published +# first (pd-host-function dev-depends on pd-vm). +ORDERING_DEPENDENCY_KINDS = frozenset({"normal", "build"}) + def workspace_packages() -> dict[str, dict]: output = subprocess.check_output( @@ -18,34 +24,46 @@ def workspace_packages() -> dict[str, dict]: } -def main() -> None: - parser = argparse.ArgumentParser() - parser.add_argument("packages", nargs="+") - args = parser.parse_args() +def dependency_kind(dependency: dict) -> str: + # cargo metadata reports normal dependencies with a null kind. + return dependency.get("kind") or "normal" + - positions = {name: index for index, name in enumerate(args.packages)} - if len(positions) != len(args.packages): - raise SystemExit("publish order contains duplicate package names") +def publish_order_errors(packages: dict[str, dict], order: list[str]) -> list[str]: + positions: dict[str, int] = {} + for index, name in enumerate(order): + if name in positions: + return [f"publish order contains duplicate package name: {name}"] + positions[name] = index - packages = workspace_packages() - unknown = [name for name in args.packages if name not in packages] + unknown = [name for name in order if name not in packages] if unknown: - raise SystemExit(f"publish order contains unknown workspace packages: {unknown}") + return [f"publish order contains unknown workspace packages: {unknown}"] errors = [] - for package_name in args.packages: + for package_name in order: package = packages[package_name] - for dependency in package["dependencies"]: + for dependency in package.get("dependencies", []): + kind = dependency_kind(dependency) + if kind not in ORDERING_DEPENDENCY_KINDS: + continue dependency_name = dependency["name"] if dependency_name not in positions: continue if positions[dependency_name] > positions[package_name]: - kind = dependency.get("kind") or "normal" errors.append( f"{package_name} has a {kind} dependency on {dependency_name}, " f"so {dependency_name} must be published first" ) + return errors + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("packages", nargs="+") + args = parser.parse_args() + errors = publish_order_errors(workspace_packages(), args.packages) if errors: raise SystemExit("\n".join(errors)) print("crate publish order is valid")