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

整個過程一氣呵成,環(huán)環(huán)相扣。而其中可以被自動化的正好是第二步:請求他人進行code review的時候的反饋消息。
根據(jù)實踐的經(jīng)驗,比較好的內容格式如下(包含Markdown格式,因為跟蹤任務的系統(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的步驟的時候都需要寫類似的回復在任務管理系統(tǒng)中,所以考慮使用Python腳本去自動生成這段文字,簡化工作。
根據(jù)樣例回復進行分析,需要獲取項目的分支名(任務目標分支),項目最后一次提交的commit id去組裝第二行的git commit的鏈接,然后Details的內容可以從git log中的提交信息里面提取。
第一步:獲取分支名稱。
為了簡化過程,默認項目的當前分支就是我們需要的分支,那么問題簡化為獲取當前分支名。可以利用git的相關命令實現(xiàn),如下:
git branch | sed -n '/\* /s///p'
第二步:獲取commit id。
而獲取commit id也非常簡單,只需要如下命令:
git rev-parse HEAD
第三步:獲取提交信息。
還需要獲取提交信息,利用git log的命令進行過濾也能得到:
git log --pretty=format:"%s" -1
git log --pretty=format命令很強大,除了獲得提交信息外,還有如下參數(shù)可以使用。
%H 提交對象(commit)的完整哈希字串 %h 提交對象的簡短哈希字串 %T 樹對象(tree)的完整哈希字串 %t 樹對象的簡短哈希字串 %P 父對象(parent)的完整哈希字串 %p 父對象的簡短哈希字串 %an 作者(author)的名字 %ae 作者的電子郵件地址 %ad 作者修訂日期(可以用 -date= 選項定制格式) %ar 作者修訂日期,按多久以前的方式顯示 %cn 提交者(committer)的名字 %ce 提交者的電子郵件地址 %cd 提交日期 %cr 提交日期,按多久以前的方式顯示 %s 提交說明
所以第二步也可以使用git log命令實現(xiàn),如下所示:
git log --pretty=format:"%H" -1
當然還需要在后面加一點人性化的感謝的話,畢竟是麻煩其他人來對你代碼進行審核,說一些感謝的話吧,這里我就用一個list來裝一些感謝的話,然后隨機獲取一段貼到最后。
如果是以面向過程的方式去編寫,那么可以編寫如下代碼:
#coding=utf-8
#!/usr/bin/python
import os, subprocess
import random
# use subprocess to get the current branch name from output
def 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_name
def get_latest_git_log(cd_path):
"""
docstring
"""
os.chdir(cd_path)
status, log_info = subprocess.getstatusoutput("git log --pretty=format:\"%s\" -1")
return log_info
def get_latest_commit_id(cd_path):
os.chdir(cd_path)
status, commit_id = subprocess.getstatusoutput("git rev-parse HEAD")
return commit_id
def 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腳本和項目沒有放在一個目錄下面,所以每次在執(zhí)行git相關命令之前都需要先cd到目標項目目錄下。而分別執(zhí)行git命令的時候使用subprocess.getstatusoutput()來執(zhí)行,方便獲取標準化輸出的結果。這里之所以不使用os.system來執(zhí)行命令,是因為os.system運行命令的返回值里面包括兩個部分,第一部分是命令的結果輸出,第二部分是結果是否成功的標識符。
例如執(zhí)行os.system("git branch | sed -n '/* /s///p'")會返回如下內容:
feature/ST-247 0
第一行是我們獲取到的分支名,第二行是成功的標識符,0表示命令沒有任何問題。
所以我考慮使用subprocess.getstatusoutput來運行命令,這個函數(shù)會分別返回結果標識和輸出,方便得到想要的執(zhí)行輸出結果。
雖然代碼還可以進一步優(yōu)化,但是已經(jīng)能滿足我的需求了,運行這個腳本就能得到如下的輸出結果:
[~Harry] *Changes made has been committed to feature/ST-247* - https://git.xxxxx.com/someproject/subname/-/commit/d21033057677e6d49d9cea07c64c49e35529545dx *Details* - Remove some invalid logic Please check it if you have free time, thanks.
如果改寫成面向對象的方式會更好,調用更簡單,傳遞參數(shù)也更少,采用Python3語法編寫的代碼如下所示:
#coding=utf-8
#!/usr/bin/python
import os
import subprocess
import random
class 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列表可以在增加多一點,這樣隨機獲取之下重復的概率會更少。當然最后一段也可以自己每次diy,畢竟感謝要發(fā)自內心的最好。
這種簡化工作流的腳本本質是減少重復性勞動,特別是一天完成了很多個任務的時候。但是反思本身是無法被簡化的,不做工作的奴隸,而是工作的主人。
拋磚引玉,希望對自己和未來的自己也是一個還原鏡像。
Todo:
1.可以每天定時執(zhí)行這個腳本去生成回復消息。
2.通過腳本傳參來動態(tài)選擇需要被處理的項目目錄。在這個案例代碼中是hard code的,默認是選擇了autoWork這個項目。
3.還可以考慮接入語料庫(thanks words),這樣感謝的話永不重復,還能學點新單詞。:)
以上就是python實現(xiàn)代碼審查回復消息生成的詳細內容,更多關于python 回復消息生成的資料請關注腳本之家其它相關文章!
相關文章
python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼
今天小編就為大家分享一篇python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python自動化爬取天眼查數(shù)據(jù)的實現(xiàn)
本文將結合實例代碼,介紹Python自動化爬取天眼查數(shù)據(jù)的實現(xiàn),對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-06-06
利用Python+PyQt5實現(xiàn)簡易瀏覽器的實戰(zhàn)記錄
這篇文章主要給大家介紹了關于如何利用Python+PyQt5實現(xiàn)簡易瀏覽器的相關資料,Qt 的主要優(yōu)勢是可以開發(fā)跨平臺的圖形界面程序,基于 Qt 的應用能夠借助于各平臺的原生性在不同類的設備上運行,而無須修改任何代碼庫,需要的朋友可以參考下2021-07-07
Python 字符串處理特殊空格\xc2\xa0\t\n Non-breaking space
今天遇到一個問題,使用python的find函數(shù)尋找字符串中的第一個空格時沒有找到正確的位置,下面是解決方法,需要的朋友可以參考下2020-02-02
使用并行處理提升python?for循環(huán)速度的過程
Python?是一門功能強大的編程語言,但在處理大規(guī)模數(shù)據(jù)或復雜計算任務時,性能可能成為一個瓶頸,這篇文章主要介紹了使用并行處理提升python?for循環(huán)速度,需要的朋友可以參考下2023-06-06

