Python 操作Sonqube API 获取检测结果并打印

Posted Jame-mei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 操作Sonqube API 获取检测结果并打印相关的知识,希望对你有一定的参考价值。

1.需求:每次Sonqube检查完毕后,需要登陆才能看到结果无法通过Jenkins发布后直接看到bug 及漏洞数量。

2.demo:发布后,可以将该项目的检测结果简单打印出来显示,后面还可以集成钉钉发送到群里。

 

 1 # -*- coding: UTF-8 -*-
 2 import sys
 3 reload(sys)
 4 sys.setdefaultencoding(utf8)
 5 
 6 ‘‘‘
 7 @author:jmmei
 8 @file: SonarQubeDingtalk.py
 9 @time: 2019/7
10 ‘‘‘
11 import  requests,json,jenkins,os,time,datetime
12 
13 
14 
15 
16 #通过jenkins变量JOB_NAME传入第一个参数projectName
17 
18 projectName=sys.argv[1]
19 
20 def notification(projectName):
21     # sonar API
22     sonar_Url = http://www.baidu.com:9000/sonar/api/measures/search?projectKeys=+ projectName +&metricKeys=alert_status%2Cbugs%2Creliability_rating%2Cvulnerabilities%2Csecurity_rating%2Ccode_smells%2Csqale_rating%2Cduplicated_lines_density%2Ccoverage%2Cncloc%2Cncloc_language_distribution
23     resopnse = requests.get(sonar_Url).text
24     result = json.loads(resopnse)
25     bug = 0
26     leak = 0
27     code_smell = 0
28     coverage = 0
29     density = 0
30     status = ‘‘
31     statusStr = ‘‘
32 
33     for item in result[measures]:
34         if item[metric]=="bugs":
35             bug = item[value]
36         elif item[metric]=="vulnerabilities":
37             leak = item[value]
38         elif item[metric]==code_smells:
39             code_smell = item[value]
40         elif item[metric]==coverage:
41             coverage = item[value]
42         elif item[metric]==duplicated_lines_density:
43             density = item[value]
44         elif item[metric]==alert_status:
45             status = item[value]
46         else:
47             pass
48 
49     if status == ERROR:
50         messageUrl = http://www.iconsdb.com/icons/preview/soylent-red/x-mark-3-xxl.png
51         statusStr = 失败
52     elif status == OK:
53         statusStr = 成功
54         messageUrl = http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png
55 
56     code_reslut=  "Bug数:" + bug + "个," + 57                   "漏洞数:" + leak + "个," + 58                   "可能存在问题代码:"+ code_smell + "行," + 59                   "覆盖率:" + coverage + "%," + 60                   "重复率:" + density + "%"
61     print("静态代码扫描统计:"+"状态:"+ status +","+code_reslut)
62     if int(bug)>=3:
63         print("bug 数量太多,请尽快修复再发布项目!")
64         sys.exit(1)
65     else:
66         print("代码质量非常好")
67     
68 
69    
70 
71 if __name__=="__main__":
72     #sonarQube刷新结果
73     #time.sleep(10)
74     notification(projectName)
75     

 

以上是关于Python 操作Sonqube API 获取检测结果并打印的主要内容,如果未能解决你的问题,请参考以下文章

Python调用华为API实现人脸检测

Python调用华为API实现人脸检测

Python调用华为API实现人脸检测

GCP AI Platform API - 类级别的对象检测指标 (Python)

Python1双系统安装/深度学习环境,目标检测,csv/excel/matplotlib,进程,文件/xml操作,百度人脸API,hal/aiohttp/restful/curl

Tensorflow 对象检测 Api M1 Macbook 冲突错误