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

这篇文章给大家分享的是有关Python操作Sonqube API如何获取检测结果并打印的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

python的五大特点是什么

python的五大特点:1.简单易学,开发程序时,专注的是解决问题,而不是搞明白语言本身。2.面向对象,与其他主要的语言如C++和Java相比, Python以一种非常强大又简单的方式实现面向对象编程。3.可移植性,Python程序无需修改就可以在各种平台上运行。4.解释性,Python语言写的程序不需要编译成二进制代码,可以直接从源代码运行程序。5.开源,Python是 FLOSS(*/开放源码软件)之一。

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 = 'https://cache..com/upload/information/20200622/113/18428.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如何获取检测结果并打印”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!