chore: add local pre-CI patch report generation for backend and frontend coverage

- Implemented a new script `local-patch-report.sh` to generate a local patch report.
- The report computes patch coverage based on changes from the current branch against `origin/main`.
- Integrated backend and frontend coverage inputs, producing both Markdown and JSON output artifacts.
- Updated existing frontend coverage script to validate the presence of LCOV coverage file.
- Added tests for coverage computation and parsing of unified diffs for changed lines.
- Enhanced error handling and validation for coverage inputs and baseline references.
This commit is contained in:
GitHub Actions
2026-02-17 13:11:29 +00:00
parent 7c82f5ad0d
commit aefbc5eee8
9 changed files with 1451 additions and 192 deletions

View File

@@ -28,12 +28,18 @@ mkdir -p coverage/.tmp
npm run test:coverage -- --run
SUMMARY_FILE="coverage/coverage-summary.json"
LCOV_FILE="coverage/lcov.info"
if [ ! -f "$SUMMARY_FILE" ]; then
echo "Error: Coverage summary file not found at $SUMMARY_FILE"
exit 1
fi
if [ ! -f "$LCOV_FILE" ]; then
echo "Error: LCOV coverage file not found at $LCOV_FILE"
exit 1
fi
# Extract coverage metrics and validate
LINES_PERCENT=$(python3 - <<'PY'
import json

52
scripts/local-patch-report.sh Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BASELINE="${CHARON_PATCH_BASELINE:-origin/main...HEAD}"
BACKEND_COVERAGE_FILE="$ROOT_DIR/backend/coverage.txt"
FRONTEND_COVERAGE_FILE="$ROOT_DIR/frontend/coverage/lcov.info"
JSON_OUT="$ROOT_DIR/test-results/local-patch-report.json"
MD_OUT="$ROOT_DIR/test-results/local-patch-report.md"
if ! command -v git >/dev/null 2>&1; then
echo "Error: git is required to generate local patch report." >&2
exit 1
fi
if ! command -v go >/dev/null 2>&1; then
echo "Error: go is required to generate local patch report." >&2
exit 1
fi
if [[ ! -f "$BACKEND_COVERAGE_FILE" ]]; then
echo "Error: backend coverage input missing at $BACKEND_COVERAGE_FILE" >&2
exit 1
fi
if [[ ! -f "$FRONTEND_COVERAGE_FILE" ]]; then
echo "Error: frontend coverage input missing at $FRONTEND_COVERAGE_FILE" >&2
exit 1
fi
BASE_REF="$BASELINE"
if [[ "$BASELINE" == *"..."* ]]; then
BASE_REF="${BASELINE%%...*}"
fi
if [[ -n "$BASE_REF" ]] && ! git -C "$ROOT_DIR" rev-parse --verify --quiet "${BASE_REF}^{commit}" >/dev/null; then
echo "Error: baseline base ref '$BASE_REF' is not available locally. Set CHARON_PATCH_BASELINE to a valid range and retry." >&2
exit 1
fi
mkdir -p "$ROOT_DIR/test-results"
(
cd "$ROOT_DIR/backend"
go run ./cmd/localpatchreport \
--repo-root "$ROOT_DIR" \
--baseline "$BASELINE" \
--backend-coverage "$BACKEND_COVERAGE_FILE" \
--frontend-coverage "$FRONTEND_COVERAGE_FILE" \
--json-out "$JSON_OUT" \
--md-out "$MD_OUT"
)