Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

5.1 KB
Raw Blame
#!/usr/bin/env bash
# Open or update THE pins PR (branch images/pins) for one cua-spacesd version:
#
#   scripts/images/pins-pr.sh VERSION [--results os=result,...] [--run-url URL] [--dry-run]
#
# 1. image_pins.py apply: every OS whose floating tags report cua-spacesd
#    VERSION (doctor reports attached to the pushed digests) and differ from
#    the catalog gets its new digests and sizes in libs/images/sandbox-images.json
#    and its README pin rows moved.
# 2. Everything generated from the catalog follows it, so the PR passes its
#    checks by itself: the app-core parity goldens (UPDATE_PARITY=1) and every
#    docs generator the docs check routes the change to
#    (scripts/docs-generators/runner.ts --changed-files-file, e.g. the sandbox
#    reference pages). The generators rewrite their whole output, so drift
#    main already had in those pages is fixed here too.
# 3. The branch is rebuilt from the checked-out main and force-pushed, and the
#    PR's title and body (a per-OS checklist) are set. The PR is a pure function
#    of main and the registry, so every run (CI, or after a local macOS
#    release) recomputes it from scratch; nothing pushed to the branch by hand
#    survives. With nothing to move, an open pins PR is closed.
#
# Run by .github/workflows/cd-images-spacesd-release.yml (pins job), which sets
# the git identity and GH_TOKEN (the release GitHub App, so the PR's checks run).
# --dry-run leaves the changes in the working tree and prints the body.
# Needs crane, oras, jq, cargo (with protoc), node (npx when docs/node_modules
# is not installed) and gh.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
BRANCH=images/pins
VERSION="" RESULTS="" RUN_URL="" DRY=0
while [ $# -gt 0 ]; do
    case "$1" in
        --results) RESULTS="$2"; shift 2 ;;
        --run-url) RUN_URL="$2"; shift 2 ;;
        --dry-run) DRY=1; shift ;;
        -h|--help) sed -n '2,28p' "$0"; exit 0 ;;
        -*) echo "unknown option $1" >&2; exit 2 ;;
        *) VERSION="$1"; shift ;;
    esac
done
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "usage: pins-pr.sh X.Y.Z [--results ...] [--run-url URL] [--dry-run]" >&2; exit 2; }
cd "$ROOT"
TITLE="chore(images): pin Space images on cua-spacesd $VERSION"
PINS=(libs/images/sandbox-images.json libs/images/README.md)
FILES=("${PINS[@]}" libs/cua/crates/cua-spaces-app-core/parity/golden docs/content)
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT

# A green build that pushed nothing fails this script, but only after the PR
# is up to date with everything else.
rc=0
python3 scripts/images/image_pins.py apply --version "$VERSION" --body "$work/body.md" \
    --results "$RESULTS" --run-url "$RUN_URL" >"$work/summary.json" || rc=$?
[ -s "$work/summary.json" ] || exit "${rc:-1}"
cat "$work/summary.json"
# Not inside an `if`: a summary that is not the JSON line fails the script
# instead of silently skipping the regeneration below.
moved="$(jq -e '.moved | length' "$work/summary.json")"

# The docs app's lockfile-pinned tsx, or that same version through npx where
# docs/node_modules is not installed (the CI pins job).
tsx() {
    if [ -f docs/node_modules/tsx/dist/cli.mjs ]; then
        node docs/node_modules/tsx/dist/cli.mjs "$@"
    else
        local v
        v="$(grep -m1 -oE '^  tsx@[0-9]+\.[0-9]+\.[0-9]+' docs/pnpm-lock.yaml | cut -d@ -f2)"
        [ -n "$v" ] || { echo "pins-pr.sh: no tsx version in docs/pnpm-lock.yaml" >&2; return 1; }
        npx --yes "tsx@$v" "$@"
    fi
}

regenerate() {
    (cd libs/cua && UPDATE_PARITY=1 cargo test --locked -p cua-spaces-app-core --test parity)
    git diff --name-only -- "${PINS[@]}" >"$work/changed"
    local gens g script
    gens="$(tsx scripts/docs-generators/runner.ts --changed-files-file "$work/changed" | tail -n1)"
    for g in $gens; do
        script="$(jq -er --arg g "$g" '.generators[$g].generatorScript' scripts/docs-generators/config.json)"
        echo "docs generator $g ($script)"
        tsx "$script"
    done
}

open_pr="$(gh pr list --head "$BRANCH" --base main --state open --json number -q '.[0].number // empty' 2>/dev/null || true)"
if [ "$moved" -gt 0 ] || ! git diff --quiet -- "${PINS[@]}"; then
    regenerate
fi
if git diff --quiet -- "${PINS[@]}"; then
    echo "nothing to pin for cua-spacesd $VERSION"
    if [ -n "$open_pr" ] && [ "$DRY" = 0 ]; then
        gh pr close "$open_pr" --delete-branch \
            --comment "Nothing on cua-spacesd $VERSION is left to pin (${RUN_URL:-a later run}); closing."
    fi
    exit "$rc"
fi
if [ "$DRY" = 1 ]; then
    git diff --stat -- "${FILES[@]}"
    echo "--- $TITLE"; cat "$work/body.md"
    exit "$rc"
fi
git checkout -B "$BRANCH"
git add -A -- "${FILES[@]}"
git commit -q -m "$TITLE" -m "Generated by scripts/images/pins-pr.sh${RUN_URL:+ in $RUN_URL}."
git push --force origin "$BRANCH"
if [ -n "$open_pr" ]; then
    gh pr edit "$open_pr" --title "$TITLE" --body-file "$work/body.md"
    url="$(gh pr view "$open_pr" --json url -q .url)"
else
    url="$(gh pr create --base main --head "$BRANCH" --title "$TITLE" --body-file "$work/body.md")"
fi
echo "pins PR: $url"
[ -z "${GITHUB_STEP_SUMMARY:-}" ] || {
    echo "### Pins PR: $url"
    cat "$work/body.md"
} >>"$GITHUB_STEP_SUMMARY"
exit "$rc"