从Bitbucket获取拉取请求id,并传递给Teamcity中的bash脚本

从Bitbucket获取拉取请求id,并传递给Teamcity中的bash脚本,bash,git,teamcity,bitbucket-server,Bash,Git,Teamcity,Bitbucket Server,我正在寻找一种从当前分支获取pull请求ID的方法,并将其传递给TeamCity内部的bash脚本 现在,我正在终端中手动传递pull请求ID,但我正在寻找一种方法来获取当前的pull请求ID,并通过TeamCity参数将其传递到这个Bash脚本中(我计划将此作为构建步骤运行)。Teamcity API似乎不支持获取请求ID #!/bin/bash branch=$(git symbolic-ref --short HEAD) repository="${PWD##*/}" mvn

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

现在,我正在终端中手动传递pull请求ID,但我正在寻找一种方法来获取当前的pull请求ID,并通过TeamCity参数将其传递到这个Bash脚本中(我计划将此作为构建步骤运行)。Teamcity API似乎不支持获取请求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=$1   Dsonar.stash.password.variable=SONAR_STASH_PASSWORD
我试过:

git ls-remote | grep refs/pull-requests/
但这将返回所有分支的所有请求


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

我不确定TeamCity是否知道拉取请求ID。这不太可能,因为构建是在提交而不是拉取请求上完成的,并且不需要拉取请求将结果发布回Bitbucket服务器。当然有很多方法可以获得你需要的信息

我能想到的第一个选择是使用Bitbucket服务器的Pull请求REST API(文档)。您需要http凭据来执行请求。我不确定TeamCity是否有一些可供您使用,但如果没有,则您将希望生成一个脚本,以便此脚本的访问受到限制

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
如果存在“提交次数”部分(在
~
之后的所有内容),您必须去掉它,并确定如果在该提交时有多个请求(name rev只会给您一个ref),您应该怎么做,但您不需要设置凭证,并且它确实适用于来自forks的PRs

另一个你必须考虑的方案是,可能还没有创建一个拉请求。


希望这有帮助

我不确定TeamCity是否知道拉取请求ID。这不太可能,因为构建是在提交而不是拉取请求上完成的,并且不需要拉取请求将结果发布回Bitbucket服务器。当然有很多方法可以获得你需要的信息

我能想到的第一个选择是使用Bitbucket服务器的Pull请求REST API(文档)。您需要http凭据来执行请求。我不确定TeamCity是否有一些可供您使用,但如果没有,则您将希望生成一个脚本,以便此脚本的访问受到限制

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
如果存在“提交次数”部分(在
~
之后的所有内容),您必须去掉它,并确定如果在该提交时有多个请求(name rev只会给您一个ref),您应该怎么做,但您不需要设置凭证,并且它确实适用于来自forks的PRs

另一个你必须考虑的方案是,可能还没有创建一个拉请求。


希望这有帮助

感谢克里斯蒂·休斯为我指明了正确的方向

我最终将脚本转换为Python。XXX是我在BitBucket服务器上的用户下设置的个人访问令牌,sonar.stash.password.variable=sonar\u stash\u 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)

感谢克里斯蒂·休斯为我指明了正确的方向

我最终将脚本转换为Python。XXX是我在BitBucket服务器上的用户下设置的个人访问令牌,sonar.stash.password.variable=sonar\u stash\u 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)

谢谢你的帮助!我最终使用了隐藏/比特桶api。Bash简而言之,因为我需要在json内部搜索,所以我正在用Python重写它。感谢您的帮助!我最终使用了隐藏/比特桶api。Bash简而言之,因为我需要在json内部搜索,所以我正在用Python重写它。