java 单机接口限流处理方案
331
2022-06-06
这篇文章主要介绍了Python操作Sonqube API获取检测结果并打印过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.需求:每次Sonqube检查完毕后,需要登陆才能看到结果无法通过Jenkins发布后直接看到bug 及漏洞数量。
2.demo:发布后,可以将该项目的检测结果简单打印出来显示,后面还可以集成钉钉发送到群里。
# -*- coding: UTF-8 -*- import sys reload(sys) sys.setdefaultencoding('utf8') ''' @author:jmmei @file: SonarQubeDingtalk.py @time: 2019/7 ''' import requests,json,jenkins,os,time,datetime #通过jenkins变量JOB_NAME传入第一个参数projectName projectName=sys.argv[1] def notification(projectName): # sonar API sonar_Url = 'http://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' resopnse = requests.get(sonar_Url).text result = json.loads(resopnse) bug = 0 leak = 0 code_smell = 0 coverage = 0 density = 0 status = '' statusStr = '' for item in result['measures']: if item['metric']=="bugs": bug = item['value'] elif item['metric']=="vulnerabilities": leak = item['value'] elif item['metric']=='code_smells': code_smell = item['value'] elif item['metric']=='coverage': coverage = item['value'] elif item['metric']=='duplicated_lines_density': density = item['value'] elif item['metric']=='alert_status': status = item['value'] else: pass if status == 'ERROR': messageUrl = 'http://iconsdb.com/icons/preview/soylent-red/x-mark-3-xxl.png' statusStr = '失败' elif status == 'OK': statusStr = '成功' messageUrl = 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png' code_reslut= "Bug数:" + bug + "个," + \ "漏洞数:" + leak + "个," + \ "可能存在问题代码:"+ code_smell + "行," + \ "覆盖率:" + coverage + "%," + \ "重复率:" + density + "%" print("静态代码扫描统计:"+"状态:"+ status +","+code_reslut) if int(bug)>=3: print("bug 数量太多,请尽快修复再发布项目!") sys.exit(1) else: print("代码质量非常好") if __name__=="__main__": #sonarQube刷新结果 #time.sleep(10) notification(projectName)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~