#!/bin/sh set -eu root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) task="$root/catalog/maidn-preview-orphan-reconciler.yaml" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT ! grep -q 'secretKeyRef' "$task" grep -qF 'key: forgejo-origin' "$task" awk ' / - name: reconcile/ { found=1 } found && /^ script: \|/ { script=1; next } script && /^ - name:/ { exit } script { sub(/^ /, ""); print } ' "$task" > "$tmp/reconcile.sh" mkdir "$tmp/bin" "$tmp/fixture" cat > "$tmp/bin/git" <<'EOF' #!/bin/sh set -eu case "$1" in clone) for arg; do target=$arg; done cp -R "$TEST_FIXTURE" "$target" ;; -C) shift 2 case "$1" in remote) [ "$2" = get-url ] && [ "$3" = origin ] || exit 1 printf '%s\n' "$TEST_ORIGIN" ;; rev-parse|config|add|push) exit 0 ;; diff) exit 1 ;; esac ;; diff) exit 1 ;; commit) [ ! -e "$PWD/apps/previews/web-ui-pr-42" ] ! grep -qF ' - web-ui-pr-42' "$PWD/apps/previews/kustomization.yaml" ;; config|add|push) exit 0 ;; *) exit 1 ;; esac EOF chmod +x "$tmp/bin/git" marker() { cat <<'EOF' apiVersion: v1 kind: ConfigMap metadata: name: maidn-preview-owner namespace: web-ui-pr-42 labels: maidn.io/preview-owner: "true" maidn.io/preview-app: "web-ui" maidn.io/preview-pr: "42" annotations: maidn.io/preview-repository: "platform/web-ui" data: app: "web-ui" repository: "platform/web-ui" pr-number: "42" EOF } fixture() { dir=$1 mkdir -p "$dir/apps/previews/web-ui-pr-42" cat > "$dir/apps/previews/kustomization.yaml" <<'EOF' apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - web-ui-pr-42 EOF marker > "$dir/apps/previews/web-ui-pr-42/ownership.yaml" : > "$dir/apps/previews/web-ui-pr-42/namespace.yaml" : > "$dir/apps/previews/web-ui-pr-42/values.yaml" : > "$dir/apps/previews/web-ui-pr-42/kustomization.yaml" : > "$dir/apps/previews/web-ui-pr-42/release.yaml" } run() { TRUSTED_FORGEJO_ORIGIN=https://git.example.invalid \ TRUSTED_MANIFESTS_URL=https://git.example.invalid/platform/manifests.git \ TRUSTED_MANIFESTS_BRANCH=main APP_NAME="$1" APP_REPOSITORY=platform/web-ui PR_NUMBER=42 \ TEST_FIXTURE="$tmp/fixture" PATH="$tmp/bin:$PATH" \ TEST_ORIGIN="${TEST_ORIGIN:-https://git.example.invalid/platform/manifests.git}" \ sh "$tmp/reconcile.sh" } fixture "$tmp/fixture" run web-ui [ -f "$tmp/fixture/apps/previews/web-ui-pr-42/ownership.yaml" ] grep -qF ' - web-ui-pr-42' "$tmp/fixture/apps/previews/kustomization.yaml" rm -rf "$tmp/fixture" fixture "$tmp/fixture" printf '\n' >> "$tmp/fixture/apps/previews/web-ui-pr-42/ownership.yaml" if run web-ui; then exit 1; fi [ -f "$tmp/fixture/apps/previews/web-ui-pr-42/ownership.yaml" ] rm -rf "$tmp/fixture" fixture "$tmp/fixture" rm "$tmp/fixture/apps/previews/web-ui-pr-42/ownership.yaml" ln -s /tmp/not-owned "$tmp/fixture/apps/previews/web-ui-pr-42/ownership.yaml" if run web-ui; then exit 1; fi rm -rf "$tmp/fixture" fixture "$tmp/fixture" TEST_ORIGIN=https://git.example.invalid/platform/other.git if run web-ui; then exit 1; fi if run 'web-ui/escape'; then exit 1; fi