我正在编写一个持续集成步骤,在允许合并之前检查拉取请求的标题是否有正确的标签和格式。
为此,我需要在给出 PR 编号时回显 Pull 请求的标题。有没有办法简单地在命令行中执行此操作?
根据@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 发行版,这应该不难..
对于拉取请求使用
https://api.github.com/repos/randombit/botan/pulls/359
对于拉取请求中的补丁,请在
Subject:
url 中搜索 .patch
:
https://github.com/randombit/botan/pull/359.patch
请注意,您只能在 Github API 上执行每小时 60 个请求和 IP。
谢谢凯文·克莱泽。 我的解决方案版本没有 jq 且只有 last tag 中的 current commits。
代替
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
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
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
空格字符将将字符串分成几部分,并获得第四个值
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
中使用 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 title
一条线,其中包含单词标题:
"title": "Postpone fetchEvents for main view after view appearing",
7
:
[--//--] | cut -d'"' -f4
我们只得到标题值:
Postpone fetchEvents for main view after view appearing
在
.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'
$ gh pr view 3 --repo OWNER/REPO --json 'title' --jq '.[]'