sonarcloud-analysis について
sonarcloud-analysis は、SonarCloud を使う開発者向けのコード品質分析スキルです。課題、メトリクス、品質ゲートを調べ、ブランチやプルリクエストのセキュリティ、カバレッジ、技術的負債をまとめます。
主な機能
- 01課題、メトリクス、品質ゲートの照会
- 02ブランチとプルリクエストの分析
- 03脆弱性とセキュリティホットスポットの特定
- 04カバレッジ、重複、技術的負債の追跡
- 05健全性レポートと傾向分析の生成
活用シーン
- 01プロジェクトの品質ゲート状態の確認
- 02プルリクエスト内の新しい課題の確認
- 03脆弱性やレビュー対象の特定
- 04カバレッジと技術的負債の傾向比較
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ harshanandak/ forge --skill sonarcloud-analysisインストール後、Claude Code で/sonarcloud-analysisを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
evals/evals.jsonREADME.mdscorecard.jsonreferences/api-reference.md詳細
SonarCloud連携
ベース:https://sonarcloud.io/api | 認証:Bearer $SONARCLOUD_TOKEN
設定
環境変数:認証に必要です
SONARCLOUD_TOKEN- sonarcloud.io/account/securityで生成しますSONARCLOUD_ORG- SonarCloud組織のキーSONARCLOUD_PROJECT- プロジェクトのキー
オプション1:.env.localを使用(推奨)
プロジェクトの.env.localに追加します:
SONARCLOUD_TOKEN=your_token_here
SONARCLOUD_ORG=your-org
SONARCLOUD_PROJECT=your-project
問い合わせる前に、環境変数を読み込みます:
# Load .env.local into current environment
export $(grep -v '^#' .env.local | xargs)
オプション2:直接エクスポート
export SONARCLOUD_TOKEN="your_token"
export SONARCLOUD_ORG="your-org"
export SONARCLOUD_PROJECT="your-project"
# Common queries
curl -H "Authorization: Bearer $SONARCLOUD_TOKEN" \
"https://sonarcloud.io/api/issues/search?organization=$SONARCLOUD_ORG&componentKeys=$SONARCLOUD_PROJECT&resolved=false"
curl -H "Authorization: Bearer $SONARCLOUD_TOKEN" \
"https://sonarcloud.io/api/measures/component?component=$SONARCLOUD_PROJECT&metricKeys=bugs,coverage"
curl -H "Authorization: Bearer $SONARCLOUD_TOKEN" \
"https://sonarcloud.io/api/qualitygates/project_status?projectKey=$SONARCLOUD_PROJECT"
エンドポイント
| エンドポイント | 用途 | 主なパラメーター |
|---|---|---|
/api/issues/search | バグ、脆弱性 | types、severities、branch、pullRequest |
/api/measures/component | カバレッジ、複雑度 | metricKeys、branch、pullRequest |
/api/qualitygates/project_status | 合否ステータス | projectKey、branch、pullRequest |
/api/hotspots/search | セキュリティホットスポット | projectKey、status |
/api/projects/search | プロジェクト一覧 | organization、q |
/api/project_analyses/search | 分析履歴 | project、from、to |
/api/measures/search_history | 時系列のメトリクス | component、metrics、from |
/api/components/tree | メトリクス付きファイル | qualifiers=FIL、metricKeys |
/api/duplications/show | 重複コードブロック | key(ファイルキー)、branch |
/api/sources/raw | ソースコードの生データ | key(ファイルキー)、branch |
/api/sources/scm | SCMのブレーム情報 | key、from、to |
/api/ce/activity | バックグラウンドタスク | component、status、type |
/api/qualityprofiles/search | 品質プロファイル | language、project |
/api/languages/list | 対応言語 | - |
/api/project_branches/list | プロジェクトのブランチ | project |
/api/project_badges/measure | SVGバッジ | project、metric、branch |
/api/rules/search | コーディングルール | languages、severities、types |
共通フィルター
課題: types=BUG,VULNERABILITY,CODE_SMELL | severities=BLOCKER,CRITICAL,MAJOR | resolved=false | inNewCodePeriod=true
メトリクス: bugs,vulnerabilities,code_smells,coverage,duplicated_lines_density,sqale_rating,reliability_rating,security_rating
新規コード: new_bugs,new_vulnerabilities,new_coverage,new_duplicated_lines_density
ワークフロー
ヘルスチェック
curl ... "/api/qualitygates/project_status?projectKey=$PROJECT"
curl ... "/api/measures/component?component=$PROJECT&metricKeys=bugs,vulnerabilities,coverage,sqale_rating"
curl ... "/api/issues/search?organization=$ORG&componentKeys=$PROJECT&resolved=false&facets=severities,types&ps=1"
PR 分析
curl ... "/api/qualitygates/project_status?projectKey=$PROJECT&pullRequest=123"
curl ... "/api/issues/search?organization=$ORG&componentKeys=$PROJECT&pullRequest=123&resolved=false"
curl ... "/api/measures/component?component=$PROJECT&pullRequest=123&metricKeys=new_bugs,new_coverage"
セキュリティ監査
curl ... "/api/issues/search?organization=$ORG&componentKeys=$PROJECT&types=VULNERABILITY&resolved=false"
curl ... "/api/hotspots/search?projectKey=$PROJECT&status=TO_REVIEW"
重複分析
# Get duplication metrics
curl ... "/api/measures/component?component=$PROJECT&metricKeys=duplicated_lines,duplicated_lines_density,duplicated_blocks,duplicated_files"
# Get files with most duplication
curl ... "/api/components/tree?component=$PROJECT&qualifiers=FIL&metricKeys=duplicated_lines_density&s=metric&metricSort=duplicated_lines_density&asc=false&ps=20"
# Get duplicate blocks for a specific file (requires file key from above)
curl ... "/api/duplications/show?key=my-project:src/utils/helpers.ts"
レスポンス処理
# Count by severity
curl ... | jq '.issues | group_by(.severity) | map({severity: .[0].severity, count: length})'
# Failed quality gate conditions
curl ... | jq '.projectStatus.conditions | map(select(.status == "ERROR"))'
# Metrics as key-value
curl ... | jq '.component.measures | map({(.metric): .value}) | add'
詳細リファレンス
API パラメーターとレスポンススキーマの全項目については、references/api-reference.md を参照してください。