对提交到gitlab的代码message进行验证以符合Angular规范

现在团队采用Angular代码规范,需要对提交到gitlab的代码进行验证,由于gitlab提供了hooks机制,所以做起来还是毕竟方便的,这里写一些我怎么做的

首先Angular代码规范参考:http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

gitlab的hook机制参考:https://docs.gitlab.com/ee/administration/server_hooks.html

也就是说我们可以在pre-receive、update和post-receive这3个阶段来对commit message进行验证,我之前写的的在update中验证的,你可以选你熟悉的脚本语言来写。在gitlab调用update脚本的时候会传入更新的2个commit的hash ID,然后在这里可以调用git来解析出有哪些commit需要验证(你最好找个别人写好的解析git commit message的库),如果验证通过就返回0,否则返回非0,标准输出可以在git push的命令行中看到。需要注意的时候当push的为tag或者新的分支的时候,hash ID可能为“0000000000000000000000000000000000000000”,还有就是“Merge branch”的需要考虑边界情况,大部分都是内容处理没啥可说的。如果你们还有别的要求,可以自己加。

Angular规范正则可以写成:

//匹配Angular规范关键字
$res = preg_match("#^\s*(feat|fix|docs|style|refactor|test|chore)(\(.+\))?\:.+#", $message, $matches);
if(!$res){
$msg = "your commit message format is wrong, see:'http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html', message:{$message}";
if($NEED_VERIFY){
log_file($msg, true);
exit(1);
}else{
log_file($msg);
}
}

最后就是,在某些情况下我们需要skip掉验证,但是hook没有提供额外的参数,这里只有手动去改了,搜了一下gitlab用了git push -o的参数,在pre-receive下,加上这行:

refs = $stdin.read

require_relative '../lib/hooks_utils'
push_options = HooksUtils.get_push_options
if push_options[0]=="skip"
aFile = File.new("/tmp/gitlab-skip", "w")
if aFile
content = aFile.syswrite(refs)
else
puts "Unable to open /tmp/gitlab-skip"
end
end

然后在update脚本里检查的时候去/tmp/gitlab-skip读取commit的hash,看相等就跳过验证

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇