Sonarapi python库 python-sonarqube-api简介

背景

每次我们需要使用sonar api接口时,都需要自己去查一下接口的参数,这就显得很低效,如果有现成的库,那就简省了我们好多的时间。无意中发现,python库中已经有人做成了包:
python-sonarqube-api

如果需要单独的Sonar api 使用指南,可以直接查看之前写的sonar api文档

简介

python-sonarqube-api库包含了集成了sonar多个版本的接口调用,包括社区/企业版本等,功能还是比较强大的,而且文档还算比较详细
原地址:https://github.com/shijl0925/python-sonarqube-api
api 获取的使用指南
https://python-sonarqube-api.readthedocs.io/en/latest/examples.html
Sonarapi python库 python-sonarqube-api简介_第1张图片

demo代码

以下是简单的demo

from sonarqube import SonarQubeClient



class SonarQube:

    def __init__(self,url,username="admin",password="admin") -> None:
        username = username
        password = password
        sonarqube_url = url
        self.client = SonarQubeClient(username=username, password=password,
                                 sonarqube_url=sonarqube_url)

    def getProjects(self):
        """ 获取项目列表"""
        projects=list(self.client.projects.search_projects())
        return projects

    def getMeasures(self, component):
        """ 获取项目各个参数数据"""
        metricKeys = "alert_status,bugs,,vulnerabilities,security_rating,code_smells,duplicated_lines_density,coverage,ncloc"
        measures = []
        measures.append(self.client.measures.get_component_with_specified_measures(
            component, metricKeys))
        return measures



s = SonarQube(url='http://127.0.0.0.1:30006/')
all_project_info = s.getProjects()
for project_info in all_project_info:
    component = project_info.get("key")
    b = s.getMeasures(component)
    print(b)
总结

使用比较简单,而且集成了很多常用的接口,还是比较方便的

你可能感兴趣的:(静态扫描,python测试开发,sonar,api,sonar,python,python,sonar,python-sonar)