在個人這篇博文中:使用jenkins+sonar進行代碼掃描,併發送自定義郵件html
郵件的配置爲默認的$PROJECT_DEFAULT_SUBJECTjava
因此發送的郵件標題中的狀態是jenkins構建的狀態mysql
可是真正有意義的是sonar執行後的狀態,就是這個sql
因此須要將以前博文的sonar.py略做修改,以下json
# 新增引進requests和json庫 import pymysql,os,sys,requests,json from jinja2 import FileSystemLoader,Environment def select_project_uuid(project_name): db = pymysql.connect(host="192.168.207.160", 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.207.160", 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" 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.207.160", 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 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.207.160", 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 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.207.160", 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 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="",state=""): 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,state=state)) fh = open(report_html_path, 'w') fh.write(html_content) fh.close()
# 獲取sonar掃描結果狀態 def get_state(projectKey): r = requests.get('http://192.168.207.160:9000/api/qualitygates/project_status?projectId=%s' % (p_uuid) )
# 返回是一個json,因此反序列化r.text,因爲是Object格式的json,因此反序列化後是字典,使用get('key')方式取值 state=json.loads(r.text).get('projectStatus').get('status') return state # 寫入propfile.txt變量文件 def write_state(state): #設置文件對象 with open('propfile.txt','w') as f: #將字符串寫入文件中 f.write("state="+state) project_name = sys.argv[1] report_html_path="report\\"+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) state=get_state(p_uuid) write_state(state) report_url="http://192.168.207.160:9000/dashboard?id=%s" %(projectKey) generate_errmsg_table(s_lines,total_data,bugs,leaks,tastes,report_url,state)
而後新增步驟,將此文件中的鍵值對設置爲全局變量api
propfile.txtn內容就是變量名=變量值併發
修改subject主題app