How can I filter all GitHub pull requests for a specific target branch
As of 2016-01-10 this has been added to the gh search bar api, see next answer.
Original accepted (and now outed answer) left un-edited.
Currently Not Available via Web Interface
GitHub currently does not provide a way to filter pull-requests by their target branch through their web interface. Instead, all you currently get is just the entire list of pull-requests with the names of the topic branches:
Clicking into a pull-request will show the target branch, but that doesn't really help you do any of the filtering that you want to do.
You Can Use the GitHub REST API Instead
It is possible to filter pull-requests by using the GitHub REST API, however:
GET /repos/:owner/:repo/pulls?base=:branch
That should show you all the open pull-requests for the repo :owner/:repo
,
filtered by requests that target :branch
as their base branch. From the
documentation:
Filter pulls by base branch name. Example:
gh-pages
.
Example Using cURL
If you have curl
available, you can test this on a public repo from the
command line. Here, the repo being queried is this one (https://github.com/codecombat/codecombat), and we are getting all pull requests from base
branch (the branch the PRs are merging TO) named master
, and then storing the results into a pulls.json file we will parse through next.
curl https://api.github.com/repos/codecombat/codecombat/pulls?base=master > \
pulls.json
That will return a JSON response of the following form, now stored inside file pulls.json:
[
{
"url": "https://api.github.com/repos/codecombat/codecombat/pulls/879",
"id": 14955421,
"html_url": "https://github.com/codecombat/codecombat/pull/879",
"head": {
"label": "DanielRodriguezRivero:patch-4",
"ref": "patch-4",
"sha": "baff84f0aeee12f23e3608558ae5341a0b5f939b",
"repo": {
"id": 16202384,
"name": "codecombat",
"full_name": "DanielRodriguezRivero/codecombat"
}
},
"base": {
"label": "codecombat:master",
"ref": "master",
"sha": "5e2f3ac7cb731a6e40e81737a5122c7fe1b746d3",
"repo": {
"id": 15193430,
"name": "codecombat",
"full_name": "codecombat/codecombat"
}
}
}
]
Each object in the array is a pull request (PR), filtered by the base=target
branch, which we specified to be master
in our curl
command above.
The JSON actually contains much more information than this; I've just removed most of it to show the relevant parts for this question.
Parsing the cURL Response
You could probably write a Python/Ruby/PHP/Whatever script to then parse out the html_url
property of each pull-request and list it on the command line. For example, here's a simple Ruby script that will parse the output of a JSON response saved from the curl
output:
require 'json'
json = JSON.parse(File.read('./pulls.json'))
pulls = json.map { |pull| { title: pull['title'], url: pull['html_url'] } }
pulls.each do |pull|
puts pull.values
puts
end
Which outputs the following:
$ ruby parser.rb
Update es-ES.coffee
https://github.com/codecombat/codecombat/pull/879
Fix deltas referring to last system saved
https://github.com/codecombat/codecombat/pull/874
Refactor getNameById and add naming to systems in deltas
https://github.com/codecombat/codecombat/pull/866
Traducido varios textos del fichero es-ES.coffe al espa├▒ol de Espa├▒a
https://github.com/codecombat/codecombat/pull/865
Anon name collide
https://github.com/codecombat/codecombat/pull/834
Yes, you can do it.
In Github's terminology the "to branch" is "base"
So the search phrase is: is:open is:pr base:X
Official description: Search based on branch names
Optionally you can add is:merged
or is:unmerged
filters as well.