This commit is contained in:
NianBroken
2024-07-15 20:11:34 +08:00 Unverified
parent b691bbba34
commit 8f0201af30
+8 -10
View File
@@ -1,7 +1,6 @@
# 必要的依赖库
import requests
from datetime import datetime, timedelta, timezone
from concurrent.futures import ThreadPoolExecutor
import os
@@ -30,7 +29,7 @@ class GitHubActionsManager:
else: # 如果删除失败
print(f"Failed to delete run with ID {run_id}. Status code: {response.status_code}") # 打印错误信息
def delete_old_runs(self, max_workers=1):
def delete_old_runs(self):
next_page = self.runs_url # 初始化下一页的URL为第一页
while next_page: # 循环直到没有下一页
# 发送GET请求获取一页工作流运行记录
@@ -40,14 +39,13 @@ class GitHubActionsManager:
runs = data["workflow_runs"] # 获取运行记录列表
next_page = response.links.get("next", {}).get("url") # 获取下一页的URL
with ThreadPoolExecutor(max_workers=max_workers) as executor: # 创建线程池
for run in runs: # 遍历每条运行记录
# 将运行记录的创建时间转换为UTC时间
run_time = datetime.strptime(run["created_at"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
# 计算当前时间与运行记录创建时间的差值
time_difference = self.current_time - run_time
if time_difference > timedelta(hours=168): # 如果差值超过168小时
executor.submit(self.delete_run, run["id"]) # 提交删除运行记录的任务
for run in runs: # 遍历每条运行记录
# 将运行记录的创建时间转换为UTC时间
run_time = datetime.strptime(run["created_at"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
# 计算当前时间与运行记录创建时间的差值
time_difference = self.current_time - run_time
if time_difference > timedelta(hours=168): # 如果差值超过168小时
self.delete_run(run["id"]) # 删除运行记录
else: # 如果请求失败
print(f"Failed to fetch runs. Status code: {response.status_code}") # 打印错误信息
break # 退出循环