windows - windows Jenkins上的groovy命令curl
问题描述
我有一个适用于 Linux Jenkins 的 groovy 脚本
import groovy.json.JsonSlurper
try {
List<String> artifacts = new ArrayList<String>()
//jira get summery for list by issue type story and label demo and project 11411
def artifactsUrl = 'https://companyname.atlassian.net/rest/api/2/search?jql=project=11411%20and%20issuetype%20in%20(Story)%20and%20labels%20in%20(demo)+&fields=summary' ;
def artifactsObjectRaw = ["curl", "-u", "someusername@xxxx.com:tokenkey" ,"-X" ,"GET", "-H", "Content-Type: application/json", "-H", "accept: application/json","-K", "--url","${artifactsUrl}"].execute().text;
def parser = new JsonSlurper();
def json = parser.parseText(artifactsObjectRaw );
//insert all result into list
for(item in json.issues){
artifacts.add( item.fields.summary);
}
//return list to extended result
return artifacts ;
}catch (Exception e) {
println "There was a problem fetching the artifacts " + e.message;
}
该脚本通过 API 返回来自 Jira 作业的所有名称,但是当我尝试在 Windows Jenkins 上运行这个 groovy 时,该脚本将不起作用,因为 Windows 没有命令 curl
def artifactsObjectRaw = [" curl ", "-u","someusername@xxxx.com:tokenkey" ,"-X" ,"GET", "-H", "Content-Type: application/json", "-H ", "accept: application/json","-K","--url","${artifactsUrl}"].execute().text;
我应该如何执行这个命令?
解决方案
以下代码:
import groovy.json.JsonSlurper
try {
def baseUrl = 'https://companyname.atlassian.net'
def artifactsUrl = "${baseUrl}/rest/api/2/search?jql=project=MYPROJECT&fields=summary"
def auth = "someusername@somewhere.com:tokenkey".bytes.encodeBase64()
def headers = ['Content-Type': "application/json",
'Authorization': "Basic ${auth}"]
def response = artifactsUrl.toURL().getText(requestProperties: headers)
def json = new JsonSlurper().parseText(response)
// the below will implicitly return a list of summaries, no
// need to define an 'artifacts' list beforehand
def artifacts = json.issues.collect { issue -> issue.fields.summary }
} catch (Exception e) {
e.printStackTrace()
}
是纯粹的 groovy,即不需要 curl。它从 jira 实例中获取项目并返回List<String>
摘要。由于我们不想要任何像 HttpBuidler 这样的外部依赖项(就像你从 jenkins 做的那样),我们必须手动进行基本的身份验证编码。
脚本测试(连接和获取 json 部分,没有测试summary
字段的提取):
Groovy Version: 2.4.15 JVM: 1.8.0_201 Vendor: Oracle Corporation OS: Linux
针对 atlassian 按需云实例。
我删除了您的 jql 查询,因为它对我不起作用,但您应该能够根据需要将其添加回来。
推荐阅读
- javascript - 服务人员不会跳过等待状态
- adaptive-cards - Microsoft Bot Framework 自适应卡中是否有轮播卡的示例架构
- python - 如何根据 Python 中数据集的最佳拟合确定微分方程的未知参数?
- swiftui - 忽略奇异矩阵,我应该关注/忽略 SwiftUI 中的这个控制台按摩吗?
- php - 为图表格式化 Google 电子表格 JSON 响应
- oracle - 'ORA-00934: group function is not allowed here' 在创建过程时
- javascript - 页面加载后,延迟 2 秒后重新加载一次页面
- python - 如何创建自动为引用创建标签的 Sphinx 角色?
- sql - 查询包含参数但导入文件包含不同的值 [将 csv 导入 Teradata SQL]
- django - 如何在 Django 中呈现 HTML 注释标签