Skip to content

Commit 803d6b3

Browse files
committed
Update sonar script
1 parent 7c33779 commit 803d6b3

2 files changed

Lines changed: 69 additions & 37 deletions

File tree

Makefile

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -87,43 +87,7 @@ kill:
8787
.PHONY: sonar sonar-cloud coverage-sonar sbom sbom-upload gitleaks fmt lint vet
8888

8989
sonar: ## Run sonar-scanner for SonarQube analysis
90-
@mkdir -p $(SONAR_DIR) $(COVERAGE_DIR)
91-
@echo "Generating coverage report for SonarQube..."
92-
@python -m pytest --cov=claude_code_api --cov-report=xml:$(COVERAGE_DIR)/coverage.xml --cov-report=term-missing --junitxml=$(SONAR_DIR)/xunit-report.xml -v tests/
93-
@if command -v sonar-scanner >/dev/null 2>&1; then \
94-
if [ -f ".env.vault" ]; then \
95-
. ./.env.vault; \
96-
fi; \
97-
if [ -f ".env" ]; then \
98-
set -a; . ./.env; set +a; \
99-
fi; \
100-
if [ -n "$${VAULT_SECRET_PATHS:-}" ] || [ -n "$${VAULT_REQUIRED_VARS:-}" ]; then \
101-
if [ -f "./scripts/vault-helper.sh" ]; then \
102-
. ./scripts/vault-helper.sh; \
103-
vault_helper::load_from_definitions "$${VAULT_SECRET_PATHS:-}" "$${VAULT_REQUIRED_VARS:-}" "$${VAULT_TOKEN_FILE:-}"; \
104-
fi; \
105-
fi; \
106-
SONAR_HOST_URL="$${SONAR_HOST_URL:-$${SONAR_URL:-}}"; \
107-
if [ -z "$$SONAR_HOST_URL" ]; then \
108-
echo "SONAR_URL or SONAR_HOST_URL is required (e.g., https://sonarcloud.io or https://sonar.local)"; \
109-
exit 1; \
110-
fi; \
111-
case "$$SONAR_HOST_URL" in \
112-
http://*|https://*) ;; \
113-
*) echo "SONAR_URL must include http(s) scheme: $$SONAR_HOST_URL"; exit 1 ;; \
114-
esac; \
115-
if [ -z "$${SONAR_TOKEN:-}" ]; then \
116-
echo "SONAR_TOKEN not set - proceeding without authentication"; \
117-
fi; \
118-
sonar-scanner \
119-
-Dsonar.host.url=$$SONAR_HOST_URL \
120-
-Dsonar.token=$$SONAR_TOKEN \
121-
-Dsonar.projectVersion=$${VERSION:-1.0.0} \
122-
-Dsonar.working.directory=$(SONAR_DIR)/scannerwork; \
123-
else \
124-
echo "sonar-scanner not found. Install with: brew install sonar-scanner or download from https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/"; \
125-
exit 1; \
126-
fi
90+
@SONAR_DIR=$(SONAR_DIR) COVERAGE_DIR=$(COVERAGE_DIR) VERSION=$(VERSION) ./scripts/run-sonar.sh
12791

12892
sonar-cloud: ## Run sonar-scanner for SonarCloud (uses different token/env)
12993
@echo "Running SonarCloud scanner..."

scripts/run-sonar.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5+
cd "$ROOT_DIR"
6+
7+
SONAR_DIR="${SONAR_DIR:-dist/quality/sonar}"
8+
COVERAGE_DIR="${COVERAGE_DIR:-dist/quality/coverage}"
9+
VERSION="${VERSION:-1.0.0}"
10+
11+
mkdir -p "$SONAR_DIR" "$COVERAGE_DIR"
12+
13+
echo "Generating coverage report for SonarQube..."
14+
python -m pytest \
15+
--cov=claude_code_api \
16+
--cov-report=xml:"$COVERAGE_DIR/coverage.xml" \
17+
--cov-report=term-missing \
18+
--junitxml="$SONAR_DIR/xunit-report.xml" \
19+
-v tests/
20+
21+
if ! command -v sonar-scanner >/dev/null 2>&1; then
22+
echo "sonar-scanner not found. Install with: brew install sonar-scanner or download from https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/"
23+
exit 1
24+
fi
25+
26+
if [ -f ".env.vault" ]; then
27+
. ./.env.vault
28+
fi
29+
30+
if [ -f ".env" ]; then
31+
set -a
32+
. ./.env
33+
set +a
34+
fi
35+
36+
if [ -n "${VAULT_SECRET_PATHS:-}" ] || [ -n "${VAULT_REQUIRED_VARS:-}" ]; then
37+
if [ -f "./scripts/vault-helper.sh" ]; then
38+
. ./scripts/vault-helper.sh
39+
vault_helper::load_from_definitions \
40+
"${VAULT_SECRET_PATHS:-}" \
41+
"${VAULT_REQUIRED_VARS:-}" \
42+
"${VAULT_TOKEN_FILE:-}"
43+
fi
44+
fi
45+
46+
SONAR_HOST_URL="${SONAR_HOST_URL:-${SONAR_URL:-}}"
47+
if [ -z "$SONAR_HOST_URL" ]; then
48+
echo "SONAR_URL or SONAR_HOST_URL is required (e.g., https://sonarcloud.io or https://sonar.local)"
49+
exit 1
50+
fi
51+
52+
case "$SONAR_HOST_URL" in
53+
http://*|https://*) ;;
54+
*)
55+
echo "SONAR_URL must include http(s) scheme: $SONAR_HOST_URL"
56+
exit 1
57+
;;
58+
esac
59+
60+
if [ -z "${SONAR_TOKEN:-}" ]; then
61+
echo "SONAR_TOKEN not set - proceeding without authentication"
62+
fi
63+
64+
sonar-scanner \
65+
-Dsonar.host.url="$SONAR_HOST_URL" \
66+
-Dsonar.token="$SONAR_TOKEN" \
67+
-Dsonar.projectVersion="$VERSION" \
68+
-Dsonar.working.directory="$SONAR_DIR/scannerwork"

0 commit comments

Comments
 (0)