从命令行获取拉取请求的标题

问题描述 投票:0回答:6

我正在编写一个持续集成步骤,在允许合并之前检查拉取请求的标题是否有正确的标签和格式。

为此,我需要在给出 PR 编号时回显 Pull 请求的标题。有没有办法简单地在命令行中执行此操作?

git github jenkins terminal ghprb
6个回答
5
投票

根据@Simon-Warta的答案,您可以执行以下操作:

git clone https://github.com/user_org/project.git
pushd project
for pr in $(git log --pretty="%s" --merges | grep pull | sed -e "s/.*#\([0-9]\+\).*/\1/g" | sort -rn | uniq); do
  curl https://api.github.com/repos/user_org/project/pulls/${pr} 2>/dev/null | jq '.title';
done
popd

您的计算机上需要

curl
jq
,但如果您有一个合理的 Linux 发行版,这应该不难..


5
投票

使用Github新官方CLI(命令行界面):

gh pr view 3 --repo OWNER/REPO | head -n 1

地点:

  • 3
    是PR号码
  • gh pr view [options]
    显示 PR 的标题(第一行)、正文和其他信息
  • head -n 1
    仅保留
    gh pr view
    输出的第一行,以便仅获取标题

请参阅其他详细信息和选项以及安装说明


2
投票

对于拉取请求使用

https://api.github.com/repos/randombit/botan/pulls/359

对于拉取请求中的补丁,请在

Subject:
url 中搜索
.patch

https://github.com/randombit/botan/pull/359.patch

请注意,您只能在 Github API 上执行每小时 60 个请求和 IP


2
投票

谢谢凯文·克莱泽。 我的解决方案版本没有 jq 且只有 last tag 中的 current commits

TL;博士

代替

curl https://api.github.com/repos/user_org/project/pulls/${pr} 2>/dev/null | jq '.title';

使用

curl https://api.github.com/repos/user_org/project/pulls/${pr} | grep title | cut -d'"' -f4

对于 CocoaHeadsRu 存储库示例

git clone https://github.com/cocoaheadsru/application.git  // clone the repository
cd application           // go to the application folder
touch getTitles.sh       // create file getTitles.sh
chmod a+x getTitles.sh   // make the file executable
vi getTitles.sh          // open the file in the vim text editor

添加以下代码getTitles.sh

for pr in $(git log $(git describe --tags --abbrev=0)..HEAD --pretty="%s" --merges | cut -d' ' -f4 | cut -d'#' -f2 | tr '\n' ' ')
do
    curl https://api.github.com/repos/cocoaheadsru/application/pulls/${pr} | grep title | cut -d'"' -f4
done

保存代码退出vim

:wq

现在我们可以使用我们的脚本创建一个发行说明文件

./getTitles.sh > ReleaseNotes.txt
cat ReleaseNotes.txt

// [qXUdCQay] Changed app build number, deleted MARKETING_VERSION and CU…
// Feature/kn vl5 eaw/add bluetooth always key
// [CQoos82f] Updated Realm, increment app version
// upgraded travis configuration
// Открытие места проведения митапа в 2ГИС
// Автокапитализация и технические улучшения
// Интеграция Travis-CI
// Postpone fetchEvents for main view after view appearing

描述脚本代码:

1

git describe --tags --abbrev=0                  // get the last tag
git log $(git describe --tags --abbrev=0)..HEAD // get all commit from last tag to HEAD
[--//--] --pretty="%s"                          // all commits in one line
[--//--] --merges                               // merged commits only

2

使用

空格字符字符串分成几部分,并获得第四个值

ex

合并来自antonsergeev88/swipeToPop_crash的拉取请求#347

[--//--] cut -d' ' -f4 // get '#347'
3

使用

'#' 字符

字符串分成几部分并获得第二个值

#

347 [--//--] cut -d'#' -f2 // get '347'

4

tr '\n' ' ' // replaced '/n' with a space character

我们得到类似 
355 354 353 352 350 349 348 347

5

loop

中使用 github api: curl https://api.github.com/repos/cocoaheadsru/application/pulls/347

我们

json格式获取所有拉取请求。其中之一: { "url": "https://api.github.com/repos/cocoaheadsru/application/pulls/347", "id": 215781239, "number": 347, "state": "closed", ... "title": "Postpone fetchEvents for main view after view appearing", ... "user": {...}, "merged_at": "2018-09-30T17:26:47Z", "merge_commit_sha": "553d8ea6f189882a22d424d0770785b53ddc4ab4", "commits": 1, ... }

6

使用

grep

[--//--] | grep title

获取

一条线,其中包含单词标题 "title": "Postpone fetchEvents for main view after view appearing",

7

使用

剪切

[--//--] | cut -d'"' -f4

最后

我们只得到标题值: Postpone fetchEvents for main view after view appearing



1
投票

.git/config

文件中找到 github 远程部分。看起来像这样:


[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = [email protected]:a.git

现在将行 
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

添加到此部分。显然,更改 github url 以匹配您的项目的 URL。它最终看起来像这样:


[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = [email protected]:a.git fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

现在获取所有拉取请求:

$ git fetch origin From github.com:ja * [new ref] refs/pull/1000/head -> origin/pr/1000 * [new ref] refs/pull/1002/head -> origin/pr/1002 * [new ref] refs/pull/1004/head -> origin/pr/1004 * [new ref] refs/pull/1009/head -> origin/pr/1009

要查看特定的拉取请求:

$ git checkout pr/999 Branch pr/999 set up to track remote branch pr/999 from origin. Switched to a new branch 'pr/999'



0
投票

$ gh pr view 3 --repo OWNER/REPO --json 'title' --jq '.[]'

	
© www.soinside.com 2019 - 2024. All rights reserved.