【问题标题】:How can I filter all GitHub pull requests for a specific target branch如何过滤特定目标分支的所有 GitHub 拉取请求
【发布时间】:2013-12-31 01:58:38
【问题描述】:

我正在开发一个包含大量分支和拉取请求的 GitHub 存储库。

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

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

有没有办法找到针对分支X(即a -> Xb -> Xd -> X)的所有拉取请求?

【问题讨论】:

  • 你应该更新接受的答案。

标签: git github pull-request


【解决方案1】:

是的,你可以做到。

在 Github 的术语中,“to branch”是“base” 所以搜索词组是:is:open is:pr base:X

官方说明:Search based on branch names

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

【讨论】:

  • 欢迎来到 StackOverflow。如果您可以将命令添加到您的答案中,它会变得更好。 :)
  • 这是一个新功能!更多详情请访问:help.github.com/articles/searching-issues/…
  • 不幸的是,这匹配所有以 X 开头的分支。有人知道如何搜索特定分支,而不是前缀吗?
  • 要指定一个head 分支,意思是:PR 来自的分支,而不是合并到的分支,使用head:my_branch 而不是base:my_branch。请参阅:docs.github.com/en/github/searching-for-information-on-github/…
  • 重要提示:此处的搜索(例如:repo:my_username/my_repository is:open is:pr base:my_branch)必须在此处的 PR 搜索栏中进行(github.robot.car/pulls),而不是在几乎所有搜索引擎左上角的通用 GitHub 搜索栏中GitHub 页面!
【解决方案2】:

截至 2016 年 1 月 10 日,这已添加到 gh 搜索栏 api,请参阅下一个答案。

原始接受(现在已过期的答案)未经编辑。


目前无法通过 Web 界面访问

GitHub 目前不提供按目标过滤拉取请求的方法 通过他们的 Web 界面进行分支。相反,您目前得到的只是 带有主题分支名称的拉取请求的完整列表:

点击拉取请求将显示目标分支,但这不会 真正帮助您进行任何您想做的过滤。

您可以改用 GitHub REST API

可以使用GitHub REST API 过滤拉取请求, 但是:

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

这应该会向您显示所有针对 repo :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),由 base=target 分支过滤,我们在上面的 curl 命令中指定为 master

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

解析 cURL 响应

您可能可以编写一个 Python/Ruby/PHP/Whatever 脚本,然后解析出每个拉取请求的 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

【讨论】:

  • 我添加了一条注释,说明这个答案现在已经过时了,犹豫要不要删除你的整个第一部分。
【解决方案3】:

如何通过“来自 (head) 分支”、“至 (base) 分支”和作者搜索 PR,包括使用自定义 Chrome 搜索引擎执行此操作可以在您的搜索栏中快速触发:

注意:以下在 GitHub 上搜索 PR(Pull Requests)是为了在 PR 搜索栏中完成(https://github.com --> 顶部的“Pull requests”)(直接链接:@987654322 @),不是在几乎所有 GitHub 页面左上角的通用 GitHub 搜索栏中,尽管它们也可能在那里工作。

也可以在这里查看我的回答:Can I search github labels with logical operator OR?

在 GitHub 术语中,您要合并的 FROM 分支是 head 分支,您要合并到的分支是 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 查找所有来自 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查找所有合并到TO 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

    按分行名称搜索

    您可以根据拉取请求来自的分支(“头”分支)或它们要合并到的分支(“基”分支)过滤拉取请求。

    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,然后输入SpaceTab,然后输入我的分支名称my_branch,我会看到。我实际上是在使用我设置的gto 快捷方式触发的自定义搜索引擎搜索所有合并到分支my_branch 的开放PR:

以下是如何配置它们:

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

完成后点击“保存”。以下是我最喜欢的 3 个:

  1. 搜索合并到给定分支的 GitHub PRs
    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 PRs
    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 PRs
    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 进行搜索。而已!一旦您在他们的网站上进行自定义搜索,这个过程适用于任何和所有将其搜索变量存储到 URL 中的网站,这是许多(如果不是大多数)具有搜索功能的网站。

关键字:GitHub PRs 的谷歌浏览器/浏览器自定义搜索引擎

【讨论】:

    【解决方案4】:

    现在GitHub CLI 已经可用,下面是给定存储库的操作方法。

    您可以在appropriate instructions 之后为您的操作系统和首选包管理器安装gh CLI。

    我使用cli/cli repo 来演示:

    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
    

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2022-12-09
      • 1970-01-01
      • 2013-02-12
      • 2018-04-04
      • 2014-11-30
      • 1970-01-01
      • 2020-06-25
      相关资源
      最近更新 更多