GitHub APIs: How To Fetch Pull Requests/Issues by an User/Organization/Repository
Last year (not so long ago 😛), I was working on a feature for Real Dev Squad where I needed to show the contributions of each member of the organization in form of pull requests. I had gone through the Github API documentation but I didn’t find it of much use to my particular usecase. I had then thought of scraping the webpage https://github.com/pulls for this purpose which had to be rejected immediately as it would not have been that efficient over long time. Digging deeper into the Github APIs, I finally figured out how to make use of the Github APIs for my task. Github APIs are like Lego blocks. Different queries can be combined in various ways to make it suitable for the requirement. What can be different scenarios?
- Listing PRs of a user: all of them/for an org/for a particular repo/open/closed
- Listing issues of a user: all of them/for an org/for a particular repo/assigned/unassigned/open/closed
- Listing total PRs: by an org/a particular repo/open/closed
- Listing total issues by an org/a particular repo/assigned/unassigned/open/closed
There can be many more combinations. You get the idea, right?
Let us dissect what this piece of URL does:
https://api.github.com/search/issues?q=author:swarajpure+org:Real-Dev-Squad+type:pr&sort=asc
Here the https://api.github.com
is the base URL. /search
and /issues
are called the path parameters (aka params). Yes, you guessed it right, they're used in your computer's file explorer to locate files or folders. Path parameter can be easily distinguished by the slash /
. Now you might be wondering what is a ?
doing in the URL? What follows the question mark is called query parameter. The question mark is used to separate the path params from the query params. While path params are used to locate resource in a pool of data, query params are used to filter inside that particular resource.
Here we can see mainly 2 query params q
and sort
. What follows these parameters are their values which are given after the =
sign. The q
parameter contains the data to be filtered and sort
sorts the data, here in ascending order (asc). The &
sign combines the two query params. Now you must be wondering about the +
signs, right? They can be thought of as if they're combining multiple nested params. Remember, using &
instead of +
would terminate the q
param.
In a broad sense, this URL tells the server that we are searching something from the search
path param. Next, are looking for issues (PRs are also a part of issues
path param, not my fault. It's provided by Github itself 🤣) Next, we encounter a ?
which means we are going to have query params now. The query param q
is telling that we need data from the author swarajpure
in the org whose name is Real-Dev-Squad
and whose type is pr
(pull request). Finally we encounter a &
sign, which specifies that earlier query param has terminated and new query param is to be added which is sort
whose value is asc
that is ascending.
Essentially, the above API endpoint would give us all the PRs by a user called swarajpure
in the organization Real-Dev-Squad
in the ascending order!
Don’t worry if you’re overwhelmed! This is easy! Just bear with me for a little more and we’ll see more examples and then things will be more clear 😄
Now let’s see how can we get total pull requests by a user (not particularly in any repo) — just removed the param org
https://api.github.com/search/issues?q=author:swarajpure+type:pr&sort=asc
How about getting all the issues started by a user in an org?
https://api.github.com/search/issues?q=author:swarajpure+org:Real-Dev-Squad+type:issue&sort=asc
We haven’t talked about a repo yet, right? Let’s see how to get open issues by a user in a repo:
Enough of stuff by a user! Let’s see how can we get total PRs in an org:
https://api.github.com/search/issues?q=org:Real-Dev-Squad+type:pr&sort=asc
Let’s see how to get all the PRs in a repo (note that this is having different path params!):
https://api.github.com/repos/Real-Dev-Squad/website-backend/issues
I guess you must have got enough idea by now. Feel free to explore the Github API docs here: https://docs.github.com/en/rest
I hope this blog somehow helps you! Thank you! 😃