You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改GitHub Actions API调用获取近30天运行列表?

正确获取过去30天GitHub Actions运行记录的API调用方法

你直接把Python代码片段datetime.today() - timedelta(days=30)作为参数值传递是错误的,GitHub API(包括你的内部部署版本)要求时间参数必须是ISO 8601格式的字符串,且需要用范围运算符明确过滤逻辑。

具体实现步骤:

  1. 生成符合要求的时间字符串
    先计算30天前的UTC时间,格式化为ISO 8601标准格式(例如2024-05-20T00:00:00Z),用UTC时间可以避免时区偏差问题。
    用Python生成的示例代码:

    from datetime import datetime, timedelta, timezone
    
    # 计算30天前的UTC时间
    thirty_days_ago = datetime.now(timezone.utc) - timedelta(days=30)
    # 转换为GitHub API要求的ISO 8601格式(带Z后缀表示UTC)
    formatted_start_date = thirty_days_ago.isoformat().replace("+00:00", "Z")
    
  2. 构造正确的API请求URL
    使用created>=的形式指定时间范围,你的内部API端点应构造为:

    https://github.infra.cloudera.com/api/v3/repos/[OWNER]/[REPO]/actions/runs?created=>={formatted_start_date}
    

    替换[OWNER]、[REPO]和{formatted_start_date}为实际值即可。如果需要同时限定结束时间(比如到当前UTC时间),可以追加参数:

    https://github.infra.cloudera.com/api/v3/repos/[OWNER]/[REPO]/actions/runs?created=>={formatted_start_date}&created<={formatted_end_date}
    
  3. 其他注意事项

    • 必须携带有效的认证信息(如个人访问令牌),令牌需要具备repo或actions权限,否则会返回权限错误。
    • GitHub API的created参数支持>=、<=、>、<四种运算符,可根据需求灵活组合时间范围。

内容的提问来源于stack exchange,提问作者Swathi DA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 03:52:10