Useful Github API GraphQL requests

2023-03-16

Here are a few useful GraphQL requests that I found while working with the Github API for get-activity and okra.

Get recent issue comments from a user

{
  viewer {
    issueComments(last: 40) {
      nodes {
        url
        publishedAt
      }
    }
  }
}

Get recent PR comments from a given repository

{
  repository(owner: "XX", name: "YY") {
    pullRequests(last: 50) {
      nodes {
        comments(last: 50) {
          nodes {
            url
            publishedAt
          }
        }
      }
    }
  }
}

Get all open issues from a repository

{
  repository(owner: "XX", name: "YY") {
    issues(filterBy: {states: [OPEN]}, last: 100) {
      nodes {
        title
        number
      }
    }
  }
}

Get CI failures from a repository

{
  repository(owner: "XX", name: "YY") {
    pullRequest(number: 13004) {
      commits(last: 1) {
        nodes {
          commit {
            checkSuites(last: 30) {
              nodes {
                workflowRun {
                  workflow { name }
                }
                checkRuns(filterBy: {conclusions: [FAILURE, STARTUP_FAILURE]}, first: 30) {
                  nodes {
                    name
                    conclusion
                    annotations(first: 30) {
                      nodes {
                        message
                        path
                        location {
                          start { line }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Get all files from a directory

{
  repository(owner: "XX", name: "YY") {
    object(expression: "branch:path/to/something/*") {
      ... on Tree {
        entries {
          name
          object {
            ... on Blob { text }
          }
        }
      }
    }
  }
}