使用 GitHub CLI 遍历 PR 列表?

Looping through list of PRs using GitHub CLI?

我正在寻找有关如何使用 GitHub CLI 解决特定脚本问题的建议。假设我使用 gh search prs "SPARK-" --match body 命令获取所有 prs 的列表,其正文包含 SPARK- 短语。现在,我想遍历该 prs 列表,并输出该 pr 的 url 及其 SPARK 编号(在 pr 正文的某处,有文字说“SPARK-”,后跟唯一的 5数字)。关于如何处理这个问题的任何想法?我很难弄清楚如何使用我的 gh cli 输出构建这个循环(或者如果它甚至可能?),让我们在一些 bash 脚本中说,并弄清楚如何专门隔离 SPARK 号.

您可以在一次 gh 调用中完成所有事情:

gh search prs "SPARK-" --match body --json url,body --jq '
    "SPARK-([[:digit:]]+)" as $re
    | map(select(.body | test($re)))
    | .[].body |= match($re).captures[0].string
    | map([.url, .body])[]
    | @csv'

--json参数选择URL和描述,--jq将JSON按摩成comma-separated列表(见jq playground ):

"https://github.com/just4jc/CSE6242-Data-and-Visual-Analytics/pull/32","1298180"
"https://github.com/h950059h/kafka-examples/pull/33","1298180"
"https://github.com/terrorizer1980/zeppelin/pull/75","2420837"
"https://github.com/muyenzo/reference-architectures/pull/10","574943"
"https://github.com/microsoft/az-python/pull/461","35714"
"https://github.com/radanalyticsio/streaming-amqp/pull/38","1298181"
"https://github.com/orysmudi/tensorflow/pull/78","573164"
"https://github.com/apache/iceberg/pull/4479","30669"
"https://github.com/alonsoir/strata-2016/pull/17","1298180"

jq命令首先定义了一个正则表达式SPARK-([[:digit:]]+),然后用它来删除所有找到SPARK但后面没有数字的PR;然后它用正则表达式的第一个匹配项的捕获组替换正文,只返回数字。

最后,它重新排列成数组并转换为 CSV。