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

Dan*_*ott 5 git terminal github jenkins ghprb

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

为此,我需要在给定PR号时回显请求请求的标题。有没有办法在命令行中简单地做到这一点?

Kev*_*ser 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
Run Code Online (Sandbox Code Playgroud)

你需要curljq在你的机器上可用,但如果你有一个合理的 Linux 发行版,这应该不难。


Xav*_*hot 5

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

gh pr view 3 --repo OWNER/REPO | head -n 1
Run Code Online (Sandbox Code Playgroud)

在哪里:

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

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


Cod*_*ard 1

如果您使用的是 github:

在文件中找到 github 远程的部分.git/config。它看起来像这样:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:a.git
Run Code Online (Sandbox Code Playgroud)

现在将该行添加fetch = +refs/pull/*/head:refs/remotes/origin/pr/*到此部分。显然,更改 github url 以匹配您的项目的 URL。它最终看起来像这样:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:a.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
Run Code Online (Sandbox Code Playgroud)

现在获取所有拉取请求:

$ 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
Run Code Online (Sandbox Code Playgroud)

要检查特定的拉取请求:

$ git checkout pr/999
Branch pr/999 set up to track remote branch pr/999 from origin.
Switched to a new branch 'pr/999'
Run Code Online (Sandbox Code Playgroud)