码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-11-26 17:34:32      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:for   main   print   sts   alert   utf8   job   not   获取   

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 获取检测结果并打印

标签:for   main   print   sts   alert   utf8   job   not   获取   

原文地址:https://www.cnblogs.com/Jame-mei/p/11936934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!