#!/usr/bin/env bash # Regression harness for Gitea PR metadata normalization and merge preflight. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" GIT_TOOLS_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" TEST_ROOT="${TEST_ROOT:-$(pwd)/.test-output/pr-gitea-wrapper-regression}" FAKE_BIN="$TEST_ROOT/bin" FAKE_REPO="$TEST_ROOT/repo" rm -rf "$TEST_ROOT" mkdir -p "$FAKE_BIN" "$FAKE_REPO" "$TEST_ROOT/state" cat > "$FAKE_BIN/git" <<'SH' #!/usr/bin/env bash set -euo pipefail if [[ "$*" == "remote get-url origin" ]]; then echo "https://git.uscllc.com/usc/uconnect.git" exit 0 fi echo "unexpected git invocation: $*" >&2 exit 2 SH chmod +x "$FAKE_BIN/git" cat > "$FAKE_BIN/curl" <<'SH' #!/usr/bin/env bash set -euo pipefail method="GET" out_file="" write_format="" url="" while [[ $# -gt 0 ]]; do case "$1" in -X) method="$2"; shift 2 ;; -o) out_file="$2"; shift 2 ;; -w) write_format="$2"; shift 2 ;; -H|-d) shift 2 ;; -s|-S|-f|-k|-sS|-fsS) shift ;; http*) url="$1"; shift ;; *) shift ;; esac done body='{}' code="200" if [[ "$method" == "GET" && "$url" == *"/api/v1/repos/usc/uconnect/pulls/1908" ]]; then body='{"number":1908,"title":"Test PR","body":"","state":"open","user":{"login":"edith"},"head":{"label":"fix/t_23fa9e1d-portal-health-backend","ref":"refs/pull/1908/head","sha":"abc123"},"base":{"label":"main","ref":"main","sha":"def456"},"labels":[],"assignees":[],"created_at":"2026-05-22T00:00:00Z","updated_at":"2026-05-22T00:00:00Z","html_url":"https://git.uscllc.com/usc/uconnect/pulls/1908","draft":false,"mergeable":true,"diff_url":"https://git.uscllc.com/usc/uconnect/pulls/1908.diff"}' elif [[ "$method" == "POST" && "$url" == *"/api/v1/repos/usc/uconnect/pulls/1908/merge" ]]; then echo "$url" > "${TEST_ROOT:?}/state/merge-url" body='{"merged":true}' else code="404" body='{"message":"not found"}' fi if [[ -n "$out_file" ]]; then printf '%s' "$body" > "$out_file" else printf '%s' "$body" fi if [[ -n "$write_format" ]]; then printf '%s' "$code" fi SH chmod +x "$FAKE_BIN/curl" cat > "$FAKE_BIN/tea" <<'SH' #!/usr/bin/env bash echo "tea must not be invoked by Gitea merge preflight" >&2 exit 99 SH chmod +x "$FAKE_BIN/tea" cat > "$TEST_ROOT/credentials.json" <<'JSON' { "gitea": { "usc": {"url": "https://git.uscllc.com", "token": "fake-token-usc"}, "mosaicstack": {"url": "https://git.mosaicstack.dev", "token": "fake-token-mosaic"} } } JSON export PATH="$FAKE_BIN:$PATH" export TEST_ROOT export MOSAIC_CREDENTIALS_FILE="$TEST_ROOT/credentials.json" cd "$FAKE_REPO" metadata="$("$GIT_TOOLS_DIR/pr-metadata.sh" -n 1908)" python3 - "$metadata" <<'PY' import json import sys metadata = json.loads(sys.argv[1]) assert metadata["baseRefName"] == "main", metadata assert metadata["headRefName"] == "fix/t_23fa9e1d-portal-health-backend", metadata PY merge_output="$("$GIT_TOOLS_DIR/pr-merge.sh" -n 1908 -m squash --skip-queue-guard --dry-run 2>&1)" if grep -q "mosaicstack\|Login name\|tea must not" <<<"$merge_output"; then echo "$merge_output" >&2 exit 1 fi if ! grep -q "Dry run: Gitea merge preflight OK" <<<"$merge_output"; then echo "$merge_output" >&2 exit 1 fi printf 'Gitea PR metadata and merge preflight regression passed\n'