一、程式碼如下
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()