代码审核(Code Review)是软件研发质量保障机制中很是重要的一环,但在实际项目执行过程当中,却由于种种缘由被Delay甚至是忽略。在实践中,给你们推荐一款免费、开放源代码的代码审查软件Gerrit。html
Code Review最直观的解释即看代码。常规的作法为本身看,有时代码逻辑问题可能本身看不出来,须要找同事一块儿看,在你们知识体系相对平均的状况下可能须要花钱专门的公司帮助查看。java
Code Review须要看哪些?对于刚入职场或者刚接触到Coding的新人来讲,代码风格是比较重要的一块。除此以外,编码规范及代码结构写法,框架和工具的选型,具体项目的业务逻辑,安全隐患,性能问题等均可以经过review的方式发现。Code Review从前日后大体分为结对编程,提交代码后,测试以前,发版以前,发版以后等几个阶段,越日后,Code Review的效果越差,修复的成本也愈来愈高。linux
为何必定要作入库前Code Review?nginx
首先,代码审查的最大的功用是纯社会性的。若是你在编程,并且知道将会有同事检查你的代码,你编程态度就彻底不同了。你写出的代码将更加整洁,有更好的注释和程序结构。git
其次,偷懒是人的天性,从节约成本的角度考虑,你们通常会选择在测试以前无限制的Delay Code Review。入库前作Code Review即是成本和效果之间最佳平衡点,它能及时发现问题,进行修改后确保代码质量。web
最后,代码审查能传播知识。在不少开发团队里,常常每一个人负责一个核心模块,每一个人都只关注本身的模块。除非是同事的模块影响了本身的程序,他们从不相互交流。这种状况的后果是,每一个模块只有一我的熟悉里面的代码。若是这我的休假或辞职了,其余人则一筹莫展。经过代码审查,至少会有两我的熟悉这些程序——做者,以及审查者。审查者并不能像程序的做者同样对程序十分了解,但至少他会熟悉程序的设计和架构,这是极其重要的。shell
Gerrit是Google为Android系统研发量身定制的一套免费开源的代码审核系统,它在传统的源码管理协做流程中强制性引入代码审核机制,经过人工代码审核和自动化代码验证过程,将不符合要求的代码屏蔽在代码库以外,确保核心代码多人校验、多人互备和自动化构建核验。数据库
Gerrit以前的系统架构:apache
Gerrit以后的系统架构:编程
经过Gerrit机制将代码作分隔。
Gerrit适用性
几乎任何须要正式发布的项目都应当使用Gerrit来进行代码审查,若是Team中有新人,必须使用Gerrit确保代码质量。
使用过git的同窗,都知道,当咱们git add --> git commit --> git push
以后,你的代码会被直接提交到repo,也就是代码仓库中,就是图中橘红色箭头指示的那样。
那么gerrit就是上图中的那只鸟,普通成员的代码是被先push到gerrit服务器上,而后由代码审核人员,就是左上角的integrator在web页面进行代码的审核(review),能够单人审核,也能够邀请其余成员一同审核,当代码审核经过(approve)以后,此次代码才会被提交(submit)到代码仓库(repo)中去。
不管有新的代码提交待审核,代码审核经过或被拒绝,代码提交者(Contributor)和全部的相关代码审核人员(Integrator)都会收到邮件提醒。
gerrit还有自动测试的功能,和主线有冲突或者测试不经过的代码,是会被直接拒绝掉的,这个功能彷佛就是右下角那个老头(Jenkins)的任务。
整个流程就是这样。 在使用过程当中,有两点须要特别注意下:
提交者不能直接把代码推到远程的master主线(或者其余远程分支)上去。这样就至关于越过了gerrit了。 gerrit必须依赖于一个refs/for/*
的分支。
假如咱们远程只有一个master主线,那么只有当你的代码被提交到refs/for/master
分支时,gerrit才会知道,我收到了一个须要审核的代码推送,须要通知审核员来审核代码了。
当审核经过以后,gerrit会自动将这条分支合并到master主线上,而后邮件通知相关成员,master分支有更新,须要的成员再去pull就行了。并且这条refs/for/master
分支,是透明的,也就是说普通成员实际上是不须要知道这条线的,若是你正确配置了sourceTree,你也应该是看不到这条线的。
这两点很重要!!这两点很重要!!这两点很重要!!
①. Linux,Gerrit须要Linux环境,ubuntu 或者 centos,这里使用Ubuntu;
②. JDK,使用1.7版本就行
③. MySQL,其实这个非必须,Gerrit自带的有H2数据库
④. nginx/apache,做为认证和反向代理服务器;
⑤. Maven, 在安装的过程当中会下载一些jar文件;
⑥. Git,用来拉取代码
下载:jdk-7u79-linux-x64.tar.gz http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
安装:sudo tar zxvf ./jdk-7u79-linux-x64.tar.gz -C /opt
配置:vim ~/.bashrc(针对当前用户) or vim /etc/profile(针对全部用户,推荐)
export JAVA_HOME=/opt/jdk1.7.0_79 export JRE_HOME=$JAVA_HOME/jre export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
验证:
java -version java version "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
gerrit依赖,用来操做git repository
sudo apt-get install git
wget https://www.gerritcodereview.com/download/gerrit-2.12.4.war
sudo apt-get install apache2
验证:
service apache2 start
gerrit依赖,用来管理gerrit。
sudo adduser gerrit sudo passwd gerrit
并将gerrit加入sudo权限
sudo visudo
gerrit ALL=(ALL:ALL) ALL (为了测试方便,开最大权限)
默认安装:
java -jar gerrit-2.12.4.war init --batch -d ~/review_site
更新配置文件:sudo vim ~/review_site/etc/gerrit.config
[gerrit] basePath = git canonicalWebUrl = http://47.120.74.47:8081/ [database] type = h2 database = /home/gerrit/review_site/db/ReviewDB [index] type = LUCENE [auth] type = http [receive] enableSignedPush = false [sendemail] smtpServer = smtp.163.com smtpServerPort = 465 smtpEncryption = ssl smtpUser = avcd@163.com smtpPass = avcd123123123 sslVerify = false from = code review<avcd@163.com> [container] user = gerrit javaHome = /opt/jdk1.7.0_79/jre [sshd] listenAddress = *:29418 [httpd] listenUrl = http://*:8081/ [cache] directory = cache [http] proxy = http://47.120.74.47:8090 proxyUsername = gerrit1 #proxy user & password proxyPassword = 123456
a、若是apache目录结构以下:
apache2.conf conf-enabled magic mods-enabled ports.conf sites-enabled conf-available envvars mods-available sites-available
开启SSL、Proxy、Rewrite等模块:
cd /etc/apache2/mods-enabled ln -s ../mods-available/proxy.load ln -s ../mods-available/proxy.conf ln -s ../mods-available/proxy_http.load ln -s ../mods-available/proxy_balancer.conf ln -s ../mods-available/proxy_balancer.load ln -s ../mods-available/rewrite.load ln -s ../mods-available/ssl.conf ln -s ../mods-available/ssl.load ln -s ../mods-available/socache_shmcb.load # ln -s ../mods-available/slotmem_shm.load #
更新配置文件:sudo vim /etc/apache2/sites-enabled/gerrit-httpd.conf
ServerName 47.200.74.47 <VirtualHost *:8090> ProxyRequests Off ProxyVia Off ProxyPreserveHost On AllowEncodedSlashes On RewriteEngine On RewriteRule ^/(.*) http://47.200.74.47:8081/$1 [NE,P] <Proxy *> Order deny,allow Allow from all </Proxy> <Location /login/> AuthType Basic AuthName "Gerrit Code Review" Require valid-user AuthBasicProvider file AuthUserFile /etc/apache2/passwords </Location> ProxyPass / http://127.0.0.1:8081/ </VirtualHost>
b、若是apache目录结构以下:
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
开启SSL、Proxy、Rewrite等模块:
# Open LoadModule LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule ssl_module modules/mod_ssl.so LoadModule rewrite_module modules/mod_rewrite.so # Gerrit config Include conf/extra/gerrit-httpd.conf
其中apache2/conf/extra/gerrit-httpd.conf内容同上,apache2/sites-enabled/gerrit-httpd.conf。
touch /etc/apache2/passwords htpasswd -b /etc/apache2/passwords admin 123456(管理员) htpasswd -b /etc/apache2/passwords gerrit1 123456(普通用户)
sudo ~/review_site/bin/gerrit.sh start sudo /etc/init.d/apache2 start
前提:须要git使用端 / gerrit服务端配合使用。
ssh -p 29418 gerrit1@47.200.74.47 gerrit create-project --empty-commit --name demo-project
#建议采用管理界面添加
ssh -p 29418 gerrit1@192.168.199.112 gerrit create-project --name exist-project #建议采用管理界面添加
或者使用gerrit管理界面
而后将已有项目与gerrit上创建的exist-project关联,即将已有代码库代码push到gerrit中进行管理。
cd ~/exist-project git push ssh://gerrit1@47.200.74.47:29418/exist-project *:*
在开发帐户中生成sshkey,用做与gerrit服务器链接。
ssh-keygen -t rsa #生成sshkey
此步骤与git流程相似,即将id_rsa.pub内容上传到git repository,gerrit中帮咱们管理git repository.
验证sshkey是否配置成功:ssh gerrit1@47.200.74.47 -p 29418
**** Welcome to Gerrit Code Review **** Hi gerrit1, you have successfully connected over SSH. Unfortunately, interactive shells are disabled. To clone a hosted Git repository, use: git clone ssh://gerrit1@47.200.74.47:29418/REPOSITORY_NAME.git Connection to 47.200.74.47 closed.
拉取代码:
git clone ssh://gerrit1@47.200.74.47:29418/REPOSITORY_NAME.git
更新githooks:
gitdir=$(git rev-parse --git-dir); scp -p -P 29418 gerrit1@47.200.74.47:hooks/commit-msg ${gitdir}/hooks/
该过程用来在commit-msg中加入change-id,gerrit流程必备。
修改代码并提交,推送时与原有git流程不一致,采用 git push origin HEAD:refs/for/master 。
[root@iZ62j8121e5Z abcd]# git push origin HEAD:refs/for/master Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 710 bytes | 710.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Processing changes: new: 1, refs: 1, done remote: remote: New Changes: remote: http://47.200.74.47:8081/5 xxxx remote: To ssh://47.200.74.47:29418/abcd * [new branch] HEAD -> refs/for/master
若是不加会提示一下错误:
[root@iZ62j8121e5Z abcd]# git push origin HEAD:refs/for/master Counting objects: 3, done. Writing objects: 100% (3/3), 239 bytes | 239.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Processing changes: refs: 1, done remote: ERROR: missing Change-Id in commit message footer remote: remote: Hint: To automatically insert Change-Id, install the hook: remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 admin@47.200.74.47:hooks/commit-msg ${gitdir}/hooks/ remote: And then amend the commit: remote: git commit --amend remote: To ssh://47.200.74.47:29418/abcd ! [remote rejected] HEAD -> refs/for/master (missing Change-Id in commit message footer) error: failed to push some refs to 'ssh://admin@47.200.74.47:29418/abcd'
当完成push后,可在gerrit管理界面看到当前提交code review的change。
查看某次提交的详细信息(审核者+2可经过本次提交,提交者可经过Abandon本次提交):
若是审核者+2经过后,可提交该次commit.
须要为每一个使用者分配gerrit帐号,不要都使用admin帐号,由于admin帐号可直接push master
pull代码后须要配置githooks文件,以便在commit时自动生成change-id,不然没法push
push代码时须要使用git push origin HEAD:refs/for/master(branch),gerrit默认关闭非admin帐号的push direct权限
push代码时须要commit email与gerrit account email一致,不然没法push成功,可选择关闭email notify,并开启forge user权限,或者经过修改gerrit数据库account email信息
gerrit数据库与gitlab同步,须要安装replication插件,并开启该功能
参考连接:
Java SDK Download: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Gerrit Code Review - Releases Download: https://gerrit-releases.storage.googleapis.com/index.html
Gerrit Code Review - Quick get started guide: https://git.eclipse.org/r/Documentation/install-quick.html
Gerrit代码审核流程 http://www.worldhello.net/gotgit/images/gerrit-workflow.png
Gerrit代码审核原理 http://www.worldhello.net/gotgit/05-git-server/055-gerrit.html
Gerrit代码审核权限管理 https://gerrit-review.googlesource.com/Documentation/access-control.html#category_forge_committer
Gerrit修改数据库email信息 http://www.cnblogs.com/kevingrace/p/5624122.html
Gerrit安装replication插件 https://gerrit-review.googlesource.com/Documentation/cmd-plugin-install.html