#!/usr/bin/env bash set -euo pipefail config_dir="$HOME/.config/shuttle-test" env_file="$config_dir/.env" key_file="$config_dir/id_ed25519" knot_url="http://127.0.0.1:5555" knot_did="did:web:knot.example.org" if [[ ! -f "$env_file" ]]; then echo "missing $env_file" >&2 exit 1 fi set -a # shellcheck disable=SC1090 source "$env_file" set +a : "${SHUTTLE_TEST_HANDLE:?missing SHUTTLE_TEST_HANDLE}" : "${SHUTTLE_TEST_PASSWORD:?missing SHUTTLE_TEST_PASSWORD}" : "${SHUTTLE_TEST_DID:=did:plc:tdw3iqyznnma3t2ylthfpyd5}" : "${SHUTTLE_TEST_PDS:=https://auriporia.us-west.host.bsky.network}" mkdir -p "$config_dir" chmod 700 "$config_dir" if [[ ! -f "$key_file" ]]; then ssh-keygen -q -t ed25519 -N '' -C shuttle-dev -f "$key_file" fi chmod 600 "$key_file" # Tokens remain shell variables. Curl receives responses through pipes, never files. session=$(jq -nc --arg identifier "$SHUTTLE_TEST_HANDLE" --arg password "$SHUTTLE_TEST_PASSWORD" \ '{identifier:$identifier,password:$password}' | \ curl -fsS "$SHUTTLE_TEST_PDS/xrpc/com.atproto.server.createSession" \ -H 'Content-Type: application/json' --data-binary @-) access_jwt=$(jq -er '.accessJwt' <<<"$session") session= created_at=$(date -u +%Y-%m-%dT%H:%M:%SZ) public_key=$(<"$key_file.pub") put_record() { local collection=$1 rkey=$2 record=$3 jq -nc --arg repo "$SHUTTLE_TEST_DID" --arg collection "$collection" \ --arg rkey "$rkey" --argjson record "$record" \ '{repo:$repo,collection:$collection,rkey:$rkey,record:$record}' | \ curl -fsS "$SHUTTLE_TEST_PDS/xrpc/com.atproto.repo.putRecord" \ -H 'Content-Type: application/json' -H "Authorization: Bearer $access_jwt" \ --data-binary @- >/dev/null } key_record=$(jq -nc --arg key "$public_key" --arg createdAt "$created_at" \ '{"$type":"sh.tangled.publicKey",key:$key,name:"Shuttle development",createdAt:$createdAt}') put_record sh.tangled.publicKey shuttle-dev "$key_record" echo "public key record: sh.tangled.publicKey/shuttle-dev" if [[ -n "${SHUTTLE_TEST_REPO_DID:-}" ]]; then describe=$(curl -sS -H 'Host: knot.example.org' \ --get "$knot_url/xrpc/sh.tangled.repo.describeRepo" \ --data-urlencode "repoDid=$SHUTTLE_TEST_REPO_DID" -w $'\n%{http_code}') status=${describe##*$'\n'} body=${describe%$'\n'*} if [[ "$status" != 200 ]]; then echo "describeRepo failed ($status): $body" >&2 exit 1 fi echo "describeRepo response: $body" else service_auth_response=$(curl -sS --get \ "$SHUTTLE_TEST_PDS/xrpc/com.atproto.server.getServiceAuth" \ -H "Authorization: Bearer $access_jwt" --data-urlencode "aud=$knot_did" \ --data-urlencode 'lxm=sh.tangled.repo.create' -w $'\n%{http_code}') status=${service_auth_response##*$'\n'} service_auth=${service_auth_response%$'\n'*} service_auth_response= if [[ "$status" != 200 ]]; then echo "getServiceAuth failed ($status): $service_auth" >&2 exit 1 fi service_jwt=$(jq -er '.token' <<<"$service_auth") service_auth= echo "getServiceAuth: succeeded (aud=$knot_did, lxm=sh.tangled.repo.create)" # This request is deliberately issued exactly once. Never add retry logic here. create_response=$(jq -nc \ '{rkey:"shuttle-test",name:"shuttle-test",defaultBranch:"main"}' | \ curl -sS -H 'Host: knot.example.org' "$knot_url/xrpc/sh.tangled.repo.create" \ -H 'Content-Type: application/json' -H "Authorization: Bearer $service_jwt" \ --data-binary @- -w $'\n%{http_code}') service_jwt= status=${create_response##*$'\n'} body=${create_response%$'\n'*} echo "repo.create response ($status): $body" minted_did=$(jq -er '.repoDid // .did // empty' <<<"$body" || true) if [[ -n "$minted_did" ]]; then printf '\nSHUTTLE_TEST_REPO_DID=%s\n' "$minted_did" >>"$env_file" export SHUTTLE_TEST_REPO_DID=$minted_did echo "recorded minted repo DID immediately: $minted_did" fi if [[ "$status" != 200 || -z "$minted_did" ]]; then echo "repo.create failed; it will not be retried" >&2 exit 1 fi fi repo_record=$(jq -nc --arg knot knot.example.org --arg repoDid "$SHUTTLE_TEST_REPO_DID" \ --arg createdAt "$created_at" \ '{"$type":"sh.tangled.repo",knot:$knot,repoDid:$repoDid,name:"shuttle-test",createdAt:$createdAt}') put_record sh.tangled.repo shuttle-test "$repo_record" echo "repo record: sh.tangled.repo/shuttle-test ($SHUTTLE_TEST_REPO_DID)"