python调取sonarqube接口,统计扫描数据自动写到excel

import requests
import json
import openpyxl
def getkey():
    global data1
    url = "****"
    projectkey = requests.get(url)
    projectkey.raise_for_status()
    data = json.loads(projectkey.text)
    data1 = data['components'][:3]
    print('projectkey is ok')


def get_analsis(project_key):
    global data3
    url = "******"
    params = {'component': project_key, 'metricKeys': 'ncloc,bugs,vulnerabilities,duplicated_lines_density'}
    result = requests.get(url, params=params)
    result.raise_for_status()
    data2 = json.loads(result.text)
    data3 = data2['component']['measures']
    print(data3)


def inexcel(vr, num, kr, vrex):
    if vr == vrex:
        wb = openpyxl.load_workbook('C:\\Users\\X1\\Desktop\\test.xlsx')
        sheet = wb['spam']
        sheet[num] = kr
        wb.save('C:\\Users\\X1\\Desktop\\test.xlsx')


getkey()
for i in range(len(data1)):
    project_key = data1[i]['key']
    print(project_key + ' 扫描结果是:')
    get_analsis(project_key)
    ir1 = i + 2
    wb = openpyxl.load_workbook('C:\\Users\\X1\\Desktop\\test.xlsx')
    sheet = wb['spam']
    sheet['A' + str(ir1)] = project_key
    wb.save('C:\\Users\\X1\\Desktop\\test.xlsx')
    for ir in range(len(data3)):
        vr = data3[ir].get('metric', 'null')
        kr = data3[ir].get('value', 0)
        print(ir1)
        inexcel(vr, 'B' + str(ir1), kr, 'ncloc')
        inexcel(vr, 'C' + str(ir1), kr, 'vulnerabilities')
        inexcel(vr, 'D' + str(ir1), kr, 'bugs')
        inexcel(vr, 'E' + str(ir1), kr, 'duplicated_lines_density')

你可能感兴趣的:(python,开发语言)