如何使用 REST API 获取项目中按认知复杂度排序的前 n 个文件?

How to get the top-n files sorted by cognitive complexity in a project using REST API?

如何获取项目中按认知复杂度排序的前 500 个文件(使用 REST API)?目的是导出指标以供其他工具使用。

在当前的 SonarQube(8.2,尽管根据文档,这也适用于早期版本),并假设您的实例在 localhost:9000 上并且项目名称是 project1,这bash 脚本 curls SonarQube 用于前 500 个文件及其认知复杂度值,按认知复杂度排序,然后用 jq 漂亮地打印它,并在 less 中显示它:

#!/bin/bash                                                                                                                                                                 

curl \                                                                                                                                                                      
"localhost:9000"\                                                                                                                                                           
"/api/measures/component_tree?"\                                                                                                                                            
"component=project1&"\                                                                                                                                                          
"strategy=leaves&"\                                                                                                                                                         
"metricKeys=cognitive_complexity&"\                                                                                                                                         
"s=metric&"\                                                                                                                                                                
"metricSort=cognitive_complexity&"\                                                                                                                                         
"asc=false&"\                                                                                                                                                               
"ps=500" \                                                                                                                                                                  
| jq "[.components[] | {path: .path, cognitive_complexity: .measures[0].value}]" \                                                                                                                                                               
| less

以上脚本产生如下输出:

[
{
  "path": "desktop/src/main/java/bisq/desktop/main/offer/MutableOfferViewModel.java",
  "cognitive_complexity": "319"
}
{
  "path": "desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookView.java",
  "cognitive_complexity": "304"
}
{
  "path": "p2p/src/main/java/bisq/network/p2p/network/Connection.java",
  "cognitive_complexity": "228"
}
{
  "path": "desktop/src/main/java/bisq/desktop/main/support/dispute/DisputeView.java",
  "cognitive_complexity": "225"
}
{
  "path": "desktop/src/main/java/bisq/desktop/util/GUIUtil.java",
  "cognitive_complexity": "192"
}
...