Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 5

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

Solved! Go to Solution.

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!

4 Replies
Copilot Lvl 3
Message 2 of 5

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

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

Solution
wei
Pilot Lvl 2
Message 3 of 5

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

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
      }
    }
  } 
}
Copilot Lvl 3
Message 4 of 5

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

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
      }
    }
  } 
}
Copilot Lvl 3
Message 5 of 5

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

@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).