Python实现【监控远程仓库代码提交,如果有提交就自动执行需要监控测试的接口,以确保新提交不会对现有功能造成影响;监控有异常发送钉钉通知】

一、代码如下

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()

 

posted @ 2024-06-06 12:05  梅梅不想踩坑  阅读(9)  评论(0编辑  收藏  举报