无法为 groovy 函数 Get 请求构建正确的 header
Not able to construct proper header for groovy function Get request
在这里为 Groovy 本机库和 运行 编写我的第一个函数成为一个问题。一种为 pull-requests 获取 Github 标签的方法。 @param github_token 具有访问和读取 PR 信息权限的字符串令牌。
getLabelsPerPullRequest.groovy: 19: expecting '}', found ':' @ line 19, column 28. 'Authorization': 'token '+ github_token, ^
这是我的函数
import groovy.json.JsonSlurper
def getLabelsPerPullRequest(String github_token) {
def labels
def scmHead = jenkins.scm.api.SCMHead.HeadByItem.findHead(currentBuild.rawBuild.getParent())
def repo = scmHead.getSourceRepo()
def prId = scmHead.getId()
if(github_token && github_token != null) {
// Set the call headers with Oauth token.
def headers = "{'Authorization': 'token '+ ${github_token},'Accept': 'application/vnd.github.v3+json'}"
// Construct request number URL in Github
def pr_url = "https://github.optum.com/api/v3/repos/SOP-Bot/${repo}/pulls/${prNbr}"
def json = sh(
script: "curl -X Get ${pr_url} -H ${headers}",
returnStdout: true
)
def prInfo = JsonOutput.toJson(text: json)
labels = prInfo.labels
}
return labels
}
每个 header 都需要是一个单独的 -H 参数。你可以这样做。我在 curl 中添加了 -s,因为您可能不想要额外的进度输出,但这实际上可能不是问题。
def headers = [
"-H 'Authorization: token ${gitub_token}'",
"-H 'Accept: application/vnd.github.v3+json'"
]
...
def json = sh(
script: "curl -s -X GET ${headers.join(' ')} '${pr_url}'",
returnStdout: true
)
在这里为 Groovy 本机库和 运行 编写我的第一个函数成为一个问题。一种为 pull-requests 获取 Github 标签的方法。 @param github_token 具有访问和读取 PR 信息权限的字符串令牌。
getLabelsPerPullRequest.groovy: 19: expecting '}', found ':' @ line 19, column 28. 'Authorization': 'token '+ github_token, ^
这是我的函数
import groovy.json.JsonSlurper
def getLabelsPerPullRequest(String github_token) {
def labels
def scmHead = jenkins.scm.api.SCMHead.HeadByItem.findHead(currentBuild.rawBuild.getParent())
def repo = scmHead.getSourceRepo()
def prId = scmHead.getId()
if(github_token && github_token != null) {
// Set the call headers with Oauth token.
def headers = "{'Authorization': 'token '+ ${github_token},'Accept': 'application/vnd.github.v3+json'}"
// Construct request number URL in Github
def pr_url = "https://github.optum.com/api/v3/repos/SOP-Bot/${repo}/pulls/${prNbr}"
def json = sh(
script: "curl -X Get ${pr_url} -H ${headers}",
returnStdout: true
)
def prInfo = JsonOutput.toJson(text: json)
labels = prInfo.labels
}
return labels
}
每个 header 都需要是一个单独的 -H 参数。你可以这样做。我在 curl 中添加了 -s,因为您可能不想要额外的进度输出,但这实际上可能不是问题。
def headers = [
"-H 'Authorization: token ${gitub_token}'",
"-H 'Accept: application/vnd.github.v3+json'"
]
...
def json = sh(
script: "curl -s -X GET ${headers.join(' ')} '${pr_url}'",
returnStdout: true
)