You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SonarQube API获取指定组件在各分析版本下的指标详情

How to Fetch Historical Metrics for a Specific Component Across SonarQube Analyses

Great question! I’ve worked with SonarQube’s API extensively, and here’s exactly how you can get the metrics you need for all or specific analysis versions:

1. The Missing Parameter: analysisId

You’re right that the api/measures/component endpoint is the way to go for metric data, but you might have missed that it supports an optional analysisId parameter. This lets you target metrics from a specific historical analysis instead of just the latest one.

2. Step-by-Step Workflow

a. Get the Full List of Analysis IDs

First, use the api/project_analyses/search endpoint you already have to fetch all analysis records for your project:

curl "http://sonar63.rd.tut.fi/api/project_analyses/search?project=org:apache:zookeper2&ps=500"

From the JSON response, extract the key field from each entry in the analyses array—this is your analysisId for each run.

b. Fetch Metrics for Each Analysis ID

For every analysisId you extracted, call the metrics endpoint with the analysisId parameter added. Here’s an example call targeting your specific component and metrics:

curl "http://sonar63.rd.tut.fi/api/measures/component?metricKeys=complexity,ncloc&componentKey=org:apache:zookeper2:src/java/main/org/apache/zookeeper/server/quorum/AuthFastLeaderElection.java&analysisId=YOUR_ANALYSIS_ID"

Replace YOUR_ANALYSIS_ID with each key from the previous step.

c. Aggregate the Results

Parse each response to pull out:

  • The analysis ID (from your input or the response’s analysisId field)
  • Component path
  • complexity value
  • ncloc value

Then format them into the table you want:

AnalysisComponentComplexityncloc
f1234/mypackage/a.java10150
f1235/mypackage/a.java10155

3. Bonus: Filter to Specific Analyses

If you don’t need all analyses, you can filter the api/project_analyses/search results first:

  • Use the from/to parameters to target a date range
  • Look for the version field to match specific project versions
  • Check the status field to only include successful analyses

Notes to Keep in Mind

  • If your project has more than 500 analyses, use the page parameter in api/project_analyses/search to paginate through results.
  • Ensure your API token (if required) has the necessary permissions to access historical analysis data.
  • Some older SonarQube versions might have slight differences in API response structures, but the analysisId parameter has been supported for several major versions.

内容的提问来源于stack exchange,提问作者Davide Taibi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:33:11