jenkins - 通过调用 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()
解决方案
推荐阅读
- c++ - 类模板部分特化中的转换
- android - 如何触发 build.gradle 并将新的依赖项下载到 gradle 缓存
- python - 如何将下面显示的 excel 函数转换为 python pandas 代码?
- delphi - 是否有可能找出连接是否被计量?
- python - 使用 Matplotlib 和 Python 绘制两个直方图
- python - 如何从 html 页面中排除内容并仅保留 html 标签?
- ios - UINavigationItem 在 UINavigationBar 后面
- apache-spark - 如何为具有多个分区的 Kafka 主题启动多个消费者?
- php - 无法在 NGNIX 的服务器端生成 pdf
- laravel - 调度操作后获取 vuex 存储状态