来自提交哈希的 GitHub 拉取请求 ID



如何从提交哈希中获取 GitHub PR ID?在 git 控制台或 GitHub API 中可能吗?

使用 GitHub API (V3( 绝对是可能的。若要获取 PR ID,需要使用 GitHub 搜索问题 API 并首先找到 PR 编号,然后可以使用 PR 编号查找 PR ID。请注意,拉取请求将具有拉取请求 ID 和单独的问题 ID。

例如,假设您有一个提交 sha -7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c。要从此 sha 中查找 PR ID,您可以执行以下操作:

第 1 步:使用提交 sha 查找 PR 编号:使用 Github 搜索 api-https://api.github.com/search/issues?q=sha:7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c.在 JSON 响应中,字段"数字"表示 PR 编号(在本例中为 16(,"ID"表示问题 ID(不是 PR ID(

步骤 2:使用 PR 编号和存储库详细信息查找 PR ID。从步骤 1 中收到的 JSON 响应中,我们可以构造以下内容 -https://api.github.com/repos/lamassu/lamassu-admin/pulls/16.在收到的 JSON 响应中,字段"ID"是必需的 PR ID。

我花了相当多的时间调查它,这是结果。该解决方案仅使用基本的 Git 功能,不需要任何 GitHub 功能。用于打印拉取请求 ID 的简单 sh 行:

git ls-remote origin 'pull/*/head' | grep -F -f <(git rev-parse HEAD) | awk -F'/' '{print $3}'

在Groovy Jenkinsfile中,我正在做以下工作:

def gitCommitSHA = sh(returnStdout: true, script: 'git rev-parse  HEAD').trim()
def allPRs = sh(returnStdout: true, script: "origin ‘pull/*/head’")
List result = allPRs.split( 'n' ).findAll { it.contains(gitCommitSHA) && it.contains("refs/pull") }
if (result.size() ==1 ){
def str = result[0]
def prId = str.substring(str.indexOf("pull")+5,str.lastIndexOf("head")-1)
echo "Pull request id: ${prId}"
}

例如,提交 SHA 是:7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c

  1. 转到"拉取请求"选项卡
  2. 在筛选器中输入:is:pr is:closed 7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c

如果 PR 未关闭,则删除is:closed

https://docs.github.com/en/rest/commits/commits#list-pull-requests-associated-with-a-commit

curl 
-H "Accept: application/vnd.github+json"  
-H "Authorization: token <TOKEN>" 
https://api.github.com/repos/OWNER/REPO/commits/COMMIT_SHA/pulls

使用 github-cli:gh api /repos/OWNER/REPO/commits/COMMIT_SHA/pulls | jq -r .[].html_url

最新更新