Python實現【監控遠端倉庫程式碼提交,如果有提交就自動執行需要監控測試的介面,以確保新提交不會對現有功能造成影響;監控有異常傳送釘釘通知】

梅梅不想踩坑發表於2024-06-06

一、程式碼如下

import git
from del_folder import del_folder
import time
from send_Dmessage import send_message
from send_gift import send_gift

 
# 設定遠端倉庫路徑
remote_url = 'xxx'
# 本地倉庫路徑
local_path = 'xxx' 

# webhook地址和金鑰
webhook_url = "xxx"
secret = "xxxf" # 金鑰(選填)

# 歷史提交列表
commit_list = []

def monitor_commits():
     
    while True:
        # 先刪除之前的資料夾
        folder_path = '/Users/mozili/Documents/xxx'
        del_folder(folder_path)
        
        # 克隆遠端倉庫到本地
        repo = git.Repo.clone_from(remote_url, local_path)
        
        # 切換到特定分支
        target_branch = "dev"
        repo.git.checkout(target_branch)
        
        try:
            # 檢視最新提交記錄
            commits = list(repo.iter_commits())[0]
            # 最新提交的加入commit_list
            if commits.hexsha not in commit_list:
                print(commits.hexsha, commits.author.name, commits.message)
                commit_list.append(commits.hexsha)
                content = send_gift()
                if content !='':
                    send_message(content,webhook_url,secret)
                else:
                    print('送禮無異常報警')
            else:
                print('沒有新提交!')
            
            # 每個5分鐘檢視一次是否有新提交
            time.sleep(300)
            
        except KeyboardInterrupt:
            break
        
if __name__== '__main__':  
    monitor_commits()

相關文章