#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
#系统路径
#sys.path.append(r"/home/jenkins0406/jenkins/sonar_script")
sys.path.append(r"E:\sonar0829")
import pymysql,os,sys
from jinja2 import FileSystemLoader,Environment
def select_project_uuid(project_name):
db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")
cursor = db.cursor()
select_p_uuid="SELECT project_uuid,kee FROM projects WHERE `name`= '%s'" %(project_name)
cursor.execute(select_p_uuid)
result = cursor.fetchone()
p_uuid = result[0]
projectKey = result[1]
db.close()
return(p_uuid, projectKey)
def select_total_info(p_uuid):
total_info=[]
# 使用cursor()方法获取操作游标
db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")
cursor = db.cursor()
select_p_links = "SELECT text_value FROM project_measures WHERE text_value LIKE 'java=%' and component_uuid=" + "\'" + p_uuid + "\'"
cursor.execute(select_p_links)
p_links = cursor.fetchone()[0].split("=")[1]
sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =%s AND status != 'CLOSED'"
for leak in [2,3,1]:
search_data = sql_info %(p_uuid, leak)
cursor.execute(search_data)
total_info.append(cursor.fetchone()[0])
db.close()
return p_links,total_info
def select_bugs(p_uuid):
bugs=[]
db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")
cursor = db.cursor()
sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =2 AND status != 'CLOSED' AND severity ='%s'"
for leak in ['BLOCKER','CRITICAL',"MAJOR",'MINOR','INFO']:
search_data=sql_info % (p_uuid,leak)
cursor.execute(search_data)
bugs.append(cursor.fetchone()[0])
db.close()
return bugs
def select_leaks(p_uuid):
leaks=[]
db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")
cursor = db.cursor()
sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =3 AND status != 'CLOSED' AND severity ='%s'"
for leak in ['BLOCKER','CRITICAL',"MAJOR",'MINOR','INFO']:
search_data=sql_info % (p_uuid,leak)
cursor.execute(search_data)
leaks.append(cursor.fetchone()[0])
db.close()
return leaks
def select_bad_tastes(p_uuid):
tastes=[]
db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")
cursor = db.cursor()
sql_info="SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =1 AND status != 'CLOSED' AND severity ='%s'"
for leak in ['BLOCKER','CRITICAL',"MAJOR",'MINOR','INFO']:
search_data=sql_info % (p_uuid,leak)
cursor.execute(search_data)
tastes.append(cursor.fetchone()[0])
return tastes
db.close()
curpath = os.getcwd()
table_tem_name="table.html"
def generate_errmsg_table(s_lines="", total_data=[], bugs=[],leaks=[],tastes=[],report_url=""):
env = Environment(loader=FileSystemLoader(curpath, 'utf-8')) # 创建一个包加载器对象
template = env.get_template(table_tem_name)
html_content = (template.render(lins=s_lines,total_data=total_data, bugs=bugs,leaks = leaks,tastes=tastes,report_url=report_url))
fh = open(report_html_path, 'w')
fh.write(html_content)
fh.close()
#外部传参
#project_name = sys.argv[1]
project_name = 'healthcode-sonar'
report_html_path=project_name+".html"
p_uuid, projectKey=select_project_uuid(project_name)
s_lines,total_data=select_total_info(p_uuid)
bugs=select_bugs(p_uuid)
leaks=select_leaks(p_uuid)
tastes=select_bad_tastes(p_uuid)
report_url="http://192.168.90.198:9009/dashboard?id=%s" %(projectKey)
generate_errmsg_table(s_lines,total_data,bugs,leaks,tastes,report_url)
一、总体情况:
- 整体运行情况:扫描代码行数:{{lins}}, bugs:{{total_data[0]}}, 漏洞:{{total_data[1]}}, 坏味道:{{total_data[2]}}
- URL地址:{{report_url}}
二、错误信息详情:
阻断 严重 主要 次要 提示 总数
bugs {{bugs[0]}} {{bugs[1]}} {{bugs[2]}} {{bugs[3]}} {{bugs[4]}} {{total_data[0]}}
漏洞 {{leaks[0]}} {{leaks[1]}} {{leaks[2]}} {{leaks[3]}} {{leaks[4]}} {{total_data[1]}}
坏味道 {{tastes[0]}} {{tastes[1]}} {{tastes[2]}} {{tastes[3]}} {{tastes[4]}} {{total_data[2]}}
python3 sonar.py healthcode-sonar
healthcode-sonar是你jenkins中创建的项目名称