主頁 > 知識庫 > python實(shí)現(xiàn)代碼審查自動回復(fù)消息

python實(shí)現(xiàn)代碼審查自動回復(fù)消息

熱門標(biāo)簽:Win7旗艦版 語音系統(tǒng) 企業(yè)做大做強(qiáng) 硅谷的囚徒呼叫中心 客戶服務(wù) 電話運(yùn)營中心 百度AI接口 呼叫中心市場需求

在一個規(guī)范化的研發(fā)流程中,一般遵循如下流程:

  1. 開發(fā)階段:研發(fā)功能或者修復(fù)bug,在本地自測。
  2. 代碼審核階段:提交代碼,并請求團(tuán)隊(duì)內(nèi)人員做code review。
  3. 測試環(huán)境測試階段:部署到測試環(huán)境并請求測試。
  4. 發(fā)布線上待測階段:測試環(huán)境通過測試發(fā)布到線上進(jìn)行測試。
  5. 驗(yàn)收完成任務(wù):線上驗(yàn)證成功,關(guān)閉這個任務(wù)。

實(shí)際上這只是一種最理想化的過程,因?yàn)槲覀兡J(rèn)每次狀態(tài)流轉(zhuǎn)都是順利的,開發(fā)沒有毛病,測試一次就通過,現(xiàn)實(shí)中的研發(fā)

流程的情況更復(fù)雜,如圖所示。

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

根據(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的步驟的時候都需要寫類似的回復(fù)在任務(wù)管理系統(tǒng)中,所以考慮使用Python腳本去自動生成這段文字,簡化工作。

根據(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)前分支名??梢岳胓it的相關(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 提交對象(commit)的完整哈希字串 
%h 提交對象的簡短哈希字串 
%T 樹對象(tree)的完整哈希字串 
%t 樹對象的簡短哈希字串 
%P 父對象(parent)的完整哈希字串 
%p 父對象的簡短哈希字串 
%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)人性化的感謝的話,畢竟是麻煩其他人來對你代碼進(jìn)行審核,說一些感謝的話吧,這里我就用一個list來裝一些感謝的話,然后隨機(jī)獲取一段貼到最后。
如果是以面向過程的方式去編寫,那么可以編寫如下代碼:

#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腳本和項(xiàng)目沒有放在一個目錄下面,所以每次在執(zhí)行g(shù)it相關(guān)命令之前都需要先cd到目標(biāo)項(xiàng)目目錄下。而分別執(zhí)行g(shù)it命令的時候使用subprocess.getstatusoutput()來執(zhí)行,方便獲取標(biāo)準(zhǔn)化輸出的結(jié)果。這里之所以不使用os.system來執(zhí)行命令,是因?yàn)閛s.system運(yùn)行命令的返回值里面包括兩個部分,第一部分是命令的結(jié)果輸出,第二部分是結(jié)果是否成功的標(biāo)識符。

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

feature/ST-247
0

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

所以我考慮使用subprocess.getstatusoutput來運(yùn)行命令,這個函數(shù)會分別返回結(jié)果標(biāo)識和輸出,方便得到想要的執(zhí)行輸出結(jié)果。

雖然代碼還可以進(jìn)一步優(yōu)化,但是已經(jīng)能滿足我的需求了,運(yùn)行這個腳本就能得到如下的輸出結(jié)果:

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

如果改寫成面向?qū)ο蟮姆绞綍?,調(diào)用更簡單,傳遞參數(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列表可以在增加多一點(diǎn),這樣隨機(jī)獲取之下重復(fù)的概率會更少。當(dāng)然最后一段也可以自己每次diy,畢竟感謝要發(fā)自內(nèi)心的最好。

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

Todo:

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

以上就是python實(shí)現(xiàn)代碼審查回復(fù)消息生成的詳細(xì)內(nèi)容,更多關(guān)于python 回復(fù)消息生成的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 使用Python建立RNN實(shí)現(xiàn)二進(jìn)制加法的示例代碼
  • Python解析m3u8拼接下載mp4視頻文件的示例代碼
  • Python實(shí)現(xiàn)我的世界小游戲源代碼
  • Python爬取你好李煥英豆瓣短評生成詞云的示例代碼
  • 七種Python代碼審查工具推薦

標(biāo)簽:山西 海南 山西 喀什 長沙 崇左 安康 濟(jì)南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python實(shí)現(xiàn)代碼審查自動回復(fù)消息》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266