首页 > 解决方案 > 通过调用 api 获取私有 Bitbucket 分支的 groovy 脚本

问题描述

我想通过对 bitbucket 私有 repo 进行 api 调用,使用 groovy 脚本动态显示所有分支名称。我正在关注这篇文章链接。在这篇文章中,顶部使用的是 https,而不是我喜欢使用 ssh。

String baseUrl = "https://bitbucket.org/api"
String version = "1.0"
String organization = "i4niac"
String repository = "mirepoix"


String branchesUrl = [baseUrl, version, "repositories", organization, repository, "branches"].join("/")


String username = "i4niac"
String password = "mypassword"
// Create authorization header using Base64 encoding
String userpass = username + ":" + password;
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
// Create URL
URL url = branchesUrl.toURL()
// Open connection
URLConnection connection = url.openConnection()
// Set authorization header
connection.setRequestProperty ("Authorization", basicAuth)
// Open input stream
InputStream inputStream = connection.getInputStream()
// Get JSON output
def branchesJson = new groovy.json.JsonSlurper().parseText(inputStream.text)
// Close the stream
inputStream.close()

标签: jenkinsjenkins-groovybitbucket-server

解决方案


参考这个

根据 Bitbucket API 文档 -

要使用 REST API,您的应用程序将发出 HTTP 或 HTTPS 请求并解析响应,因此我非常怀疑您是否可以使用 ssh URL


推荐阅读