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

如何通过GitHub API检查PR中必填GitHub Actions的状态?

如何通过GitHub API快速检查PR的所有必填检查状态(含GitHub Actions)

背景

  • 我有一个编号为12345的PR,目标分支是受保护的master分支
  • 该分支设置了两个合并必填检查:一个来自GitLab CI的gitlab/some-required-test,另一个来自GitHub Action的some-required-github-action
  • 除此之外,GitLab和GitHub上还有大量非必填任务,这些我并不关心
  • 我希望通过GitHub API仅检查该PR是否通过了所有必填检查。

现有尝试与问题

我需要专门确认GitHub Actions的必填检查状态,过程比预期复杂:

1. 获取GitLab检查状态

通过查询https://api.github.com/repos/OWNER/REPO/commits/REFNAME/status可以拿到GitLab的检查状态,但响应里没有GitHub Actions的信息:

{
  "state": "pending",
  "statuses": [
    {
        "state": "success",
        "context": "gitlab/check-1"
    },
    {
        "state": "pending",
        "context": "gitlab/check-2"
    }
  ]
  ...
}

2. 获取必填检查列表

通过分支保护端点可以正确拿到所有必填检查的列表,这一步没问题:

curl https://api.github.com/repos/ORG/REPO/branches/PROTECTED_BRANCH_NAME/protection/required_status_checks

响应示例:

{
    "url": "https://api.github.com/repos/ORG/REPO/branches/PROTECTED_BRANCH_NAME/protection/required_status_checks",
    "strict": false,
    "contexts": [
      "gitlab/some-required-test",
      "some-required-github-action"
    ],
    "contexts_url": "https://api.github.com/repos/ORG/REPO/branches/PROTECTED_BRANCH_NAME/protection/required_status_checks/contexts",
    "checks": [
      {
        "context": "gitlab/some-required-test",
        "app_id": null
      },
      {
        "context": "some-required-github-action",
        "app_id": 15368
      }
    ]
}

(注:app_id 15368对应GitHub Actions应用)

3. 检查GitHub Actions状态的繁琐流程

查询PR分支的check-suites:

curl https://api.github.com/repos/ORG/REPO/commits/PR_BRANCH/check-suites

响应里有大量check_suites数据,只能通过app.id/app.slug判断是GitHub Actions的任务,但无法直接确认是否是必填检查,也看不到具体任务名称:

{
  "total_count": 10,
  "check_suites": [{
      "id": 10271137035,
      "node_id": "CS_kwDOCf2-888AAAACZDUdKQ",
      "head_branch": "BRANCHNAME",
      "head_sha": "936b3031077ba7b71e0233fec23783ca0d3ed93e",
      "status": "completed",
      "conclusion": "success",
      "url": "https://api.github.com/repos/OWNER/REPO/check-suites/10271137065",
      "before": "8e579556a3909a0d25dbdd34e6ff0a9535653848",
      "after": "936b3031077ba7b71e0233fec23783ca0d3ed93e",
      "pull_requests": [...],
      "app": {
        "id": 15368,
        "slug": "github-actions",
        "node_id": "MDM6QXBwMTUzNjg=",
        "owner": {
          "login": "github",
          ...
        },
        "name": "GitHub Actions",
        "description": "Automate your workflow from idea to production",
        "external_url": "https://help.github.com/en/actions",
        "html_url": "https://github.com/apps/github-actions",
        "created_at": "2018-07-30T09:30:17Z",
        "updated_at": "2019-12-10T19:04:12Z",
        "permissions": {
          ...
        },
        "events": [
          ...
        ]
      },
      "created_at": "2023-01-09T21:36:29Z",
      "updated_at": "2023-01-09T21:38:16Z",
      "rerequestable": true,
      "runs_rerequestable": false,
      "latest_check_runs_count": 1,
      "check_runs_url": "https://api.github.com/repos/OWNER/REPO/check-suites/10271137035/check-runs",
      "head_commit": {
        "id": "936b3031077ba7b71e0233fec23783ca0d3ed93e",
        "tree_id": "4f341091f31a419915f6d8b70f42febdd115ee2f",
        "message": "COMMIT MESSAGE",
        "timestamp": "2023-01-09T21:36:12Z",
        "author": {...},
        "committer": {...}
      },
      "repository": {...}
  }, ...]
}

于是只能进一步查询每个GitHub Actions对应的check_runs_url:

curl https://api.github.com/repos/ORG/REPO/check-suites/10271137035/check-runs

通过返回的check_runs[i].name匹配必填检查列表,再查看status/conclusion判断是否通过:

{
  "total_count": 1,
  "check_runs": [
    {
      "id": 10536722916,
      "name": "some-required-github-action",
      "node_id": "CR_kwDOCf2-888AAAACdAmh5A",
      "head_sha": "936b3031077ba7b71e0233fec23783ca0d3ed93e",
      "external_id": "373218f8-38be-52de-27db-c87f3616be12",
      "url": "https://api.github.com/repos/ORG/REPO/check-runs/10536722916",
      "html_url": "https://github.com/ORG/REPO/actions/runs/3877963115/jobs/6613591600",
      "details_url": "https://github.com/ORG/REPO/actions/runs/3877963115/jobs/6613591600",
      "status": "completed",
      "conclusion": "success",
      "started_at": "2023-01-09T21:36:38Z",
      "completed_at": "2023-01-09T21:38:14Z",
      "output": {
        "title": null,
        "summary": null,
        "text": null,
        "annotations_count": 0,
        "annotations_url": "https://api.github.com/repos/ORG/REPO/check-runs/10536722916/annotations"
      }
    }
  ]
}

这种方法需要多次请求,流程非常繁琐,有没有更简单的方式?


内容的提问来源于stack exchange,提问作者jakub.g

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 10:16:23