#!/usr/bin/env bash
# Block pushes whose new commits contain a secret, in content or in messages.
# stdin: "<local_ref> <local_sha> <remote_ref> <remote_sha>" per pushed ref.

# Advisory, never blocks: GitHub disables scheduled workflows after 60 days without
# repo activity, and a disabled workflow ignores every trigger, pushes included.
if command -v gh >/dev/null 2>&1; then
  for wf in gitleaks.yml live-api.yml; do
    # on an HTTP error (e.g. 404 before the workflow exists) gh prints the error body
    # to stdout and exits non-zero, so a failed call is skipped, not reported as a state
    st=$(timeout 5 gh api "repos/{owner}/{repo}/actions/workflows/$wf" --jq .state </dev/null 2>/dev/null) || continue
    [ -n "$st" ] && [ "$st" != active ] && echo "⚠️  workflow $wf is '$st' — it will not run on GitHub (gh workflow enable $wf)" >&2
  done
fi

command -v gitleaks >/dev/null 2>&1 || { echo "⚠️  gitleaks not installed — skipping" >&2; exit 0; }
z=0000000000000000000000000000000000000000; fail=0
tmp=$(mktemp -d) || exit 0; trap 'rm -rf "$tmp"' EXIT
while read -r _ local_sha _ remote_sha; do
  [ "$local_sha" = "$z" ] && continue                       # deleting a ref
  if [ "$remote_sha" = "$z" ]; then set -- "$local_sha" --not --remotes   # new branch
  else set -- "$remote_sha..$local_sha"; fi                 # existing branch: new commits only
  gitleaks detect --log-opts="$*" --redact -v || fail=1     # v8.19+: gitleaks git --log-opts=...
  git log --format=%B "$@" > "$tmp/m" 2>/dev/null
  [ -s "$tmp/m" ] && { gitleaks detect --no-git --source "$tmp/m" --config .gitleaks.toml --redact -v || fail=1; }
done
[ "$fail" -eq 0 ] || { echo "❌ push blocked (see finding above)" >&2; exit 1; }
