isaacs / github

Just a place to track issues and feature requests that I have for github
2.21k stars 129 forks source link

GitHub GraphQL (v4) API - how to check status? #1710

Open oprogramador opened 4 years ago

oprogramador commented 4 years ago

How can I check the status (success/ failure/ pending) for the last commit of the default branch for a given repo, using the GraphQL API?

In the v3 API I can do this:

const generalInfo = await retrieve(`https://api.github.com/repos/${organization}/${repo}`);
const defaultBranch = generalInfo.default_branch;
const lastCommit = (await retrieve(`https://api.github.com/repos/${organization}/${repo}/commits/${defaultBranch}`)).sha;
const lastCommitStatus = (await retrieve(`https://api.github.com/repos/${organization}/${repo}/commits/${lastCommit}/status`)).state;
kevinmukuna commented 3 years ago

Hey, in case anyone else needs this. You can also query reviews within a pr nodes, please

Query pr statuses

query {
    repository(owner:"<org/username>", name:"<repository>") {
      pullRequest(number:< pr_number >){
        commits(last:1){
          nodes{
            commit{
              status{
                state
                contexts{
                  state,
                  context,
                  description,
                  createdAt,
                  targetUrl
                }
              }
            }
          }
        }
      }
    }
}

Query pr reviews

query {
  repository(owner:"<org/username>", name:"<repository>") {
    pullRequest(number:< pr_number >){
      reviews(last:5){
       nodes{
        state,
        url, author{login},
        createdAt,
       }
      }
    }
  }
}

Query pr statuses and reviews

query {
    repository(owner:"<org/name>", name:"<repository>") {
      pullRequest(number:<pr_number>){
        reviews(last:5){
         nodes{
          state,
          url, author{login},
          createdAt,
         }
       }
        commits(last:1){
          nodes{
            commit{
              status{
                state
                contexts{
                  state,
                  context,
                  description,
                  createdAt,
                  targetUrl
                }
              }
            }
          }
        }
      }
    }
}
oprogramador commented 3 years ago

Thanks @kevinmukuna

At this moment, I don't need this but when I check the commit build status with v4 API in the future, maybe I'll tell whether your solution works.