Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

4.3 KB
Raw Blame
#!/usr/bin/env bash
# Runs the Repowise CI gates for the composite action in action.yml.
#
# Every selected gate runs even when an earlier one fails, so one job reports
# all of them. Exit codes follow the gates: 1 when any gate failed, 2 when any
# could not evaluate (and none failed), 0 otherwise. Inputs arrive as
# environment variables set by action.yml; nothing is interpolated into code.

set -u

# Commas, spaces and newlines all separate names, so a YAML block list works.
names=$(tr -s ', \t\r\n' ' ' <<<"$CHECKS")
checks=" $names "
wants() { [[ "$checks" == *" $1 "* ]]; }

if [[ -z "${names// /}" ]]; then
  echo "::error::No check selected; set checks to coverage, doc-drift, security, health or risk."
  exit 2
fi
for name in $names; do
  case "$name" in
    coverage | doc-drift | security | health | risk) ;;
    *) echo "::error::Unknown check '$name'; use coverage, doc-drift, security, health or risk." ; exit 2 ;;
  esac
done

# Coverage, security, health and risk diff the change, so they need its history
# and the target branch; risk also ranks it against recent commits. Baseline growth
# reads the doc-drift baseline at the change's base.
growth=()
[[ "${FAIL_ON_BASELINE_GROWTH:-}" == "true" ]] && growth=(--fail-on-baseline-growth)
if wants coverage || wants security || wants health || wants risk || { wants doc-drift && [[ -n "$DOC_DRIFT_BASELINE" && ${#growth[@]} -gt 0 ]]; }; then
  here=${0//\\//}
  source "${here%/*}/fetch-base.sh"
fi

failed=0
broken=0

# run NAME COMMAND...: one gate in a log group, its exit code recorded.
run() {
  local name=$1
  shift
  echo "::group::repowise $name"
  "$@"
  local code=$?
  echo "::endgroup::"
  echo "$name=$code" >>"$GITHUB_OUTPUT"
  case "$code" in
    0) ;;
    1) failed=1 ;;
    *) broken=1 ;;
  esac
}

base=()
[[ -n "$BASE" ]] && base=("$BASE")

if wants coverage; then
  args=(coverage check ${base[@]+"${base[@]}"} --format github)
  while IFS= read -r report; do
    [[ -n "$report" ]] && args+=(--report "$report")
  done <<<"$COVERAGE_REPORT"
  [[ -n "$COVERAGE_FAIL_UNDER" ]] && args+=(--fail-under "$COVERAGE_FAIL_UNDER")
  [[ -n "$COVERAGE_MIN_COVERABLE_LINES" ]] && args+=(--min-coverable-lines "$COVERAGE_MIN_COVERABLE_LINES")
  [[ -n "$COVERAGE_FAIL_UNDER_RISKY" ]] && args+=(--fail-under-risky "$COVERAGE_FAIL_UNDER_RISKY")
  [[ -n "$COVERAGE_FAIL_UNDER_BRANCHES" ]] && args+=(--fail-under-branches "$COVERAGE_FAIL_UNDER_BRANCHES")
  while IFS= read -r report; do
    [[ -n "$report" ]] && args+=(--base-report "$report")
  done <<<"$COVERAGE_BASE_REPORT"
  [[ -n "$COVERAGE_MAX_DROP" ]] && args+=(--max-drop "$COVERAGE_MAX_DROP")
  run coverage repowise "${args[@]}"
fi

drift=(doc-drift --check)
[[ -n "$DOC_DRIFT_BASELINE" ]] && drift+=(--baseline "$DOC_DRIFT_BASELINE" ${growth[@]+"${growth[@]}"})
security=(security check ${base[@]+"${base[@]}"} --fail-on "$SECURITY_FAIL_ON")
[[ -n "$SECURITY_BASELINE" ]] && security+=(--baseline "$SECURITY_BASELINE" ${growth[@]+"${growth[@]}"})
health=(health check ${base[@]+"${base[@]}"} --fail-on "$HEALTH_FAIL_ON" --fail-at "$HEALTH_FAIL_AT")

wants doc-drift && run doc-drift repowise "${drift[@]}" --format github
wants security && run security repowise "${security[@]}" --format github
wants health && run health repowise "${health[@]}" --format github

if wants risk; then
  # Without a base, --format github scores the pull request against its target
  # branch. Without a percentile the rank is reported, not gated.
  risk=(risk ${base[@]+"${base[@]}"} --format github)
  [[ -n "$RISK_FAIL_ABOVE_PERCENTILE" ]] && risk+=(--fail-above-percentile "$RISK_FAIL_ABOVE_PERCENTILE")
  run risk repowise "${risk[@]}"
fi

# SARIF is a second run in its own format. A gate that could not evaluate
# writes an empty file, which code scanning would reject, so it is dropped.
if [[ "$SARIF" == "true" ]] && { wants doc-drift || wants security || wants health; }; then
  mkdir -p "$SARIF_DIR"
  wants doc-drift && repowise "${drift[@]}" --format sarif >"$SARIF_DIR/doc-drift.sarif"
  wants security && repowise "${security[@]}" --format sarif >"$SARIF_DIR/security.sarif"
  wants health && repowise "${health[@]}" --format sarif >"$SARIF_DIR/health.sarif"
  find "$SARIF_DIR" -name '*.sarif' -empty -delete
  if [[ -n "$(ls -A "$SARIF_DIR")" ]]; then
    echo "sarif-dir=$SARIF_DIR" >>"$GITHUB_OUTPUT"
  fi
fi

if ((failed)); then
  exit 1
fi
if ((broken)); then
  exit 2
fi
exit 0