亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python實現代碼審查自動回復消息

瀏覽:3日期:2022-06-28 16:15:36

在一個規范化的研發流程中,一般遵循如下流程:

開發階段:研發功能或者修復bug,在本地自測。 代碼審核階段:提交代碼,并請求團隊內人員做code review。 測試環境測試階段:部署到測試環境并請求測試。 發布線上待測階段:測試環境通過測試發布到線上進行測試。 驗收完成任務:線上驗證成功,關閉這個任務。

實際上這只是一種最理想化的過程,因為我們默認每次狀態流轉都是順利的,開發沒有毛病,測試一次就通過,現實中的研發

流程的情況更復雜,如圖所示。

python實現代碼審查自動回復消息

整個過程一氣呵成,環環相扣。而其中可以被自動化的正好是第二步:請求他人進行code review的時候的反饋消息。

根據實踐的經驗,比較好的內容格式如下(包含Markdown格式,因為跟蹤任務的系統支持這種格式):

**Changes has been committed to feature/xxx-xxx**- https://git.xxx.com/xxxx/ddaf18f9be4613c31363d4c92b8bafc3sdfdsf**Details**Remove invalid logic for admin pannel

由于每次走到Code Review的步驟的時候都需要寫類似的回復在任務管理系統中,所以考慮使用Python腳本去自動生成這段文字,簡化工作。

根據樣例回復進行分析,需要獲取項目的分支名(任務目標分支),項目最后一次提交的commit id去組裝第二行的git commit的鏈接,然后Details的內容可以從git log中的提交信息里面提取。

第一步:獲取分支名稱。

為了簡化過程,默認項目的當前分支就是我們需要的分支,那么問題簡化為獲取當前分支名。可以利用git的相關命令實現,如下:

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命令很強大,除了獲得提交信息外,還有如下參數可以使用。

%H 提交對象(commit)的完整哈希字串 %h 提交對象的簡短哈希字串 %T 樹對象(tree)的完整哈希字串 %t 樹對象的簡短哈希字串 %P 父對象(parent)的完整哈希字串 %p 父對象的簡短哈希字串 %an 作者(author)的名字 %ae 作者的電子郵件地址 %ad 作者修訂日期(可以用 -date= 選項定制格式) %ar 作者修訂日期,按多久以前的方式顯示 %cn 提交者(committer)的名字 %ce 提交者的電子郵件地址 %cd 提交日期 %cr 提交日期,按多久以前的方式顯示 %s 提交說明

所以第二步也可以使用git log命令實現,如下所示:

git log --pretty=format:'%H' -1

當然還需要在后面加一點人性化的感謝的話,畢竟是麻煩其他人來對你代碼進行審核,說一些感謝的話吧,這里我就用一個list來裝一些感謝的話,然后隨機獲取一段貼到最后。如果是以面向過程的方式去編寫,那么可以編寫如下代碼:

#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腳本和項目沒有放在一個目錄下面,所以每次在執行git相關命令之前都需要先cd到目標項目目錄下。而分別執行git命令的時候使用subprocess.getstatusoutput()來執行,方便獲取標準化輸出的結果。這里之所以不使用os.system來執行命令,是因為os.system運行命令的返回值里面包括兩個部分,第一部分是命令的結果輸出,第二部分是結果是否成功的標識符。

例如執行os.system('git branch | sed -n ’/* /s///p’')會返回如下內容:

feature/ST-2470

第一行是我們獲取到的分支名,第二行是成功的標識符,0表示命令沒有任何問題。

所以我考慮使用subprocess.getstatusoutput來運行命令,這個函數會分別返回結果標識和輸出,方便得到想要的執行輸出結果。

雖然代碼還可以進一步優化,但是已經能滿足我的需求了,運行這個腳本就能得到如下的輸出結果:

[~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.

如果改寫成面向對象的方式會更好,調用更簡單,傳遞參數也更少,采用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列表可以在增加多一點,這樣隨機獲取之下重復的概率會更少。當然最后一段也可以自己每次diy,畢竟感謝要發自內心的最好。

這種簡化工作流的腳本本質是減少重復性勞動,特別是一天完成了很多個任務的時候。但是反思本身是無法被簡化的,不做工作的奴隸,而是工作的主人。拋磚引玉,希望對自己和未來的自己也是一個還原鏡像。

Todo:

1.可以每天定時執行這個腳本去生成回復消息。2.通過腳本傳參來動態選擇需要被處理的項目目錄。在這個案例代碼中是hard code的,默認是選擇了autoWork這個項目。3.還可以考慮接入語料庫(thanks words),這樣感謝的話永不重復,還能學點新單詞。:)

以上就是python實現代碼審查回復消息生成的詳細內容,更多關于python 回復消息生成的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日本特黄特黄特刺激大片 | 国产黄色片在线看 | 中国精品久久精品三级 | 91亚洲国产成人久久精品网站 | 成人国产永久福利看片 | 99re最新地址| 91综合视频 | 国产一区二区三区在线 | 国产福利不卡视频在免费播放 | 亚洲一级在线观看 | 上海一级毛片 | 成年人网址在线观看 | 色婷婷久久 | 亚洲黄色片一级 | 手机国产精品一区二区 | 亚洲成人福利在线观看 | 台湾永久内衣秀130部vk | 免费观看一级特黄三大片视频 | 欧洲男女下面进出的视频 | 黄色成人影院 | 久久成年人 | 日韩在线中文字幕 | 全国男人的天堂网站 | 快射视频在线观看 | 亚洲欧洲精品一区二区三区 | 欧美毛片在线观看 | 亚洲你懂得 | 99久久久国产精品免费牛牛四川 | 色综合在 | 在线观看人成网站深夜免费 | 亚洲 欧美 国产另类首页 | 久久99热国产这有精品 | 欧美成人性色xxxx视频 | 久久激情五月丁香伊人 | 免费一级黄色片 | 色婷婷中文字幕在线一区天堂 | 欧美jizz8性欧美18 | 伊人影院久久 | 色牛网 | 日本免费人成网ww555在线 | 亚洲欧美中文字幕高清在线一 |