curl webapi sonarqube,shell,python3

该文描述了一个使用SonarQubeAPI进行项目质量检查的过程,包括获取项目指标如bug、漏洞和覆盖率等。通过Shell命令和Python脚本,自动化收集和处理数据,生成CSV报告,并实现远程文件传输。Python脚本使用了python-sonarqube-api库,对多个项目的数据进行聚合和排序,最后计算各组件的bug和漏洞总数。
摘要由CSDN通过智能技术生成

 

SonarQube

shell: 

token=46d4ac9295ec62dc14687096142e0a76718f804b
#curl -s -X GET  -H "token:$token"  "http://172.16.10.1:9000/api/measures/component?component=jettoloader-basic&metricKeys=bugs" | python -m json.tool
#curl -s -X GET -u ${authString} \'${sonarProps['sonar.host.url']}/api/measures/component?" +  "metricKeys=coverage,duplicated_lines_density" + "&component=${sonarQubeProjectName}&branch=${branchName}'"
#project name
: '
指标
new_bugs
vulnerabilities
new_vulnerabilities
security_hotspots
new_security_hotspots
sqale_index
new_technical_debt
sqale_rating
new_maintainability_rating
reliability_rating
new_reliability_rating
security_rating
ncloc
new_lines
ncloc_language_distribution
projects
tests
new_security_rating
alert_status
quality_gate_details
duplicated_lines_density
new_duplicated_lines_density
duplicated_blocks
coverage
new_coverage
new_lines_to_cover
code_smells
new_code_smells
bugs
...
'
curl -s -X GET  -u admin:123456aA  "http://172.16.10.1:9000/api/projects/search" | python -m json.tool |  jq -r .components[].key
#project
curl -s -X GET  -H "token:$token"  "http://172.16.10.1:9000/api/projects/search" | python -m json.tool
#project metricKeys
curl -s -X GET  -H "token:$token"  "http://172.16.10.1:9000/api/measures/component?component=jettoloader-basic&branch=master&metricKeys=bugs,coverage,duplicated_lines_density,ncloc,complexity" | python -m json.tool
#search
curl -s -X GET  -H "token:$token"  "http://172.16.10.1:9000/api/projects/search?projects=jettoloader-basic" | python -m json.tool
#project status
curl -s -X GET  -u admin:123456aA  "http://172.16.10.1:9000/api/project_branches/list?project=jettoloader-basic" | python -m json.tool
#bugs
curl -s -X GET  -H "token:$token"  "http://172.16.10.1:9000/api/measures/component?component=jettoloader-basic&branch=master&metricKeys=bugs,vulnerabilities" | python -m json.tool | jq -r '.component.measures[] | .metric,.value' | sed -n "N;s/\n/ /p"

for i in $(curl -s -X GET  -u admin:123456aA  "http://172.16.10.1:9000/api/projects/search" | python -m json.tool |  jq -r .components[].key);do echo $i | tee -a log; curl -s -X GET -u admin:123456aA "http://172.16.10.1:9000/api/measures/component?component=$i&branch=master&metricKeys=bugs,vulnerabilities" | python -m json.tool | jq -r '.component.measures[] | .metric,.value' | sed -n "N;s/\n/ /p" | tee -a log ;done

python3:

依赖:

[root@localhost sonar]# pip3 install  --upgrade python-sonarqube-api scp

代码 

[root@localhost sonar]# cat wubo.py 
#!/bin/python3
# encoding: utf-8
from sonarqube import SonarQubeClient
from operator import itemgetter
import json
import csv
import os
import time
import shutil
class SonarQube:
    def __init__(self,url,username="admin",password="123456aA") -> None:
        username = username
        password = password
        sonarqube_url = url
        self.client = SonarQubeClient(username = username,password = password,sonarqube_url = sonarqube_url)
    def getProjects(self):
        """获取项目列表"""
        projects = self.client.projects.search_projects().get("components")
        return projects
    def getMessages(self,component):
        """ 获取项目各个参数数据"""
        #metricKeys = "alert_status,bugs,,vulnerabilities,security_rating,code_smells,duplicated_lines_density,coverage,ncloc"
        metricKeys = "bugs,,vulnerabilities"
        messages = []
        messages.append(self.client.measures.get_component_with_specified_measures(component, metricKeys))
        return messages[0]
    def getMeasures(self,component,message):
        measures = []
        measures.insert(0,component)
        measures_all = message.get("component").get("measures")
        for measure_item in measures_all:
            measures_type = measure_item.get("metric")
            if "bugs" == measures_type:
                measures.insert(1,measure_item.get("value"))
            if "vulnerabilities" == measures_type:
                measures.insert(2,measure_item.get("value"))
        return measures

class CSV:
    def __init__(self,filepath,filename) -> None:
        self.filepath = filepath
        self.filename = filename
    def csv_write(self,project_measure_all):
        #header = ['1project_name','2bugs','3vulnerabilities']
        with open(self.filepath+"/"+self.filename, 'a') as file_obj:
            writer = csv.writer(file_obj)
            #writer.writerow(header)
            for p in project_measure_all:
                writer.writerow(p)

    def csv_sort(self):
        datas=[]
        with open(self.filepath+"/"+self.filename, 'r') as f:
            table = []
            for line in f:
                line = line.replace("\n","").replace("\r","")
                col = line.split(',')
                col[0] = str(col[0])
                col[1] = col[1].strip("\n")
                table.append(col)
            table_sorted = sorted(table, key=itemgetter(0), reverse=False)  # 精确的按照第1列排序
            for row in table_sorted:
                datas.append(row)
        f.close()
        with open(self.filepath+"/"+self.filename,"w", newline='') as csvfile:
            writer = csv.writer(csvfile)
            for data in datas:
                writer.writerow(data)
        csvfile.close()

    def csv_insert(self):
       header = 'project_name,bugs,vulnerabilities'
       with open(self.filepath+"/"+self.filename, 'r+', encoding='utf-8') as f:
           content = f.read()
           f.seek(0, 0)
           f.write(header + '\n' + content)
       f.close()

    def csv_delete(self):
        if (os.path.exists(self.filepath)):
            shutil.rmtree(self.filepath,ignore_errors=True)
   
    def csv_sum(self):
        with open(self.filepath+"/"+self.filename) as fin:
            readline_item=fin.readline()
            total_bug_api = 0
            total_bug_manager = 0
            total_bug_loader = 0
            total_bug_ui = 0
            total_vulnerabilities_api = 0
            total_vulnerabilities_manager = 0
            total_vulnerabilities_loader = 0
            total_vulnerabilities_ui = 0
            for row in csv.reader(fin):
                row_project_name=row[0].split("-")[0]
                if "jettoapi" == row_project_name:
                     total_bug_api += int(row[1])
                     total_vulnerabilities_api += int(row[2])
                if "jettoloader" == row_project_name:
                     total_bug_loader += int(row[1])
                     total_vulnerabilities_loader += int(row[2])
                if "jettomanager" == row_project_name:
                     total_bug_manager += int(row[1])
                     total_vulnerabilities_manager += int(row[2])
                if "jettoui" == row_project_name:
                     total_bug_ui += int(row[1])
                     total_vulnerabilities_ui += int(row[2])
        fin.close()

        header_kong = ['','','']
        header_api = ['jettoapi','bug总数',str(total_bug_api),'vulnerabilities总数',str(total_vulnerabilities_api)]
        header_loader = ['jettoloader','bug总数',str(total_bug_loader),'vulnerabilities总数',str(total_vulnerabilities_loader)]
        header_manager = ['jettomanager','bug总数',str(total_bug_manager),'vulnerabilities总数',str(total_vulnerabilities_manager)]
        header_ui = ['jettoui','bug总数',str(total_bug_ui),'vulnerabilities总数',str(total_vulnerabilities_ui)]
        with open(self.filepath+"/"+self.filename, 'a') as file_obj:
            writer = csv.writer(file_obj)
            writer.writerow(header_kong)
            writer.writerow(header_api)
            writer.writerow(header_loader)
            writer.writerow(header_manager)
            writer.writerow(header_ui)
        file_obj.close()
                

class SCP: 
    def __init__(self,localdir,remoteip,remotedir) -> None:
        self.localdir = localdir
        self.remoteip = remoteip
        self.remotedir = remotedir
    def scp_operate(self):
        os.system('scp -r "%s" "%s:%s"' % (self.localdir, self.remoteip, self.remotedir))

def main():
    sonarQube = SonarQube(url='http://172.16.10.1:9000/')
    all_project_info = sonarQube.getProjects()
    project_measure_all=[]
    for project_info in all_project_info:
        component = project_info.get("key")
        message = sonarQube.getMessages(component)
        measure = sonarQube.getMeasures(component,message)
        project_measure_all.extend([tuple(measure)])
        print([tuple(measure)])

    filepath=time.strftime("%Y-%m-%d")
    filename="jettech_sornar_"+filepath+"_projects.csv"
    if not os.path.exists(filepath):
        os.makedirs(filepath)
    if  os.path.exists(filepath+"/"+filename):
        os.remove(filepath+"/"+filename)
    csv_obj = CSV(filepath=filepath,filename=filename)
    csv_obj.csv_write(project_measure_all)
    csv_obj.csv_sort()
    csv_obj.csv_insert()
    csv_obj.csv_sum()

    localdir=filepath
    remoteip="192.168.1.99"
    remotedir="/product/gohttpserver/data/sornar/" 
    scp_obj = SCP(localdir,remoteip,remotedir)
    scp_obj.scp_operate()
    csv_obj.csv_delete()

if __name__== "__main__" :
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值