114 lines
3.6 KiB
Bash
Executable File
114 lines
3.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
QUALITY_METRICS_ENABLED="${ANANKE_QUALITY_METRICS_ENABLED:-1}"
|
|
QUALITY_METRICS_FILE="${ANANKE_QUALITY_METRICS_FILE:-/var/lib/ananke/quality-gate.prom}"
|
|
QUALITY_STATE_FILE="${ANANKE_QUALITY_STATE_FILE:-/var/lib/ananke/quality-gate.state}"
|
|
|
|
read_quality_counter() {
|
|
local key="$1"
|
|
if [[ ! -f "${QUALITY_STATE_FILE}" ]]; then
|
|
echo 0
|
|
return 0
|
|
fi
|
|
local value
|
|
value="$(awk -F= -v key="${key}" '$1==key {print $2}' "${QUALITY_STATE_FILE}" | tail -n1)"
|
|
if [[ ! "${value}" =~ ^[0-9]+$ ]]; then
|
|
echo 0
|
|
return 0
|
|
fi
|
|
echo "${value}"
|
|
}
|
|
|
|
write_quality_metrics() {
|
|
local exit_code="$1"
|
|
if [[ "${QUALITY_METRICS_ENABLED}" != "1" ]]; then
|
|
return 0
|
|
fi
|
|
|
|
local metrics_dir state_dir
|
|
metrics_dir="$(dirname "${QUALITY_METRICS_FILE}")"
|
|
state_dir="$(dirname "${QUALITY_STATE_FILE}")"
|
|
mkdir -p "${metrics_dir}" "${state_dir}" >/dev/null 2>&1 || return 0
|
|
|
|
local ok failed total last_success now success_percent
|
|
ok="$(read_quality_counter ok)"
|
|
failed="$(read_quality_counter failed)"
|
|
last_success=0
|
|
if [[ "${exit_code}" -eq 0 ]]; then
|
|
ok=$((ok + 1))
|
|
last_success=1
|
|
else
|
|
failed=$((failed + 1))
|
|
fi
|
|
total=$((ok + failed))
|
|
now="$(date +%s)"
|
|
success_percent="$(awk -v ok="${ok}" -v total="${total}" 'BEGIN { if (total <= 0) { print "0.00" } else { printf "%.2f", (ok * 100.0) / total } }')"
|
|
|
|
local tmp_metrics tmp_state
|
|
tmp_metrics="$(mktemp "${metrics_dir}/quality-gate.prom.XXXXXX")"
|
|
tmp_state="$(mktemp "${state_dir}/quality-gate.state.XXXXXX")"
|
|
|
|
cat > "${tmp_metrics}" <<EOF
|
|
# HELP ananke_quality_gate_runs_total Total Ananke quality gate runs by status.
|
|
# TYPE ananke_quality_gate_runs_total counter
|
|
ananke_quality_gate_runs_total{suite="ananke",status="ok"} ${ok}
|
|
ananke_quality_gate_runs_total{suite="ananke",status="failed"} ${failed}
|
|
# HELP ananke_quality_gate_last_run_success Whether the latest quality gate run succeeded.
|
|
# TYPE ananke_quality_gate_last_run_success gauge
|
|
ananke_quality_gate_last_run_success{suite="ananke"} ${last_success}
|
|
# HELP ananke_quality_gate_last_run_timestamp_seconds Unix timestamp of the latest quality gate run.
|
|
# TYPE ananke_quality_gate_last_run_timestamp_seconds gauge
|
|
ananke_quality_gate_last_run_timestamp_seconds{suite="ananke"} ${now}
|
|
# HELP ananke_quality_gate_success_percent Running quality gate success percentage for Ananke.
|
|
# TYPE ananke_quality_gate_success_percent gauge
|
|
ananke_quality_gate_success_percent{suite="ananke"} ${success_percent}
|
|
EOF
|
|
|
|
cat > "${tmp_state}" <<EOF
|
|
ok=${ok}
|
|
failed=${failed}
|
|
last_success=${last_success}
|
|
last_run=${now}
|
|
EOF
|
|
|
|
mv -f "${tmp_metrics}" "${QUALITY_METRICS_FILE}"
|
|
mv -f "${tmp_state}" "${QUALITY_STATE_FILE}"
|
|
}
|
|
|
|
quality_gate_finalize() {
|
|
local exit_code="$1"
|
|
set +e
|
|
write_quality_metrics "${exit_code}" || true
|
|
exit "${exit_code}"
|
|
}
|
|
|
|
trap 'quality_gate_finalize $?' EXIT
|
|
|
|
cd "${REPO_DIR}"
|
|
|
|
echo "[quality] unit tests"
|
|
go test ./...
|
|
|
|
echo "[quality] hygiene: doc contracts"
|
|
cd testing
|
|
go test ./hygiene -run TestHygieneContracts/doc_contract -count=1
|
|
|
|
echo "[quality] hygiene: naming contracts"
|
|
go test ./hygiene -run TestHygieneContracts/naming_contract -count=1
|
|
|
|
echo "[quality] hygiene: LOC limits"
|
|
go test ./hygiene -run TestHygieneContracts/loc_limit -count=1
|
|
cd "${REPO_DIR}"
|
|
|
|
echo "[quality] lint"
|
|
./scripts/lint.sh
|
|
|
|
echo "[quality] installer template contracts"
|
|
./scripts/verify_install_templates.sh
|
|
|
|
echo "[quality] per-file coverage gate (95%)"
|
|
cd testing
|
|
ANANKE_ENFORCE_COVERAGE=1 ANANKE_PER_FILE_COVERAGE_TARGET=95 go test ./coverage -run TestPerFileCoverageReport -count=1 -v
|