python 获取sonarqube数据

1.sonarqube是一款代码分析的工具,通过soanrScanner扫描后的数据传递给sonarqube进行分析
2.sonarqube社区版没有对c++类代码的分析,但是可以找到一个开源的包,安装即可,扫描的话可以使用cppcheck来进行扫描

  1. 安装python对于sonarqube的api包:python-sonarqube-api
  2. 建立sonarqube连接
from sonarqube import SonarQubeClient

sonar = SonarQubeClient(
    sonarqube_url="http://192.168.xx.xx:9000",
    username='admin',
    password='admin'
)
  1. 使用:建议大家先参考sonarqube的python-api

https://python-sonarqube-api.readthedocs.io/en/latest/

  1. 使用示例
# 通过项目名称获取id
# 传递参数:创建分析配置文件时候的项目名称
component = sonar.components.get_project_component_and_ancestors("python_test")

# 获取任务
# 参数1:上一步获取的component值
# 参数2:逗号分割的状态值
tasks1 = sonar.ce.search_tasks(
    componentId="AX5v36mo0Npud3J2od3a",
    status="FAILED,CANCELED,PENDING,IN_PROGRESS,SUCCESS"
)

# 获取所有项目
projects = list(sonar.projects.search_projects())

# 获取这个项目下最近一次分析任务的详情
"""
componentKeys:某一个项目名称
types:参数类型
CODE_SMELL==异常
BUG==bug
VULNERABILITY==漏洞
SECURITY_HOTSPOT==
"""
issues2 = list(sonar.issues.search_issues(componentKeys="python_s", types="CODE_SMELL"))

  1. 通过metricKeys参数获取这个项目中需要的值
# 参数1:component 项目名称
# 参数2:metricKeys 想要获取的某个值,逗号分割
component_data = sonar.measures.get_component_with_specified_measures(
    component="python_test",
    metricKeys="functions,classes"
)['component']['measures']

# 目前已收集的值和含义
'''
ncloc==总代码长度
ncloc_language_distribution==其中每种语言的行数
bugs==bug数
vulnerabilities==漏洞数
code_smells==异常数
duplicated_lines_density==重复度百分比
coverage==代码覆盖率百分比
files==文件数量
functions==方法数量
classes==类数量
'''
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值