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

GitGithubPull Request

Git Problem Overview


I'm working on a GitHub repo with lots of branches and pull requests.

Let's say, for example, that I have the following pull requests:

  • a to branch X
  • b to branch X
  • c to branch Y
  • d to branch X
  • e to branch Y.

Is there a way to find all the pull requests that are targeted for branch X (i.e. a -> X, b -> X, d -> X)?

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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: https://stackoverflow.com/questions/29136057/can-i-search-github-labels-with-logical-operator-or/61618255#61618255

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

Solution 4 - Git

Now that the GitHub CLI is available, here's how you would do it for a given repository.

You can install the gh CLI following the appropriate instructions for your OS and preferred package manager.

I use the cli/cli repo to demonstrate:

gh pr list --base trunk -R cli/cli

Output:

#3045  [hackday] mergeconflict                                                  hackday2102
#3044  Isolate test suite from environment variables                            env-tests
#3042  Remove functionality to add, view and edit binary files in gists         g14a:bug/gist-binary-files
#3023  Issue/pr create: exit with nonzero status code when "Cancel" was chosen  cancel-error-status
#3018  Add `pr create --body-file` flag                                         castaneai:pr-create-body-file
#3010  Add `api --cache` flag                                                   api-cache
#3008  Add interactive select in gist view                                      ganboonhong:interactive-gist-view
#2997  Feature/add files to gist                                                g14a:feature/add-files-to-gist
#2991  Repo create tweaks                                                       repo-create-prompt-change
#2953  Add `repo list` command                                                  cristiand391:add-repo-list
#2923  [WIP] first round of actions support                                     actions
#2728  Better completions                                                       rsteube:better-completions
#2261  pr status: show number of approvals                                      despreston:des/approval-count
#2160  Sign Windows .exes in a post-build hook                                  mbpreble:sign-windows-executables
#2080  store gh config in %APPDATA% on windows os                               RozzaysRed:appdata_configPath

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAVIDeveloperView Question on Stackoverflow
Solution 1 - GitAndor DávidView Answer on Stackoverflow
Solution 2 - Gituser456814View Answer on Stackoverflow
Solution 3 - GitGabriel StaplesView Answer on Stackoverflow
Solution 4 - GitastrochunView Answer on Stackoverflow