Find a commit by oid/sha via graphql (v4) api

With the REST api it is possible to find a commit by the commit sha via this endpoint:

https://api.github.com/search/commits?q=hash:${sha}%20repo:${repoOwner}/${repoName}

and this

https://api.github.com/repos/${repoOwner}/${repoName}/git/commits/${sha}

How can I achieve the same via the graphql api? I tried using the search resouce but it seems like it only supports searching across issues, repositories and users - not commits:

{
  search(query: "hash: e9e1b1f5021358f2c769da20579773688f7d5638" type: COMMIT) {
    ...
  }
}

Thanks in advance!

I was able to find the global node id by using the REST api and then getting the commit via the graphql api:

{
  node(id: "MDY6Q29tbWl0NzgzMzE2ODowZjNmZWFlZGMyODBlY2FmMTE2NDE5MTkzNWRiMmYxNjdhMDc0NDgy") {
    id
    ... on Commit {
      message
    }
  }
}

This seems a bit counter-productive, since I’ve migrated to the graphql api to avoid over-fetching from the REST api. 

Additional question: Is there any way to see which branch the commit belongs to? I can see that Github Web UI uses an internal API to do this:

https://github.com/elastic/kibana/branch_commits/6a97f9a7ee797d302cb4f05120ff935bfe22eba0

The v4 equilivent for

https://api.github.com/repos/${repoOwner}/${repoName}/git/commits/${sha}

is 

{
  repository(owner: "<repoOwner>", name: "<repoName>") {
    object(oid: "<sha>") {
      ... on Commit {
        message
      }
    }
  }
}
2 Likes

This is great, thanks!

I was able to make it work for short sha’s as well:

{
  repository(owner: "elastic", name: "kibana") {
    object(expression: "e9e1b1f5") {
      ... on Commit {
        message
      }
    }
  }
}
1 Like

@wei Thanks again for your help.

I’m only interested in commits from master - do you know how to limit the results by branch (in addition to sha)?

Or alternatively: if it’s possible to see which branch the returned commit object belongs to? (that’ll allow me to discard non-master commits elsewhere in my application).

2 Likes

I’m also looking to filter by commits to master. Were you able to figure this out @sqren ? 

Not with v4 unfortunately. You can do it with v3: https://developer.github.com/v3/search/#considerations-for-commit-search

1 Like

Branch’s are just refs, like commits, and they are eaiser to get I think, because you just need to get it by it’s ref name

repository(name:\<name\> owner:\<owner\>){  
ref(qualifiedName:”refs/branchs/\<branchName\>”){}}  

And for master it’s even eaiser, because Repisitory has a defaultBranchRef field that usually points to master.

1 Like

Hey @jstacoder

Thanks for chiming in. I tried your suggestion like:

query getCommitMessage($repoOwner: String!, $repoName: String!, $sourceBranch: String!, $oid: GitObjectID!) {
  repository(owner: $repoOwner, name: $repoName) {
    ref(qualifiedName: $sourceBranch) {
      repository {
        object(oid: $oid) {
          ... on Commit {
            message
          }
        }
      }
    }
  }
}

The commit is retrieved regardless which branch I limit it to. I’d expect the commit to only be found if it’s in the given $sourceBranch

i see what happend, you are going back up to the repo, but you are already there.

@sqren looks like my original format for the ref was off, because I was going from memory. But yea, it should be refs/heads/BRANCHNAME

sorry i answered without really looking at your question, you are getting the commit regardless because you are asking by oid, which doesnt limit itself by anything, to gaurentee you are in the correct repo you cou ask its name, or get the commit message from the latest commit ie:

query getCommitMessage($repoOwner: String!, $repoName: String!, $sourceBranch: String!) {
  repository(owner: $repoOwner, name: $repoName) {
    ref(qualifiedName: $sourceBranch) {
      name
      target{
        ...on Commit{
          message
        }

      }
    }
  }
}

Hi again,

to gaurentee you are in the correct repo you cou ask its name, or get the commit message from the latest commit ie:

Thanks for the suggestion but I need to know whether the commit is on the given branch. Your suggestion seems to only get the head commit.

hello Sqren,

 I hope this message finds someone reading it right, I just want to ask if I can use this commit?

@html-codder wrote:

hello Sqren,

 I hope this message finds someone reading it right, I just want to ask if I can use this commit?

Not sure which commit you mean but you are welcome to use any of the code snippets in this thread.

ok thanks for your kindness i do really appreciate alot