| 1 | #!/usr/bin/env bash |
| 2 | # Refresh or check the first-party catalog snapshot embedded in the Engine |
| 3 | # (crates/plugins/assets/first-party-marketplace.json) against the head of the |
| 4 | # public marketplace repository. |
| 5 | # |
| 6 | # scripts/refresh-first-party-marketplace.sh rewrite the snapshot from the marketplace head |
| 7 | # scripts/refresh-first-party-marketplace.sh --check fail when the snapshot is older than the head |
| 8 | # --marketplace DIR use this checkout instead of cloning the head |
| 9 | # |
| 10 | # Nothing is installed, trusted or executed. "Older" means the files the |
| 11 | # catalog installs from (marketplace.json, plugins/, skills/) differ between the |
| 12 | # snapshot's revision and the head; unrelated commits do not make it stale. |
| 13 | set -euo pipefail |
| 14 | |
| 15 | root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 16 | snapshot="${root}/crates/plugins/assets/first-party-marketplace.json" |
| 17 | repo_url="https://github.com/codewhale-hq/codewhale-plugin-marketplace" |
| 18 | mode=refresh |
| 19 | checkout="" |
| 20 | |
| 21 | while [[ $# -gt 0 ]]; do |
| 22 | case "$1" in |
| 23 | --check) mode=check; shift ;; |
| 24 | --marketplace) |
| 25 | [[ $# -ge 2 ]] || { echo "--marketplace needs a directory" >&2; exit 2; } |
| 26 | checkout="$2"; shift 2 ;; |
| 27 | -h|--help) sed -n '2,12p' "${BASH_SOURCE[0]}"; exit 0 ;; |
| 28 | *) echo "unknown argument: $1" >&2; exit 2 ;; |
| 29 | esac |
| 30 | done |
| 31 | |
| 32 | if [[ -z "${checkout}" ]]; then |
| 33 | tmp="$(mktemp -d)" |
| 34 | trap 'rm -rf "${tmp}"' EXIT |
| 35 | git clone --quiet --filter=blob:none --no-checkout "${repo_url}" "${tmp}/marketplace" |
| 36 | checkout="${tmp}/marketplace" |
| 37 | fi |
| 38 | |
| 39 | head_rev="$(git -C "${checkout}" rev-parse HEAD)" |
| 40 | |
| 41 | if [[ "${mode}" == refresh ]]; then |
| 42 | python3 "${root}/scripts/sync-marketplace.py" --marketplace "${checkout}" |
| 43 | exit 0 |
| 44 | fi |
| 45 | |
| 46 | snapshot_rev="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["revision"])' "${snapshot}")" |
| 47 | if [[ "${snapshot_rev}" == "${head_rev}" ]]; then |
| 48 | python3 "${root}/scripts/sync-marketplace.py" --marketplace "${checkout}" --check |
| 49 | echo "First-party catalog snapshot is at the marketplace head ${head_rev}." |
| 50 | exit 0 |
| 51 | fi |
| 52 | if ! git -C "${checkout}" cat-file -e "${snapshot_rev}^{commit}" 2>/dev/null; then |
| 53 | echo "::error::Snapshot revision ${snapshot_rev} is not in the marketplace history; run scripts/refresh-first-party-marketplace.sh." >&2 |
| 54 | exit 1 |
| 55 | fi |
| 56 | tree_ids() { |
| 57 | local rev="$1" path |
| 58 | for path in marketplace.json plugins skills; do |
| 59 | git -C "${checkout}" rev-parse "${rev}:${path}" 2>/dev/null || echo "missing:${path}" |
| 60 | done |
| 61 | } |
| 62 | if [[ "$(tree_ids "${snapshot_rev}")" == "$(tree_ids "${head_rev}")" ]]; then |
| 63 | echo "First-party catalog snapshot (${snapshot_rev}) is current: nothing it installs from changed up to ${head_rev}." |
| 64 | exit 0 |
| 65 | fi |
| 66 | echo "::error::The first-party catalog snapshot (${snapshot_rev}) is older than the marketplace head (${head_rev}). Run scripts/refresh-first-party-marketplace.sh, review the diff and commit it." >&2 |
| 67 | git -C "${checkout}" diff --stat "${snapshot_rev}" "${head_rev}" -- marketplace.json plugins skills >&2 || true |
| 68 | exit 1 |
| 69 |