1

How to write a GraphQL query to retrieve all the workflows/runs from github I tried below query for to get node id

organization(login: "abc") {
        repositories(first: 100) {
            nodes {
                    id
                    name
                    }
                }
            }

and below query to get workflow.

    nodes(ids: "sdefrgrt") {
        ... on Workflow {
          id
          createdAt
          name
          updatedAt
        }
}
Panda
  • 513
  • 2
  • 11
  • I was looking for similar info ("how to get workflow runs for a repo via GraphQL") and couldn't find it, but the REST API has what you want: https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository – Zach Bloomquist Dec 02 '22 at 02:29
  • Thank you for helping. I did use REST APIs and my app is ready already. – Panda Dec 20 '22 at 08:57

3 Answers3

3

You may get the workflow runs via:

     {
      node(id: "YOUR-WORKFLOW-NODE-ID") {
        ... on Workflow {
          runs(first: 10) {
            nodes {
              runNumber
              createdAt
              checkSuite {
                commit {
                  message
                  associatedPullRequests(first: 1) {
                    nodes {
                      number
                    }
                  }
                  history {
                    nodes {
                      author {
                        date
                        email
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

Workflow's node_id could be taken from https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow

VolkanT
  • 574
  • 5
  • 10
  • Just adding a link to GitHub docs about global ids and how to get them from REST, and use them in graphQL. https://docs.github.com/en/graphql/guides/using-global-node-ids – joeycozza May 31 '23 at 18:42
2

Yes, this should be available via GraphQL (you can try this query out with Github's GraphQL explorer):

{
  repository(owner: "$ORG", name: "$REPO") {
    pullRequest(number: $NUMBER) {
      commits(first: 5) {
        edges {
          node {
            commit {
              checkSuites(first: 5) {
                nodes {
                  workflowRun {
                    url
                    workflow {
                      name
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

You'd adjust the first parameters, and the client would need to do some grouping on the data that comes back from GraphQL.

  • 2
    Careful, doing it this way only gets you workflows based on a pull request. But you can have workflows that are not associated with pull requests at all. – joeycozza May 31 '23 at 18:41
1

Top-level GitHub Actions access (instead of going through pullRequest, which is incomplete) is not available. The roadmap doesn't mention future plans regarding GraphQL and GitHub Actions. Neither does the changelog, looking into the past. Lastly, the present state doesn't mention GitHub Actions in its schema either (not as a top-level concept anyway).

Alex Povel
  • 150
  • 2
  • 12