python

超轻量级php框架startmvc

Python操作Sonqube API获取检测结果并打印过程解析

更新时间:2020-08-10 16:54:01 作者:startmvc
这篇文章主要介绍了Python操作SonqubeAPI获取检测结果并打印过程解析,文中通过示例代码介绍

这篇文章主要介绍了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://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'
 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://www.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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Python Sonqube API 获取 结果 打印