Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine which Pull Request a commit belongs to using the GitHub API?

Given a commit SHA, I'd like to use the GitHub API to determine which pull request it belongs to.

GitHub displays this information on the commit page:

PR link for a commit

This information isn't in the API response for that commit, however:

curl --silent 'https://api.github.com/repos/hammerlab/pileup.js/commits/ee49f07dba3821109b3cf45404446f434a897558' | grep 328
(nothing)

Is it possible to get this information using the GitHub API? I realize that I could crawl all the recent PRs for the repo, but this seems inefficient.

For context: I'd like to do this from a Travis-CI "push" job, where I have access to the SHA but not any information about whether it's part of a pull request.

like image 828
danvk Avatar asked Oct 23 '15 16:10

danvk


2 Answers

It is possible, using the Search API, which supports this feature:

https://help.github.com/articles/searching-issues/#search-by-the-commit-shas-within-a-pull-request

For example:

https://api.github.com/search/issues?q=ee49f07dba3821109b3cf45404446f434a897558

returns a single result, which is this pull request:

https://api.github.com/repos/hammerlab/pileup.js/issues/328
like image 169
Ivan Zuzak Avatar answered Sep 28 '22 06:09

Ivan Zuzak


It seems to be possible using this new feature in the API v.3:

https://developer.github.com/v3/repos/commits/#list-pull-requests-associated-with-a-commit

This URL now redirects to:

https://docs.github.com/en/rest/reference/repos#list-pull-requests-associated-with-a-commit

This is the syntax:

GET /repos/:owner/:repo/commits/:commit_sha/pulls
like image 33
JoseMMC Avatar answered Sep 28 '22 05:09

JoseMMC