当前位置:网站首页>Hi,你有一份Code Review攻略待查收!
Hi,你有一份Code Review攻略待查收!
2022-06-27 16:23:00 【InfoQ】
介绍
- 方法过长,把整个逻辑都写在一个方法里面,没有进行必要的抽象与封装。
- 方法没有必要注释,方法名还做不到望文生义。
- 文件打开以后,忘记关闭工作流。
组件介绍
- checkstyle官方文档
- SonarQube官方文档
整体使用流程

- Coder将自己的代码提交到代码仓库,发起Merge Request。
- 代码仓库调用建木钩子,触发建木流程。
- 建木从仓库拉取代码,进行checkstyle和SonarQube静态代码扫描。
- 建木将扫描结果,以评论的方式,写回到Merge Request的评论中。
- Reviewer开始工作,参考扫描结果,进行Code Review。
如何搭建
- 对项目本身进行改造,在项目中引入checkstyle和SonarQube插件,并完成配置工作。关于checkstyle和sonarqube的配置使用可以参考:checkstyle官方文档SonarQube官方文档
- 开始准备我们需要使用到的建木节点🤪
- git-clone节点(官方:https://jianmuhub.com/_/git_clone)

- maven节点(官方:https://jianmuhub.com/_/maven_build)

- check_style节点(自定义:https://jianmuhub.com/weizhongyuan/check_style)

- sonarqube-analysis节点(自定义:https://jianmuhub.com/fat_squirrel/sonarqube_analysis)

- git_merge_comment节点(自定义:https://jianmuhub.com/weizhongyuan/git_merge_comment)

- 企业微信通知节点(官方:https://jianmuhub.com/_/qywx_notice)

- 然后编写DSL文件
name: java-style-check
# 可并发执行
global:
concurrent: true
trigger:
type: webhook
param:
- name: action
type: STRING
exp: $.body.json.object_attributes.action
- name: object_kind
type: STRING
exp: $.body.json.object_kind
- name: user_name
type: STRING
exp: $.body.json.object_attributes.last_commit.author.name
- name: project_name
type: STRING
exp: $.body.json.project.name
- name: source_branch
type: STRING
exp: $.body.json.object_attributes.source_branch
- name: target_branch
type: STRING
exp: $.body.json.object_attributes.target_branch
- name: remote_url
type: STRING
exp: $.body.json.project.git_http_url
- name: git_host
type: STRING
exp: $.header.x-real-ip
- name: project_id
type: STRING
exp: $.body.json.project.id
- name: iid
type: STRING
exp: $.body.json.object_attributes.iid
# webhook事件 一个merge事件open、update、reopen都可以触发该流程
only: (${trigger.object_kind} == "merge_request" && ${trigger.action} == "open" || ${trigger.action} == "update" || ${trigger.action} == "reopen")
workflow:
start:
type: start
targets:
- git_source_branch
- git_target_branch
# 源分支
git_source_branch:
type: "git_clone:1.1.1"
sources:
- start
targets:
- maven1
param:
remote_url: "${trigger.remote_url}"
ref: "refs/heads/${trigger.source_branch}"
netrc_password: "((gitlab.password))"
netrc_machine: "x.x.x.x"
netrc_username: "((gitlab.username))"
# 通过mvn命令,使用代码规范检查工具check-style检查源分支,生成规范结果文件
# 通过mvn命令,使用代码缺陷检查工具sonarqube来检查代码,并且把结果上传到服务器
maven1:
type: "jianmu/maven:jdk"
sources:
- git_source_branch
targets:
- sonarqube_analysis
param:
mvn_action: "compile sonar:sonar clean checkstyle:checkstyle-aggregate"
workspace: "${git_source_branch.git_path}"
maven_public_id: "maven-releases"
maven_public_url: "http://x.x.x.x:8081/repository/maven-public/"
# 获取本次代码新增BUG和新增漏洞的数量,以及获取sonarqube的部署地址
sonarqube_analysis:
sources:
- maven1
targets:
- check_style
type: "fat_squirrel/sonarqube_analysis:v2"
param:
ip: "x.x.x.x:9000"
project_name: "name"
# 目标分支
git_target_branch:
type: "git_clone:1.2.2"
sources:
- start
targets:
- maven2
param:
remote_url: "${trigger.remote_url}"
ref: "refs/heads/${trigger.target_branch}"
password: "((gitlab.password))"
username: "((gitlab.username))"
# 使用代码规范检查工具check-style检查目标分支,生成规范结果文件
maven2:
type: "maven_build:1.3.1-jdk"
sources:
- git_target_branch
targets:
- check_style
param:
mvn_action: "checkstyle:checkstyle-aggregate"
workspace: "${git_target_branch.git_path}"
maven_public_id: "maven-releases"
maven_public_url: "http://172.16.35.15:8081/repository/maven-public/"
# 从共享目录中获取源分支和目标分支的规范结果文件进行比较
check_style:
sources:
- sonarqube_analysis
- maven2
targets:
- condition
type: "jianmu/check_style:v3"
param:
file2: "${git_target_branch.git_path}/target/checkstyle-result.xml"
file1: "${git_source_branch.git_path}/target/checkstyle-result.xml"
# 判断节点,走不同的分支
condition:
sources:
- check_style
type: condition
expression: ${check_style.result}=="1"
cases:
true: qywx_notice
false: condition2
qywx_notice:
type: "qywx_notice:1.2.1"
sources:
- condition
targets:
- git_merge_comment1
param:
mentioned_mobile_list: "[]"
bot_webhook_url: "((chatbot.merge))"
text_content: "项目:${trigger.project_name},提交人:${trigger.user_name},消息:本次提交增加了不符合规范数:${check_style.diff}"
msgtype: "text"
mentioned_list: "[]"
git_merge_comment1:
type: "jianmu/git_merge_comment:v1"
sources:
- qywx_notice
targets:
- end
param:
project_id: "${trigger.project_id}"
host: "${trigger.git_host}"
comment: "action:${trigger.action},项目:${trigger.project_name},提交人:${trigger.user_name},消息:本次合并新增${check_style.diff}个格式问题,新增${sonarqube_analysis.new_bugs}个bug风新增,新增${sonarqube_analysis.new_vulnerabilities}个漏洞问题,详情可见:'${sonarqube_analysis.url}'"
merge_request_iid: "${trigger.iid}"
token: "((AUTOOPS.private_token))"
condition2:
sources:
- condition
type: condition
expression: ${check_style.result}=="0"
cases:
true: qywx_notice2
false: qywx_notice3
qywx_notice2:
type: "qywx_notice:1.2.1"
sources:
- condition2
targets:
- git_merge_comment2
param:
mentioned_mobile_list: "[]"
bot_webhook_url: "((chatbot.merge))"
text_content: "项目:${trigger.project_name},提交人:${trigger.user_name},消息:本次提交不多不少,正正好好!"
msgtype: "text"
mentioned_list: "[]"
git_merge_comment2:
type: "jianmu/git_merge_comment:v1"
sources:
- qywx_notice2
targets:
- end
param:
project_id: "${trigger.project_id}"
host: "${trigger.git_host}"
comment: "action:${trigger.action},项目:${trigger.project_name},提交人:${trigger.user_name},消息:本次合并无新增格式问题,新增${sonarqube_analysis.new_bugs}个bug风新增,新增${sonarqube_analysis.new_vulnerabilities}个漏洞问题,详情可见:'${sonarqube_analysis.url}'"
merge_request_iid: "${trigger.iid}"
token: "((AUTOOPS.private_token))"
qywx_notice3:
type: "qywx_notice:1.2.1"
sources:
- condition2
targets:
- git_merge_comment3
param:
mentioned_mobile_list: "[]"
bot_webhook_url: "((chatbot.merge))"
text_content: "项目:${trigger.project_name},提交人:${trigger.user_name},消息:本次提交减少了不符合规范数:${check_style.diff}"
msgtype: "text"
mentioned_list: "[]"
git_merge_comment3:
type: "jianmu/git_merge_comment:v1"
sources:
- qywx_notice3
targets:
- end
param:
project_id: "${trigger.project_id}"
host: "${trigger.git_host}"
comment: "action:${trigger.action},项目:${trigger.project_name},提交人:${trigger.user_name},消息:本次合并减少${check_style.diff}个格式问题,新增${sonarqube_analysis.new_bugs}个bug风新增,新增${sonarqube_analysis.new_vulnerabilities}个漏洞问题,详情可见:'${sonarqube_analysis.url}'"
merge_request_iid: "${trigger.iid}"
token: "((AUTOOPS.private_token))"
end:
type: end
sources:
- git_merge_comment1
- git_merge_comment2
- git_merge_comment3

- 触发流程
- 结果展示

结尾
边栏推荐
- 【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新
- Galaxy Kirin V10 system activation
- Wanzhou gold industry: what are the differences between gold t+d investment and other investments?
- Anfulai embedded weekly report (issue 252): February 7, 2022 to February 13, 2022
- Explain in detail the differences between opentsdb and tdengine in system functions
- Openssf security plan: SBOM will drive software supply chain security
- Teach you how to realize pynq-z2 bar code recognition
- Row to column and column to row in MySQL
- 产学合作协同育人,麒麟软件携手南开大学合力完成《软件测试与维护》实践课程
- Space calculation of information and innovation industry in 2022
猜你喜欢
Control file related views
如何制作登录界面
Asemi rectifier bridge kbp310 function pin diagram
Teach you to use elastic search: run the first hello world search command
Teach you how to realize pynq-z2 bar code recognition
Lvgl8.x migrating to stm32f4
Asemi rectifier bridge kbp307 parameters, kbp307 details, kbp307 pictures
Application of scaleflux CSD 2000 in Ctrip
[leetcode] 2. Add two numbers (user-defined listnode), medium
Alibaba's mission, vision and core values
随机推荐
How to rewrite tdengine code from 0 to 1 with vscode in "technical class"
详解 OpenTSDB 与 TDengine 在系统功能层面上存在的差异
MFS distributed file system
Row to column and column to row in MySQL
SQL update批量更新
SQL update batch update
Overview of Inspur Yunxi database executor
Teach you how to realize pynq-z2 bar code recognition
Vscode suggests that you enable gopls. What exactly is it?
How much room does Migu video have for development without relying on sports events?
TDengine 连接器上线 Google Data Studio 应用商店
Wechat applet association search
Study on heritability and field experiment design
[UVM foundation] build of UVM_ Phase execution sequence
After the number length of Oracle exceeds 19, the entity uses long mapping. Why does this cause problems?
Advanced learning of MySQL -- Application -- view, stored procedure, trigger
TP5 restrict access frequency
阅文、中文在线等网文平台如何布局数字藏品?未来是否会推出“Read/Write-to-Earn”产品?
ansible环境安装及数据恢复
How to create a login interface