从 Bitbucket 获取 pull request id,并传递给 Teamcity 中的 bash 脚本

Get pull request id from Bitbucket, and pass to bash script in Teamcity

我正在寻找一种方法来从当前分支获取拉取请求 ID,并将其传递给 TeamCity 中的 bash 脚本。

现在我正在终端中手动传递拉取请求 ID - 但我正在寻找一种方法来获取当前拉取请求 ID,并通过 TeamCity 参数将其传递到此 Bash 脚本中(我计划 运行 这是一个构建步骤)。 TeamcityAPI好像不支持抓pull request ID

 #!/bin/bash
 branch=$(git symbolic-ref --short HEAD)

 repository="${PWD##*/}"


 mvn sonar:sonar -Dsonar.projectKey="$repository" -Dsonar.analysis.mode=issues -Dsonar.stash.comments.reset \
        -Dsonar.stash.notification=true -Dsonar.stash.project=datasmurfen-repos  Dsonar.stash.repository="$repository" \
        -Dsonar.stash.pullrequest.id=   Dsonar.stash.password.variable=SONAR_STASH_PASSWORD

我试过:

git ls-remote | grep refs/pull-requests/

但是这个 returns 所有分支的所有拉取请求。

有谁知道如何从 TeamCity 或 bash 获取拉取请求 ID,并将其传递到脚本中?

我不确定 TeamCity 是否知道拉取请求 ID。这不太可能,因为构建是在提交时完成的,而不是拉取请求,并且它不需要拉取请求 post 将结果返回到 Bitbucket Server。不过,当然有一些方法可以获得您需要的信息。

我能想到的第一个选项是使用 Bitbucket Server 的 Pull Request REST API(文档 here). You'd need http credentials to do the request. I'm not sure whether TeamCity has some available for you to use, but if it doesn't then you'll want to generate a personal access token 以便此脚本的访问受到限制。

curl -u username:password https://url.to.bitbucket/rest/api/latest/projects/<project_name>/repos/<repo_name>/pull-requests?state=OPEN&at=refs/heads/<branchname>&direction=OUTGOING

然后您必须解析 json 以从响应中获取拉取请求(也许像 Python 这样的方式比 bash 更容易)。另一个限制是它不会让你从分叉中获得 PR。

如果通过 REST API 执行此操作太困难(因为凭据和 json 解析),您可以使用 [=12] git 来完成=] 找到最近的拉取请求参考。这不太传统,我实际上还没有尝试这样做,所以我认为可能会有更多的边缘情况需要处理,因为 REST API 给了你一个非常直接的答案。

像这样:

> git fetch origin refs/pull-requests/*:refs/pull-requests/* # fetch all the pull-request refs
> git name-rev --name-only --refs="refs/pull-requests/*/from" HEAD # Find the pull request 'from' ref that is closest to the latest commit
pull-requests/11732/from~4^2

如果存在 'number of commits' 部分(~ 之后的所有内容),您必须删除它,并确定如果该提交有多个拉取请求该怎么做( name-rev 只会给你一个 ref) 但你不需要设置你的凭证,它确实适用于来自 forks 的 PR。

您必须考虑使用任一选项的另一种情况是,可能尚未创建拉取请求。

希望对您有所帮助!

感谢 Kristy Hughes 为我指明了正确的方向。

我最终将脚本转换为 Python。 XXX 是我在 BitBucket 服务器上为我的用户设置的个人访问令牌,sonar.stash.password.variable=SONAR_STASH_PASSWORD 它是指向 SonarQube 用户密码的本地环境变量。

#!/usr/bin/python3

import os
import requests
import subprocess

pwd =  os.getcwd()
repository = str(pwd).split("/")[-1]

getbranch = subprocess.run("git symbolic-ref --short HEAD", shell=True, stdout=subprocess.PIPE,
                    universal_newlines=True)
branch = getbranch.stdout

projectid = "myproject"

url = "https://x.mydomain.com/rest/api/latest/projects/{}/repos/{}/pull-requests".format( projectid, repository )

headers = { "Authorization": "Bearer {}".format("XXXXXX")}
r = requests.get(url, headers=headers)
print(r.json())

jsondict = r.json()
print (jsondict)

for project in jsondict["values"]:
    if project["fromRef"]["displayId"] == branch.strip():
            prid =  project["id"]
            print(prid)


buildproject = "mvn compile && git reset --hard HEAD"
 os.system(buildproject)         
print(buildproject)

buildanalysis = "mvn sonar:sonar -Dsonar.projectKey={} -Dsonar.analysis.mode=issues -Dsonar.stash.comments.reset -Dsonar.stash.notification=true -Dsonar.stash.project={} -Dsonar.stash.repository={} -Dsonar.stash.pullrequest.id={} -Dsonar.stash.password.variable=SONAR_STASH_PASSWORD".format( repository, projectid, repository, prid ) 
os.system(buildanalysis)
print(buildanalysis)