通过GraphQL找到最近的GitHub Actions工作流运行。

huangapple go评论66阅读模式
英文:

Find most recent github actions workflow runs via graphql

问题

我看到了 https://stackoverflow.com/questions/74054176/how-to-write-a-graphql-query-to-retrieve-all-the-workflows-runs-from-github/75455707#75455707

第一个答案 建议使用 REST API 获取问题中工作流的全局节点 ID,然后使用该 ID 进行 GraphQL 查询。我宁愿不混合使用两种 API 类型和多次请求来解决这个问题。

第二个答案 建议通过拉取请求来解决,但在这种情况下,我没有拉取请求,因为我是通过 API 针对特定分支启动工作流运行。

最终,我试图解决的问题是获取我通过 API 启动的工作流程的工作流 ID,但 GitHub 在响应中没有提供工作流 ID,并且正如你从这个 stackoverflow 帖子和答案中看到的,这涉及编写一个相当滑稽的算法。我认为我提出的问题如果有人知道如何使用 GitHub GraphQL API 这种方式,可能会显著简化解决方案,尽管也许我找不到它的原因是因为它不存在。我观察到 Repository GraphQL 对象似乎没有 workflow_runs 字段。

英文:

I see https://stackoverflow.com/questions/74054176/how-to-write-a-graphql-query-to-retrieve-all-the-workflows-runs-from-github/75455707#75455707.

The first answer suggests using the REST API to get the global node id for the workflow in question, and then using that to query graphql. I'd rather not mix and match two API flavors and multiple requests to solve the problem.

The second answer suggests going through pull requests, and I don't have a pull request in this case, since I'm kicking off workflow runs via the API against a specific branch.

Ultimately, the problem I'm trying to solve is getting the workflow id that I just kicked off via the API, but github doesn't provide the workflow id in the response, and, as you can see from this stackoverflow post and answers, it involves writing a pretty hilarious algorithm. I think the question I'm asking could simplify the solution significantly if anyone knows how to use the github graphql API in this way, though maybe the reason I can't find this is because it doesn't exist. I observe that the Repository graphql object doesn't seem to have a workflow_runs field.

答案1

得分: 1

以下是翻译好的部分:

bertrandmartel/github_action_dispatch_runid.py仍然是一个有效的选项,这里是相同的脚本,包括:

  • 注释(根据我对代码的理解)
  • 错误处理,用于检查响应状态代码并在出现问题时打印错误消息。
  • 脚本顶部的常量,用于GitHub令牌、所有者、仓库、分支和操作,使配置更容易。
  • 更新的退出代码,以使其更具有意义(1表示错误,0表示成功)。
import requests
import time
import sys

# 常量
TOKEN = 'YOUR_GITHUB_TOKEN'    # 您的GitHub令牌
OWNER = 'YOUR_GITHUB_OWNER'    # 您的GitHub用户名或组织名
REPO = 'YOUR_GITHUB_REPO'      # 您的GitHub存储库名称
BRANCH = 'YOUR_BRANCH'         # 添加分支名称
ACTION = 'YOUR_ACTION_NAME'    # 您的GitHub Actions工作流文件名称

# 触发GitHub Actions工作流
response = requests.post(
    f'https://api.github.com/repos/{OWNER}/{REPO}/actions/workflows/{ACTION}/dispatches',
    headers={
        'Authorization': f'token {TOKEN}',
        'Accept': 'application/vnd.github.v3+json',
        'Content-Type': 'application/json'
    },
    json={'ref': BRANCH}
)

# 检查工作流分发是否成功
if response.status_code != 204:
    print("无法触发工作流")
    sys.exit(1)

# 轮询直到创建工作流运行
while True:
    # 获取最新的工作流运行
    response = requests.get(
        f'https://api.github.com/repos/{OWNER}/{REPO}/actions/runs',
        headers={
            'Authorization': f'token {TOKEN}',
            'Accept': 'application/vnd.github.v3+json'
        }
    )

    # 检查是否成功获取工作流运行
    if response.status_code != 200:
        print("无法获取工作流运行")
        sys.exit(1)

    # 解析响应的JSON
    runs = response.json()['workflow_runs']

    # 检查最新的工作流运行是否适用于正确的分支和操作
    for run in runs:
        if run['head_branch'] == BRANCH and run['name'] == ACTION:
            print(f'工作流运行ID:{run["id"]}')
            sys.exit(0)

    # 在再次轮询之前休眠5秒
    time.sleep(5)
英文:

Since bertrandmartel/github_action_dispatch_runid.py remains a valid option, here is the same script with:

  • comments (from what I understand of the code)
  • error handling to check the response status codes and print an error message if something goes wrong.
  • constants at the top of the script for the GitHub token, owner, repo, branch, and action, making it easier to configure.
  • updated exit codes, to be more meaningful (1 for an error and 0 for success).
import requests
import time
import sys

# Constants
TOKEN = 'YOUR_GITHUB_TOKEN'    # Your GitHub token
OWNER = 'YOUR_GITHUB_OWNER'    # Your GitHub username or organization name
REPO = 'YOUR_GITHUB_REPO'      # Your GitHub repository name
BRANCH = 'YOUR_BRANCH'         # Add the branch name
ACTION = 'YOUR_ACTION_NAME'    # Your GitHub Actions workflow file name

# Trigger a GitHub Actions workflow
response = requests.post(
    f'https://api.github.com/repos/{OWNER}/{REPO}/actions/workflows/{ACTION}/dispatches',
    headers={
        'Authorization': f'token {TOKEN}',
        'Accept': 'application/vnd.github.v3+json',
        'Content-Type': 'application/json'
    },
    json={'ref': BRANCH}
)

# Check if the workflow dispatch was successful
if response.status_code != 204:
    print("Failed to trigger the workflow")
    sys.exit(1)

# Poll until the workflow run is created
while True:
    # Fetch the latest workflow runs
    response = requests.get(
        f'https://api.github.com/repos/{OWNER}/{REPO}/actions/runs',
        headers={
            'Authorization': f'token {TOKEN}',
            'Accept': 'application/vnd.github.v3+json'
        }
    )

    # Check if fetching workflow runs was successful
    if response.status_code != 200:
        print("Failed to fetch workflow runs")
        sys.exit(1)

    # Parse the response JSON
    runs = response.json()['workflow_runs']

    # Check if the latest workflow run is for the correct branch and action
    for run in runs:
        if run['head_branch'] == BRANCH and run['name'] == ACTION:
            print(f'Workflow run ID: {run["id"]}')
            sys.exit(0)

    # Sleep for 5 seconds before polling again
    time.sleep(5)

huangapple
  • 本文由 发表于 2023年6月12日 20:21:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定