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

我正在做一个 GitHub 回购,有很多分支和请求。

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

  • a呼叫 X分支
  • b呼叫 X分支
  • c呼叫 Y分支
  • d呼叫 X分支
  • e呼叫 Y分支。

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

37579 次浏览

截至2016-01-10,这已经被添加到高级搜索栏 API,请参阅下一个答案。

原始答案未编辑。


目前不能通过 Web 界面使用

GitHub 目前没有提供过滤目标请求的方法 分支通过他们的网络界面。相反,所有你目前得到的只是 带有主题分支名称的 pull-request 的完整列表:

GitHub Pull-Request UI

单击 pull-request 将显示目标分支,但这并不显示 真的可以帮助你做任何你想做的过滤。

您可以改用 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分支(PRs 合并为 TO 的分支)获得所有的拉请求,命名为 master,然后将结果存储到一个 拉住 Json文件中,我们将在下一步解析。

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

它将返回以下表单的 JSON 响应,现在存储在文件 拉住 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"
}
}
}
]

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

JSON 实际上包含比这更多的信息; 我只是删除了其中的大部分,以显示这个问题的相关部分。

解析 cURL 响应

您可以编写一个 Python/Ruby/PHP/Whatever 脚本,然后解析每个 pull-request 的 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

是的,你能做到。

在 Github 的术语中,“分支”是“基础” 所以搜索短语是: is:open is:pr base:X

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

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

如何通过“ from (head) Branch”,通过“ to (base) Branch”,以及通过作者 包括来搜索 PR,使用自定义的 Chrome 搜索引擎,你可以在搜索栏中快速触发:

注意: 以下 GitHub 上的公关(拉请求)搜索应该在这里的公关搜索栏中完成(https://github.com—— > “拉请求”在顶部)(直接链接: https://github.com/pulls) ,没有在几乎任何 GitHub 页面左上角的通用 GitHub 搜索栏中完成,尽管他们也可能在那里工作。

在这里也可以看到我的答案: 我可以用逻辑运算符 OR 搜索 github 标签吗?

在 GitHub 行话中,您正在合并 FROM 的分支是 head分支,而您正在合并 TO 的分支是 base分支。看这里: https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name。我不知道为什么他们没有选择 from_branchto_branch,因为那样会更容易记住。

1. 使用搜索字符串 head:my_branch查找所有 PRs FROM my_branch:

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. 来自 GitHub 的官方文档: https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name:

    按分支名称搜索

    您可以根据请求来自哪个分支(“ head”分支)或者它们合并到哪个分支(“ base”分支)来筛选请求。

    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 的回答

4. 更进一步: 为 GitHub 搜索添加自定义的 Google Chrome 搜索引擎

对于那些不知道的人,Chrome 浏览器允许你创建自定义搜索引擎。如果我去我的浏览器搜索栏,键入 gto后面跟着 SpaceTab,然后键入我的分支名称,my_branch,我看到这一点。我正在逐字地搜索所有公开的公关合并到分支 my_branch,使用我的自定义搜索引擎触发的 gto快捷方式,我设置:

enter image description here

下面是如何配置它们:

在 Chrome 中,点击右上角的3个点—— > 设置—— > 搜索引擎(在左窗格中)—— > “管理搜索引擎”—— > “其他搜索引擎”下,点击“添加”按钮。设置成这样: enter image description here

完成后点击“保存”。以下是我最喜欢的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 公关译者:
    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,使用 GitHub 的工具手动搜索,然后复制粘贴 URL,在自定义 Chrome 搜索引擎 URL 中用 %s替换你想要搜索的搜索字符串。就是这样!这个过程适用于任何和所有的网站存储他们的搜索变量到网址一旦你做一个自定义搜索他们的网站,这是许多,如果不是大多数,网站的搜索功能。

关键词: 用于 GitHub PR 的 Google chrome/浏览器自定义搜索引擎

既然 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