#!/usr/bin/env bash # Once per package, ever: tell npm that .gitlab-ci.yml in gitlab.com/bitbaum/kits # may publish it. One npm sign-in with the security key covers the whole run # (tick "do not challenge for 5 minutes" when npm asks). Run it right before # the package's first release this way: npm drops a trust that has not # published within 2 days. # # scripts/trust-gitlab.sh [folder ...] # # `--all` does every package in packages/. npm keeps ONE trust per package: # a package that already trusts this GitLab job is left alone, and one that # trusts anything else (its old GitHub workflow) has that entry revoked first, # because adding a second fails and the GitLab publish then ends in ENEEDAUTH # (what happened to bip-kit and chartkit on 2026-11-21). set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" PROJECT="${GITLAB_PROJECT:-bitbaum/kits}" # `npm trust` needs npm >= 11.15, and returned 400 on 11.x for us (2026-10): use 12. NPM=(npx -y npm@12) folders=("$@") [ ${#folders[@]} -gt 0 ] || { echo "name the package folder(s), or --all" >&2; exit 2; } [ "${folders[0]}" = --all ] && folders=($(ls packages)) ok=0; failed=() for f in "${folders[@]}"; do name=$(node -p "require('./packages/$f/package.json').name") # A failed `--json` call still prints JSON ({"error": ...}) and exits 1, so # never append a fallback to it: `|| echo '[]'` made two documents and the # parse below crashed instead of saying npm wanted a sign-in (EOTP). entries=$("${NPM[@]}" trust list "$name" --json || true) plan=$(printf '%s' "$entries" | PROJECT="$PROJECT" node -e ' let d = JSON.parse(require("fs").readFileSync(0, "utf8").trim() || "[]"); if (d && d.error) { if (d.error.code !== "E404") { console.log("ERROR", d.error.code, d.error.authUrl || d.error.summary || ""); process.exit(); } d = []; } if (!Array.isArray(d)) d = Object.values(d).find(Array.isArray) || [d]; for (const e of d) { const s = JSON.stringify(e).toLowerCase(); if (s.includes("gitlab") && s.includes(process.env.PROJECT.toLowerCase())) console.log("KEEP", e.id ?? "?"); else if (e.id) console.log("REVOKE", e.id); }') if grep -q '^ERROR' <<<"$plan"; then echo "FAILED $name: npm trust list said $(sed -n 's/^ERROR //p' <<<"$plan")"; failed+=("$name"); continue fi if grep -q '^KEEP' <<<"$plan"; then echo "already $name"; ok=$((ok + 1)); continue; fi while read -r action id; do [ "$action" = REVOKE ] || continue "${NPM[@]}" trust revoke --id "$id" "$name" && echo "revoked $name (old trust $id)" sleep 2 done <<<"$plan" if "${NPM[@]}" trust gitlab "$name" --file .gitlab-ci.yml --project "$PROJECT" --allow-publish --yes; then echo "trusted $name"; ok=$((ok + 1)) else echo "FAILED $name"; failed+=("$name") fi sleep 2 done echo "trusted $ok, failed ${#failed[@]}${failed:+: ${failed[*]}}" [ ${#failed[@]} -eq 0 ]