Github API Search

Usecase 1
I have a private repository say R1.
I have created 2 pull requests on that repo - R1, namely P1 & P2
I have added a label L1 for the pull request P2.

  • How will the REST API search query look like , if i want to get the list of all open pull requests from github repository - R1 that has the label L1 ? I dont want the results from any other repos in github (public or private)

Usecase 2
I have an organization say O1 and it contains n number of repos.

  • How will the REST API search query look like , if i want to get the list of all open pull requests from all the repositories under the organization O1 that has the label L1 ? I dont want the results from any other repos in github (public or private)

Any hint to this will be of great help.

Usecase 2 - Solution

After a lot of trial and error, found the answer

https://api.github.com/search/issues?q=org:ORGANIZATION_NAME+is:pr+state:open

replace ORGANIZATION_NAME with your desired organization name

Example:

https://api.github.com/search/issues?q=org:Googly+is:pr+state:open

This will return all the open pull requests for the github organization named - Googly


if you want to list all open pull requests that has a specific label, the url should be appended with +label:LABEL_NAME

https://api.github.com/search/issues?q=org:ORGANIZATION_NAME+is:pr+state:open+label:LABEL_NAME

Example:

https://api.github.com/search/issues?q=org:Googly+is:pr+state:open+label:december-bug

Usecase 1 - solution

https://api.github.com/search/issues?q=repo:REPO_NAME+is:pr+state:open

replace REPO_NAME with your desired repository name.

Please keep in mind that, if the repository is under an organization, then specify it fully as you see in the Github web version.
That is, if the repository - R1 is under an organization named Googly , then REPO_NAME should be given as Googly/R1

The label based search can also be done similarly as mentioned in above Usecase -2 solution

Generic Note (ignore if you already know):
In both the cases, you need to set the following headers

  • Accept (value should be application/vnd.github.v3+json for json response )
  • Authorization (if you are using basic authentication mechanism)
  • user-agent (add this only if your application gets a failure message asking to add it. Give some meaningful value.)
1 Like