前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jenkins +sonarqube 对后端代码静态扫描,钉钉群通知执行结果

jenkins +sonarqube 对后端代码静态扫描,钉钉群通知执行结果

作者头像
雷子
发布2021-03-15 15:16:35
9120
发布2021-03-15 15:16:35
举报
文章被收录于专栏:雷子说测试开发

代码提交,触发后端sonar测试,测试完成,jenkins触发依赖任务,执行python脚本,达到预期,调用上线任务模块,进行上线,达不到预期,钉钉群通知。

牵涉到配置:

1.配置sonar测试任务

https://www.cnblogs.com/leiziv5/p/9863211.html 参考

2,对配置测试任务进行调试,

构建失败触发邮件通知

2,配置一个新任务,执行python脚本

python脚本如下

代码语言:javascript
复制
import  requests,json,jenkins
  
def sendding(Dingtalk_access_token, content,title):
   url = Dingtalk_access_token
   pagrem = {        "msgtype": "link",        "link": {          
  'title':title,            "text": content,          
 'messageUrl':'http://localhost:9000/dashboard?id=testmvndemo'
       },        "isAtAll": True
   }
   headers = {        
 
'Content-Type': 'application/json'
   }
   requests.post(url, data=json.dumps(pagrem), headers=headers)
yanzhongbiaozhun=0

def huitui():
   huiguntongzhi='回退机器人url'
   url = 'http://localhost:9000/api/issues/search?componentKeys
=testmvndemo&s=FILE_LINE&resolved=false&types=BUG&ps=
100&facets=severities%2Ctypes&additionalFields=_all'
   resopnse = requests.get(url).text
   result = json.loads(resopnse)
   zhuyao=0
   yanzhong=0
   code_reslut=[]    

for item in result['issues']:        
     if item['severity']=='MAJOR':
           zhuyao+=1
           code_reslut.append({'文件':item['component'],
    "异常信息":item['message']})        
    elif item['severity']=='CRITICAL':
           yanzhong+=1
           code_reslut.append({'文件': item['component'], 
      "异常信息": item['message']})        
   else:            passif yanzhong>=yanzhongbiaozhun:
       sendresuly={'严重bug':yanzhong,'主要bug':zhuyao,
     "结果概览":code_reslut}
       sendding(huiguntongzhi,content=sendresuly,title="回滚通知")    

else:
       conde=jenkins.Jenkins(url="http://localhost:8080",
    username='liwanlei',password="liwanlei")
       conde.build_job("后端上线")

def xinxitongji():
   dingdingurl='钉钉url'
   url='http://localhost:9000/api/measures/search?projectKeys=
testmvndemo&metricKeys=alert_status%2Cbugs%2Creliability_rating%2
Cvulnerabilities%2Csecurity_rating%2Ccode_smells%2Csqale_rating%
2Cduplicated_lines_density%2Ccoverage%2Cncloc%2Cncloc_language_distribution'
   resopnse = requests.get(url).text
   result = json.loads(resopnse)
   bug=0
   code=0
   fugai=0
   chongf=0    
  
for item in result['measures']:        
  
       if item['metric']=="bugs":
           bug=item['value']       
       elif item['metric']=='code_smells':
           code=item['value']       
       elif item['metric']=='coverage':
           fugai=item['value']        

       elif item['metric']==['duplicated_lines_density']:
           chongf=item['value']        

       else:            pass
   code_reslut={'bug数量':bug,"可能存在问题代码":code,
    "覆盖率":fugai,"重复代码":chongf}
   sendding(dingdingurl, content=code_reslut, title="代码覆盖率统计")

if __name__=="__main__":
   huitui()
   xinxitongji()

选择:Build after other projects are built,选择依赖的项目,稳定后调用

构建:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python sonar.py

如果达到预期,脚本会自动调用第三个任务

代码调试过程,小心坑。

结果

钉钉通知:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-02-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 雷子说测试开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档