2

I see How to write a GraphQL query to retrieve all the workflows/runs from github.

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.

burnettk
  • 13,557
  • 4
  • 51
  • 52
  • 1
    [This answer](https://stackoverflow.com/a/73837663/6309) remains your best approach, but might also be the "pretty hilarious algorithm" you alluded to in your question. – VonC Jun 14 '23 at 16:05
  • Yeah, that is the pretty hilarious algorithm i was referring to. the bash version has some bugs, so i'd go with the python version as a model if you were trying to implement this yourself. – burnettk Jun 19 '23 at 14:37

1 Answers1

1

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)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • i think the relative quiet here proves that the API I was hoping for does not actually exist. i gave you the bounty. feel free to give it back as a random gift if you have too many points. :) but in all seriousness, thanks for all of your contributions to this community over the years. :pray: – burnettk Jun 20 '23 at 21:11
  • 1
    @burnettk I already have all the points... but not the right answer for this question. I hope that API will exist someday. – VonC Jun 20 '23 at 22:37