如何筛选所有针对特定目标分支的GitHub拉取请求

110

我正在开发一个拥有许多分支和拉取请求的GitHub存储库。

例如,假设我有以下拉取请求:

  • a 到 分支 X
  • b 到 分支 X
  • c 到 分支 Y
  • d 到 分支 X
  • e 到 分支 Y

是否有一种方法可以找到所有针对分支 X 的拉取请求(即 a -> Xb -> Xd -> X)?

4个回答

161

可以的,你可以做到。

在GitHub术语中,“to branch”被称为“base”,所以搜索短语是:is:open is:pr base:X

官方描述:基于分支名称搜索

您还可以选择添加is:mergedis:unmerged过滤器。


1
欢迎来到StackOverflow。如果您能在答案中添加命令,那将会更好。 :) - Drazisil
1
不幸的是,这将匹配所有以X开头的分支。有人知道如何搜索特定的分支而不是前缀吗? - yoyo
2
要指定一个“head”分支,意思是:PR来自哪个分支,而不是合并到哪个分支,请使用“head:my_branch”而不是“base:my_branch”。请参见:https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name。 - Gabriel Staples
1
我详细阐述了这一点,并编写了自定义的Chrome搜索。以下是我的回答及其方法:https://dev59.com/vmIj5IYBdhLWcg3wKyS0#66287268。 - Gabriel Staples
有点傻,你只能搜索以搜索查询开头的分支。真希望能够搜索包含的内容。 - undefined
显示剩余2条评论

17

如何通过“from(head)分支”、“to(base)分支”和作者搜索PR(Pull Requests),包括使用可以快速在搜索栏中触发的自定义Chrome搜索引擎:

注意:下面在GitHub上搜索PR(Pull Requests)的搜索是在这里完成的(https://github.com --> 顶部的“Pull requests”)(直接链接:https://github.com/pulls),而不是在几乎任何GitHub页面左上角的通用GitHub搜索栏中,尽管它们也可能会在那里起作用。

另请参见我在这里的答案:我可以使用逻辑运算符OR搜索GitHub标签吗?

在 GitHub 的术语中,你正在合并的分支是“源”分支(FROM),而你将合并到的分支是“目标”分支(TO)。请查看此处:https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name。我不确定为什么他们没有选择使用“from_branch”和“to_branch”,因为那样会更容易记住。
1. 使用搜索字符串“head:my_branch”查找所有来自于“my_branch”的 PR:
is:open is:pr archived:false head:my_branch

可选地,也可以指定一个代码库:

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

2. 使用搜索字符串base:my_branch查找所有合并到my_branch的PR:

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

可选,还可以指定一个代码库:

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

3. 参考文献:

  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. 更进一步:为GitHub搜索添加自定义Google Chrome搜索引擎

对于那些不知道的人来说,Chrome浏览器允许您创建自定义搜索引擎。如果我在浏览器搜索栏中键入gto,然后按下空格Tab,然后输入我的分支名称my_branch,我会看到这个结果。我实际上是使用我设置的gto快捷方式触发的自定义搜索引擎,搜索所有合并到分支my_branch的开放式PR:

enter image description here

以下是如何配置它们的方法:
在Chrome中,点击右上角的3个点 -> 设置 -> 搜索引擎(在左侧面板中)-> “管理搜索引擎” -> 在“其他搜索引擎”下,点击“添加”按钮。设置如下: 输入图像描述 完成后点击“保存”。这里有我最喜欢的3个搜索引擎:
  1. 搜索合并到给定分支的GitHub PR
    1. 搜索引擎:GitHub PRs merging TO branch
    2. 关键词:gto
    3. 使用%s替换查询的URL:https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+base%3A%s
  2. 搜索从给定分支合并的GitHub PR
    1. 搜索引擎:GitHub PRs merging FROM branch
    2. 关键词:gfrom
    3. 使用%s替换查询的URL:https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+head%3A%s
  3. 按某个作者搜索GitHub PR
    1. 搜索引擎:GitHub PRs BY this User
    2. 关键词:gby
    3. 使用%s替换查询的URL:https://github.com/pulls?q=is%3Aopen+is%3Apr+author%3A%s+archived%3Afalse+
你确定如何添加URL搜索字符串的方法其实很简单:只需手动使用GitHub工具进行搜索,然后复制并粘贴URL,在自定义的Chrome搜索引擎URL中用%s替换要搜索的搜索字符串。就这样!一旦在网站上进行自定义搜索,所有将搜索变量存储到URL中的网站都可以使用此过程,这是许多网站(如果不是大多数)具备的搜索功能。
关键词:Google Chrome / 浏览器自定义搜索引擎适用于GitHub PRs

15

截至2016-01-10,这已被添加到gh搜索栏api,请参见下一个答案。

原始的已接受(现已公开)答案未被编辑。


目前无法通过Web界面提供

GitHub目前没有提供通过Web界面过滤拉取请求的方法,以便按其目标分支进行筛选。相反,您当前只能获得具有主题分支名称的整个拉取请求列表:

GitHub Pull-Request UI

点击进入拉取请求会显示目标分支,但这并不能帮助你进行任何想要做的筛选。

您可以使用 GitHub REST API 取代

可以使用 GitHub REST API 进行拉取请求的筛选,然而:

GET /repos/:owner/:repo/pulls?base=:branch

这将显示代码库:owner/:repo的所有打开拉取请求,过滤目标为:branch分支的请求。参考文档:

按基本分支名称筛选拉取请求。 例如:gh-pages

cURL示例

如果您可以使用curl,则可以从命令行测试公共代码库。 这里查询的代码库是此代码库(https://github.com/codecombat/codecombat),我们正在获取所有从base分支(PR合并到的分支)中命名为master的拉取请求,然后将结果存储到我们接下来要解析的 pulls.json 文件中。

curl https://api.github.com/repos/codecombat/codecombat/pulls?base=master > \
pulls.json

这将返回一个JSON响应,其格式如下,现在存储在文件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"
      }
    }
  }
]

数组中的每个对象都是一个拉取请求 (PR),由我们在上面的 curl 命令中指定的 base=target 分支过滤,我们将其设置为 master

JSON 实际上包含比这更多的信息;我已删除了大部分内容,以显示与此问题相关的部分。

解析 cURL 响应

您可以编写 Python/Ruby/PHP/其他脚本来解析每个拉取请求的 html_url 属性,并在命令行上列出它。例如,下面是一个简单的 Ruby 脚本,它将解析从 curl 输出保存的 JSON 响应的输出:

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

它输出以下内容:

$ 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

我添加了一条注释,说明这个答案现在已经过时了,所以有些犹豫是否要删除你的整个第一部分。 - tacaswell

1
现在GitHub CLI已经可用,这是如何为给定的存储库执行操作的方法。
您可以按照适用于您的操作系统和首选软件包管理器的适当说明安装gh CLI。
我使用cli/cli 存储库进行演示: gh pr list --base trunk -R cli/cli 输出:
#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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接