python實(shí)現(xiàn)代碼審查自動(dòng)回復(fù)消息
在一個(gè)規(guī)范化的研發(fā)流程中,一般遵循如下流程:
開發(fā)階段:研發(fā)功能或者修復(fù)bug,在本地自測。 代碼審核階段:提交代碼,并請(qǐng)求團(tuán)隊(duì)內(nèi)人員做code review。 測試環(huán)境測試階段:部署到測試環(huán)境并請(qǐng)求測試。 發(fā)布線上待測階段:測試環(huán)境通過測試發(fā)布到線上進(jìn)行測試。 驗(yàn)收完成任務(wù):線上驗(yàn)證成功,關(guān)閉這個(gè)任務(wù)。實(shí)際上這只是一種最理想化的過程,因?yàn)槲覀兡J(rèn)每次狀態(tài)流轉(zhuǎn)都是順利的,開發(fā)沒有毛病,測試一次就通過,現(xiàn)實(shí)中的研發(fā)
流程的情況更復(fù)雜,如圖所示。

整個(gè)過程一氣呵成,環(huán)環(huán)相扣。而其中可以被自動(dòng)化的正好是第二步:請(qǐng)求他人進(jìn)行code review的時(shí)候的反饋消息。
根據(jù)實(shí)踐的經(jīng)驗(yàn),比較好的內(nèi)容格式如下(包含Markdown格式,因?yàn)楦櫲蝿?wù)的系統(tǒng)支持這種格式):
**Changes has been committed to feature/xxx-xxx**- https://git.xxx.com/xxxx/ddaf18f9be4613c31363d4c92b8bafc3sdfdsf**Details**Remove invalid logic for admin pannel
由于每次走到Code Review的步驟的時(shí)候都需要寫類似的回復(fù)在任務(wù)管理系統(tǒng)中,所以考慮使用Python腳本去自動(dòng)生成這段文字,簡化工作。
根據(jù)樣例回復(fù)進(jìn)行分析,需要獲取項(xiàng)目的分支名(任務(wù)目標(biāo)分支),項(xiàng)目最后一次提交的commit id去組裝第二行的git commit的鏈接,然后Details的內(nèi)容可以從git log中的提交信息里面提取。
第一步:獲取分支名稱。為了簡化過程,默認(rèn)項(xiàng)目的當(dāng)前分支就是我們需要的分支,那么問題簡化為獲取當(dāng)前分支名。可以利用git的相關(guān)命令實(shí)現(xiàn),如下:
git branch | sed -n ’/* /s///p’第二步:獲取commit id。
而獲取commit id也非常簡單,只需要如下命令:
git rev-parse HEAD第三步:獲取提交信息。
還需要獲取提交信息,利用git log的命令進(jìn)行過濾也能得到:
git log --pretty=format:'%s' -1
git log --pretty=format命令很強(qiáng)大,除了獲得提交信息外,還有如下參數(shù)可以使用。
%H 提交對(duì)象(commit)的完整哈希字串 %h 提交對(duì)象的簡短哈希字串 %T 樹對(duì)象(tree)的完整哈希字串 %t 樹對(duì)象的簡短哈希字串 %P 父對(duì)象(parent)的完整哈希字串 %p 父對(duì)象的簡短哈希字串 %an 作者(author)的名字 %ae 作者的電子郵件地址 %ad 作者修訂日期(可以用 -date= 選項(xiàng)定制格式) %ar 作者修訂日期,按多久以前的方式顯示 %cn 提交者(committer)的名字 %ce 提交者的電子郵件地址 %cd 提交日期 %cr 提交日期,按多久以前的方式顯示 %s 提交說明
所以第二步也可以使用git log命令實(shí)現(xiàn),如下所示:
git log --pretty=format:'%H' -1
當(dāng)然還需要在后面加一點(diǎn)人性化的感謝的話,畢竟是麻煩其他人來對(duì)你代碼進(jìn)行審核,說一些感謝的話吧,這里我就用一個(gè)list來裝一些感謝的話,然后隨機(jī)獲取一段貼到最后。如果是以面向過程的方式去編寫,那么可以編寫如下代碼:
#coding=utf-8#!/usr/bin/pythonimport os, subprocessimport random# use subprocess to get the current branch name from outputdef get_branch_name(cd_path): os.chdir(cd_path) status, branch_name = subprocess.getstatusoutput('git branch | sed -n ’/* /s///p’') # print(output) # exit(0) return branch_namedef get_latest_git_log(cd_path): ''' docstring ''' os.chdir(cd_path) status, log_info = subprocess.getstatusoutput('git log --pretty=format:'%s' -1') return log_infodef get_latest_commit_id(cd_path): os.chdir(cd_path) status, commit_id = subprocess.getstatusoutput('git rev-parse HEAD') return commit_iddef get_reviewer_by_random(reviewers): return random.choice(reviewers)def get_thanks_words_by_random(thanks_words): return random.choice(thanks_words)def create_comment(reviewers, branch_name, log_info, commit_id, thanks_words): print(get_reviewer_by_random(reviewers)) print('*Changes made has been committed to ' + branch_name + '*') print('- https://git.xxxxx.com/someproject/subname/-/commit/' + commit_id) print('*Details*') print('-' + log_info) print(get_thanks_words_by_random(thanks_words))branch_name = get_branch_name(’/Users/tony/www/autoWork’)log_info = get_latest_git_log(’/Users/tony/www/autoWork’)commit_id = get_latest_commit_id(’/Users/tony/www/autoWork’)reviewers = [ ’[~Harry]’, ’[~Tom]’]random_thanks_words = [ ’Review it please, thanks.’, ’Actually, I am glad to see you have time to review it, thanks a lot.’, ’Please check it if you have free time, thanks.’, ’Check it please.’ ’Waiting for your code review, thank you.’]create_comment(reviewers, branch_name, log_info, commit_id, random_thanks_words)
由于Python腳本和項(xiàng)目沒有放在一個(gè)目錄下面,所以每次在執(zhí)行g(shù)it相關(guān)命令之前都需要先cd到目標(biāo)項(xiàng)目目錄下。而分別執(zhí)行g(shù)it命令的時(shí)候使用subprocess.getstatusoutput()來執(zhí)行,方便獲取標(biāo)準(zhǔn)化輸出的結(jié)果。這里之所以不使用os.system來執(zhí)行命令,是因?yàn)閛s.system運(yùn)行命令的返回值里面包括兩個(gè)部分,第一部分是命令的結(jié)果輸出,第二部分是結(jié)果是否成功的標(biāo)識(shí)符。
例如執(zhí)行os.system('git branch | sed -n ’/* /s///p’')會(huì)返回如下內(nèi)容:
feature/ST-2470
第一行是我們獲取到的分支名,第二行是成功的標(biāo)識(shí)符,0表示命令沒有任何問題。
所以我考慮使用subprocess.getstatusoutput來運(yùn)行命令,這個(gè)函數(shù)會(huì)分別返回結(jié)果標(biāo)識(shí)和輸出,方便得到想要的執(zhí)行輸出結(jié)果。
雖然代碼還可以進(jìn)一步優(yōu)化,但是已經(jīng)能滿足我的需求了,運(yùn)行這個(gè)腳本就能得到如下的輸出結(jié)果:
[~Harry]*Changes made has been committed to feature/ST-247*- https://git.xxxxx.com/someproject/subname/-/commit/d21033057677e6d49d9cea07c64c49e35529545dx*Details*- Remove some invalid logicPlease check it if you have free time, thanks.
如果改寫成面向?qū)ο蟮姆绞綍?huì)更好,調(diào)用更簡單,傳遞參數(shù)也更少,采用Python3語法編寫的代碼如下所示:
#coding=utf-8#!/usr/bin/pythonimport osimport subprocessimport randomclass CommitComment: def __init__(self, project_path: str, reviewers: list, thanks_words: list): self.project_path = project_path self.reviewers = reviewers self.thanks_words = thanks_words # use subprocess to get the current branch name from output def get_branch_name(self) -> str: os.chdir(self.project_path) status, branch_name = subprocess.getstatusoutput('git branch | sed -n ’/* /s///p’') return branch_name # use subprocess to get the latest commit message from git log def get_latest_git_log(self) -> str: os.chdir(self.project_path) status, log_info = subprocess.getstatusoutput('git log --pretty=format:'%s' -1') return log_info # use subprocess to get the latest commit id from git log def get_latest_commit_id(self) -> str: os.chdir(self.project_path) status, commit_id = subprocess.getstatusoutput('git rev-parse HEAD') return commit_id def get_reviewer_by_random(self) -> str: return random.choice(self.reviewers) def get_thanks_words_by_random(self) -> str: return random.choice(self.thanks_words) def create_comment(self): print(self.get_reviewer_by_random()) print('*Changes has been committed to ' + self.get_branch_name() + '*') print('- https://git.xxxx.com/MyProject/ProjectName/-/commit/' + self.get_latest_commit_id()) print('*Details*') print('-' + self.get_latest_git_log()) print(self.get_thanks_words_by_random())thanks_words = [ ’Review it please, thanks.’, ’Actually, I am glad to see you have time to review it, thanks a lot.’, ’Please check it if you have free time, thanks.’, ’Check it please.’ ’Waiting for your code review, thank you.’ ]reviewers = [’[~Harry]’,’[~Tom]’]comment = CommitComment(’/Users/tony/www/autoWork’, reviewers, thanks_words)comment.create_comment() # will print out the complete comment
thanks_words列表可以在增加多一點(diǎn),這樣隨機(jī)獲取之下重復(fù)的概率會(huì)更少。當(dāng)然最后一段也可以自己每次diy,畢竟感謝要發(fā)自內(nèi)心的最好。
這種簡化工作流的腳本本質(zhì)是減少重復(fù)性勞動(dòng),特別是一天完成了很多個(gè)任務(wù)的時(shí)候。但是反思本身是無法被簡化的,不做工作的奴隸,而是工作的主人。拋磚引玉,希望對(duì)自己和未來的自己也是一個(gè)還原鏡像。
Todo:1.可以每天定時(shí)執(zhí)行這個(gè)腳本去生成回復(fù)消息。2.通過腳本傳參來動(dòng)態(tài)選擇需要被處理的項(xiàng)目目錄。在這個(gè)案例代碼中是hard code的,默認(rèn)是選擇了autoWork這個(gè)項(xiàng)目。3.還可以考慮接入語料庫(thanks words),這樣感謝的話永不重復(fù),還能學(xué)點(diǎn)新單詞。:)
以上就是python實(shí)現(xiàn)代碼審查回復(fù)消息生成的詳細(xì)內(nèi)容,更多關(guān)于python 回復(fù)消息生成的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. php模擬實(shí)現(xiàn)斗地主發(fā)牌2. Python random庫使用方法及異常處理方案3. 理解PHP5中static和const關(guān)鍵字4. spring acegi security 1.0.0 發(fā)布5. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)6. Vuex localStorage的具體使用7. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼8. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟9. vue 使用localstorage實(shí)現(xiàn)面包屑的操作10. MyBatis中的JdbcType映射使用詳解

網(wǎng)公網(wǎng)安備