#!/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)
1、执行环境 python 3.6
2、写入的模板html,table.html,如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<body>
<p style="font-weight:bold;">一、总体情况:</p>
<ul>
<li style="font-weight:bold;">整体运行情况:扫描代码行数:<span style="color:blue">{{lins}}</span>, bugs:<span style="color:red">{{total_data[0]}}</span>, 漏洞:<span style="color:red">{{total_data[1]}}</span>, 坏味道:<span style="color:red">{{total_data[2]}}</span></li>
<li style="font-weight:bold;">URL地址:<a style="font-weight:bold;" href={{report_url}} >{{report_url}}</a></li>
</ul>
<p style="font-weight:bold;">二、错误信息详情:</p>
<table border="1" cellpadding="10" width="540" height="120">
<tr ><th></th><th>阻断</th><th>严重</th><th>主要</th><th>次要</th><th>提示</th><th>总数</th></tr>
<tr bgcolor=#ECFFFF><td>bugs</td><td align="center">{{bugs[0]}}</td><td align="center">{{bugs[1]}}</td><td align="center">{{bugs[2]}}</td><td align="center">{{bugs[3]}}</td><td align="center">{{bugs[4]}}</td><td align="center" style="color:red">{{total_data[0]}}</td></tr>
<tr bgcolor=#D2E9FF><td>漏洞</td><td align="center">{{leaks[0]}}</td><td align="center">{{leaks[1]}}</td><td align="center">{{leaks[2]}}</td><td align="center">{{leaks[3]}}</td><td align="center">{{leaks[4]}}</td><td align="center" style="color:red">{{total_data[1]}}</td></tr>
<tr bgcolor=#ECFFFF><td>坏味道</td><td align="center">{{tastes[0]}}</td><td align="center">{{tastes[1]}}</td><td align="center">{{tastes[2]}}</td><td align="center">{{tastes[3]}}</td><td align="center">{{tastes[4]}}</td><td align="center" style="color:red">{{total_data[2]}}</td></tr>
</table>
<br></br>
</body>
</html>
3、执行命令
python3 sonar.py healthcode-sonar
healthcode-sonar是你jenkins中创建的项目名称