| name | sonar |
| description | SonarQube 代码质量平台操作。用于:(1) 查询代码质量报告 (2) 分析项目质量 gate 状态 (3) 管理质量规则 (4) 查看代码热点和漏洞 |
SonarQube Skill
配置
SONAR_URL=http://sonar.example.com:9000
SONAR_TOKEN=your-sonar-token
获取 Token
- 登录 SonarQube
- 点击右上角头像 → "My Account"
- 左侧选择 "Security"
- 在 "Generate Tokens" 部分输入名称生成
- 复制生成的 token
注意: SonarQube 9.0+ 使用 token 作为用户名,密码为空:
curl -u "$SONAR_TOKEN:" "$SONAR_URL/api/projects/search"
操作示例
source ~/.openclaw/workspace/.env
项目操作
source ~/.openclaw/workspace/.env
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/projects/search?projects=PROJECT_KEY"
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/measures/component?component=PROJECT_KEY&metricKeys=bugs,vulnerabilities,code_smells,coverage"
代码质量 Gate
source ~/.openclaw/workspace/.env
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/qualitygates/project_status?project=PROJECT_KEY"
代码问题
source ~/.openclaw/workspace/.env
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/issues/search?projects=PROJECT_KEY&status=OPEN&types=BUG"
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/hotspots/search?project=PROJECT_KEY"
代码度量
source ~/.openclaw/workspace/.env
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/metrics/search"
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/measures/component?component=PROJECT_KEY&metricKeys=ncloc,lines"
触发扫描
source ~/.openclaw/workspace/.env
sonar-scanner \
-Dsonar.projectKey=PROJECT_KEY \
-Dsonar.sources=./src \
-Dsonar.host.url=$SONAR_URL \
-Dsonar.token=$SONAR_TOKEN
Webhooks
source ~/.openclaw/workspace/.env
curl -X POST -u "$SONAR_TOKEN:" \
-H "Content-Type: application/json" \
"$SONAR_URL/api/webhooks/create" \
-d '{
"name": "CI Webhook",
"url": "https://your-callback.com/sonar",
"secret": "optional-secret"
}'
常用场景
- 质量检查: 查看项目是否通过质量 gate
- Bug 追踪: 列出代码中的 bug 和漏洞
- 代码覆盖: 查看测试覆盖率
- 热点审查: 识别安全热点
使用场景
场景 1: CI/CD 质量门禁
在部署前检查代码质量是否达标:
source ~/.openclaw/workspace/.env
GATE_STATUS=$(curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/qualitygates/project_status?project=my-app" | \
jq -r '.projectStatus.status')
if [ "$GATE_STATUS" != "OK" ]; then
echo "质量门禁未通过!"
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/qualitygates/project_status?project=my-app" | \
jq '.projectStatus.conditions[] | select(.status != "OK")'
exit 1
fi
echo "质量检查通过,可以部署"
场景 2: 统计代码质量趋势
获取项目代码度量:
source ~/.openclaw/workspace/.env
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/measures/component?component=my-app&metricKeys=bugs,vulnerabilities,code_smells,coverage,duplicated_lines_density" | \
jq '.component.measures[] | {metric: .metric, value: .value}'
场景 3: 自动修复问题
获取需要修复的问题列表:
source ~/.openclaw/workspace/.env
curl -s -u "$SONAR_TOKEN:" \
"$SONAR_URL/api/issues/search?projects=my-app&types=BUG&status=OPEN&sinceLeakPeriod=true" | \
jq '.issues[] | {key: .key, message: .message, file: .component}'
常用 API
| API | 说明 |
|---|
/api/projects/search | 项目列表 |
/api/qualitygates/project_status | 质量 gate 状态 |
/api/issues/search | 问题列表 |
/api/measures/component | 代码度量 |
/api/hotspots/search | 安全热点 |
注意事项
- Token 需要有对应项目权限
- 新版 SonarQube (10.0+) API 可能有变化
- 某些 API 需要管理员权限