百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

jenkins集成sonar扫描

toyiye 2024-06-22 20:30 10 浏览 0 评论

一、本地安装sonar scanner客户端

从官网下载合适版本进行安装 https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/

配置sonar-scanner.properties

#----- Default SonarQube server
sonar.host.url=http://172.18.37.214:9000

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=123456

二、jenkins配置sonar扫描

1、安装sonarqube scanner插件

2、配置扫描


3、项目根目录下新增扫描配置文件sonar-project.properties

sonar.sourceEncoding=UTF-8
sonar.projectKey=syncmanager
sonar.projectName=syncmanager
sonar.projectVersion=2.7.2P1
sonar.language=java
sonar.source=G:/jenkins/workspace/syncmanager/src/main/java/com
sonar.projectBaseDir=G:/jenkins/workspace/syncmanager/src/main/java/com
sonar.java.binaries=G:/jenkins/workspace/syncmanager/target/classes/com

三、sonar数据分析并发送邮件

1、安装python

从官网选择适合的安装包 https://www.python.org/downloads/windows/

2、安装psycopg2

3、部署python脚本sonar.py以及报告模板report-template.html

import psycopg2,os,sys
from jinja2 import FileSystemLoader,Environment

def select_project_uuid(project_name):
    db = psycopg2.connect(host="172.18.37.201", port=5432, user="postgres", password="123456", database="sonar")
    cursor = db.cursor()
    select_p_uuid="SELECT 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 = psycopg2.connect(host="172.18.37.201", port=5432, user="postgres", password="123456", database="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 = psycopg2.connect(host="172.18.37.201", port=5432, user="postgres", password="123456", database="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 = psycopg2.connect(host="172.18.37.201", port=5432, user="postgres", password="123456", database="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 = psycopg2.connect(host="172.18.37.201", port=5432, user="postgres", password="123456", database="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="report-templete.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]
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)
report_url="http://172.18.37.214:9000/dashboard?id=%s" %(projectKey)
generate_errmsg_table(s_lines,total_data,bugs,leaks,tastes,report_url)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="GBK">
<body>
<p style="font-weight:bold;">一、sonar扫描总体情况:</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>

4、jenkins中配置python环境变量

5、配置项目执行分析脚本

相关推荐

「2022 年」崔庆才 Python3 爬虫教程 - 代理的使用方法

前面我们介绍了多种请求库,如urllib、requests、Selenium、Playwright等用法,但是没有统一梳理代理的设置方法,本节我们来针对这些库来梳理下代理的设置方法。1.准备工作...

Python 3 基础教程 - 函数(python基础函数大全)

函数是一组有组织的、可重用的代码,用于执行单个相关操作。函数为应用程序提供更好的模块化和高度的代码重用。Python提供了许多内置函数,如print()等。也可以创建自己的函数。这些函数称为用户...

Python3.7.4图文安装教程(python3.7详细安装教程)

Python更新的很快,一转眼Python2已经过时了,本文为大家详细说明Python最新版本3.7.4的安装过程,跟着步骤一步一步操作,轻松搞定安装。没有软件可以关注我头条私信我1、下载好后是一个压...

非程序员的其他从业者,三天可入门Python编程,附教程与相应工具

这是一种应用十分广泛的编程语言Python,它打破了只有程序员才能编程的“戒律”,尤其是近年来国家予以Python编程的支持,让这门语言几乎应用到各种工作中。那么对于并不是职业程序员的人,该如何才能快...

008 - 匿名函数lambda-python3-cookbook中文教程

有名函数通过def来定义有一个有名字的函数。defmyfun():return1,2,3...

花了3万多买的python教程全套,现在分享给大家(python全栈)

花了3万多买的Python教程全套,现在分享给大家(Python全栈)文末惊喜记得看完哦。...

花来3万多买的python教程全套,现在分享给大家(python全栈)

花了3万多买的Python教程全套,现在分享给大家(Python全栈)文末惊喜记得看完哦。...

Python3最新版安装教程(Windows)(python3.7.0安装教程win10)

接下来给大家讲解一下python最新安装包的安装教程。·首先大家可以去这里搜索一下我的笔记,大概讲了一下,然后找到它的官网,下载的是windows,可以看一下最新的版本。·选择64位,点击下载就即可了...

笨办法学python3》再笨的人都能学会python,附PDF,拿走不谢

《笨办法学python3》这本书的最终目标是让你起步python编程,虽然说是用“笨办法”学习写程序,但是其实并不是这样的。所谓的“笨办法”就是指这本书的教学方式,也就是“指令式”的教学,在这个过程中...

python3 (1)(python312)

Python3Introduction:LearnthebasicsofPython3programming,withitskeyfeatures,andprovideyo...

Python3 教程-- 3、解释器(python3菜鸟教程官网)

Python3解释器Python解释器Linux/Unix的系统上,Python解释器通常被安装在/usr/local/bin/python3.4这样的有效路径(目录)里。我们可以将路径/us...

《笨办法学python3》再笨的人都能学会python,附PDF,拿走不谢

《笨办法学python3》这本书的最终目标是让你起步python编程,虽然说是用“笨办法”学习写程序,但是其实并不是这样的。所谓的“笨办法”就是指这本书的教学方式,也就是“指令式”的教学,在这个过程中...

入门经典!《Python 3程序开发指南》python学习教程赠送!

《Python3程序开发指南》(????)??嗨!你们的小可爱又来辣,小编自学python时用到的视频学习教程分享给大家~都是非常系统性、非常详细的教程哦,希望能帮助到你!转发文章+私信小编“资料”...

Python3.7最新安装教程,一看就会

一、博主自言随着人工智能的快速发展,python语言越来越受大家的欢迎,博主前段时间先自学了一次,这次再次巩固,顺便分享给大家我遇到的坑。帮助大家学习的时候少走弯路。希望会对大家有所帮助,欢迎留言...

# Python 3 # Python 3 教程(#python3.10教程)

Python3教程Python的3.0版本,常被称为Python3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python3....

取消回复欢迎 发表评论:

请填写验证码