項目越來越大称鳞,人員越來越多,代碼變動很難通知到每一個項目組成員稠鼻,尤其是一些核心配置的變動冈止,故此需要在GIT代碼庫服務(wù)端做控制,客戶端也可以候齿,但是需要每一個人都在本地進(jìn)行配置熙暴,麻煩亦沒必要
核心訴求:
1、限制成員提交代碼注釋長度
2慌盯、核心配置文件修改需要郵件通知到每一個開發(fā)人員
要解決第一個訴求用到了GIT的pre-receive鉤子周霉,這個鉤子會在客戶端發(fā)起push請求后調(diào)用執(zhí)行,只有執(zhí)行通過才能合并進(jìn)代碼庫亚皂,話不多說直接上代碼:
#!/bin/bash
#pre-receive script
#set -x #for debugging
validate_ref()
{
# --- Arguments
oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)
refname="$3"
commitList=`git rev-list $oldrev..$newrev`
#echo $commitList
split=($commitList)
for s in ${split[@]}
do
echo "@@@@@@@"
echo "$s"
msg=`git cat-file commit $s | sed '1,/^$/d'`
echo $msg
if [ ${#msg} -lt 15 ];then
echo "!!! Commit message length less than 15"
exit 1
else
echo "bigger than 5"
fi
done
}
fail=""
# Allow dual mode: run from the command line just like the update hook, or
# if no arguments are given then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
# Output to the terminal in command line mode - if someone wanted to
# resend an email; they could redirect the output to sendmail
# themselves
PAGER= validate_ref $2 $3 $1
else
while read oldrev newrev refname
do
validate_ref $oldrev $newrev $refname
done
fi
if [ -n "$fail" ]; then
exit $fail
fi
將代碼文件命名為pre-receive俱箱,并且可執(zhí)行權(quán)限設(shè)置為777(注意跨平臺文件格式問題)
在git服務(wù)器源代碼存儲的目錄(如abc.git),在abc.git目錄創(chuàng)建一個新目錄custom_hooks
將pre-receive放在custom_hooks目錄下灭必,不用重啟gitlab會立刻生效狞谱。
第二個訴求的實現(xiàn)要稍微復(fù)雜一些,因為涉及到發(fā)送郵件禁漓,需要外掛一個python腳本實現(xiàn)跟衅,這里用到了git的另一個鉤子post_receive,這個鉤子會在代碼入庫后執(zhí)行播歼,文件內(nèi)容很簡單:
#!/bin/sh
#一定要編寫這句話伶跷,清除GIT自己設(shè)置的根目錄,否則無法正常執(zhí)行下一句
#經(jīng)過實際測試荚恶,可能不需要此行撩穿,大家看自己情況加
#unset GIT_DIR
#用python2執(zhí)行py文件,就是cmd命令谒撼,需要指定py文件的絕對路徑食寡,python3需要多加一行,具體看代碼
python /data/hooks/send.py
下面是send.py的代碼:
# coding:utf-8
import smtplib as sm
from email.mime.text import MIMEText
from subprocess import check_output
#發(fā)送郵箱的smtp服務(wù)器
mail_host = 'smtpdm.aliyun.com'
#發(fā)送郵箱
sender = '77777777@qq.com'
#發(fā)送郵箱的smtp的授權(quán)碼(非郵箱的登錄密碼)
password = '12345678'
#接收者郵箱
receiver = '8888@qq.com,9999@qq.com'
#郵箱主題
subject = 'Code Update Notification'
#需要郵件提醒的文件
file1 = 'src/main/webapp/WEB-INF/web.xml'
file2 = 'src/main/resources/'
def Info():
'''
該函數(shù)主要用來獲取最近一次更新記錄,并提取主要信息:
'''
#使用subprocess的check_output函數(shù)來捕獲git命令后的
#標(biāo)準(zhǔn)輸出廓潜,以獲取最近一次更新記錄,該記錄是一個字符串
#log = check_output(['git','log','-l','-p'])
log = check_output(['git','show','--stat'])
#規(guī)避TypeError: a bytes-like object is required, not 'str'抵皱,byte轉(zhuǎn)str
log = str(log,encoding="utf-8")
#print(files)
if (file1 in log) or (file2 in log):
detail = log.split('\n\n')
#對字符串進(jìn)行分割善榛,以獲取相關(guān)信息author date comment
#更新者
#print(detail)
updater = detail[0].split('\n')[1].replace('Author','Updater')
#print(updater)
#更新時間,去掉最后的更新次數(shù)信息
update_time = detail[0].split('\n')[-1].replace('Date','Update time')[0:-6]
#注釋,并去掉左右兩邊空格
try:
comment = 'Comment: '+detail[1].strip()
except:
comment = 'Comment: '+"Sorry,the updater didn't comment."
#影響文件
files = 'Files:<br>'+detail[2].replace('\n','<br>')
#要比對文件需拿到第一步驟中的commitId使用git show commitId命令獲取
msg = """
<h3 style='color:red'>Hi,buddy!</h3>
<h3 style='color:red;text-indent:2em'>The resource file has been updated,the following is details:</h3>
<h3 style='color:red;text-indent:4em'>{0}</h3>
<h3 style='color:red;text-indent:4em'>{1}</h3>
<h3 style='color:red;text-indent:4em'>{2}</h3>
<h3 style='color:red;text-indent:4em'>{3}</h3>
<h3 style='color:red;text-indent:2em'>If any question,please contact the updater</h3>
""".format(updater,update_time,comment,files)
msg = MIMEText(msg,'html','utf-8')
send_email(msg)
def send_email(msg):
'''
發(fā)送郵件
'''
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = subject
smtpObj = sm.SMTP(mail_host)
smtpObj.login(sender,password)
smtpObj.sendmail(sender,receiver.split(','),msg.as_string())
smtpObj.quit()
if __name__ =='__main__':
Info()
是不是很簡單呻畸!看完記得點贊哦