How can I filter all GitHub pull requests for a specific target branch

Solution 1:

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.

Solution 2:

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:

GitHub Pull-Request UI

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

Solution 3:

How to search for PRs by "from (head) branch", by "to (base) branch", and by author, including doing this using custom Chrome search engines you can quickly trigger in your search bar:

Note: the below searches for PRs (Pull Requests) on GitHub are intended to be done in the PR search bar here (https://github.com --> "Pull requests" at top) (direct link: https://github.com/pulls), not in the generic GitHub search bar at the top-left of nearly any GitHub page, although they might work there too.

See also my answer here: Can I search github labels with logical operator OR?

In GitHub lingo, the branch you are merging FROM is the head branch, and the branch you are merging TO is the base branch. See here: https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name. I'm not sure why they didn't choose from_branch and to_branch instead, as that would have been easier to remember.

1. Find all PRs FROM my_branch by using the search string head:my_branch:

is:open is:pr archived:false head:my_branch

Optionally, specify a repository too:

is:open is:pr archived:false repo:some_username/some_repository head:my_branch

2. Find all PRs merging inTO my_branch by using the search string base:my_branch:

is:open is:pr archived:false base:my_branch

Optionally, specify a repository too:

is:open is:pr archived:false repo:some_username/some_repository base:my_branch

3. References:

  1. From the official GitHub documentation: https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name:

    Search by branch name

    You can filter pull requests based on the branch they came from (the "head" branch) or the branch they are merging into (the "base" branch).

    Qualifier               Example  
    ---------------------   -------------------------------------------------
    `head:HEAD_BRANCH`      `head:change is:closed is:unmerged` matches pull 
                            requests opened from branch names beginning with 
                            the word "change" that are closed.
    
    `base:BASE_BRANCH`      `base:gh-pages` matches pull requests that are 
                            being merged into the gh-pages branch.
    
  2. @Andor Dávid's answer here

4. GOING FURTHER: Add custom Google Chrome search engines for GitHub searches

For those who don't know, the Chrome browser allows you to create custom search engines. If I go to my browser search bar and type gto followed by Space or Tab, then type in my branch name, my_branch, I see this. I am literally searching for all open PRs merging TO branch my_branch, using my custom search engine triggered by the gto shortcut I set up:

enter image description here

Here's how to configure those:

In Chrome, click on 3 dots in top-right --> Settings --> Search engine (in left pane) --> "Manage search engines" --> under "Other search engines", click the "Add" button. Set it up like this: enter image description here

Click "Save" when done. Here are 3 of my favorites:

  1. Search for GitHub PRs merging TO a given branch:
    1. Search engine: GitHub PRs merging TO branch
    2. Keyword: gto
    3. URL with %s in place of query: https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+base%3A%s
  2. Search for GitHub PRs merging FROM a given branch:
    1. Search engine: GitHub PRs merging FROM branch
    2. Keyword: gfrom
    3. URL with %s in place of query: https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+head%3A%s
  3. Search for GitHub PRs BY a certain author:
    1. Search engine: GitHub PRs BY this User
    2. Keyword: gby
    3. URL with %s in place of query: https://github.com/pulls?q=is%3Aopen+is%3Apr+author%3A%s+archived%3Afalse+

The way you figure out what to add for the URL search string is actually quite simple: you just go to GitHub and do a search manually using GitHub's tools, then you copy and paste the URL, replacing the search string you'd like to search for with %s in the custom Chrome Search engine URL. That's it! This process works for any and all websites which store their search variables into the URL once you do a custom search on their website, which is many, if not most, websites with search capability.

Keywords: Google chrome / browser custom search engines for GitHub PRs