gpt4 book ai didi

java - 为什么 httpbuilder 没有给出准确的 JSON 输出?

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:18 25 4
gpt4 key购买 nike

我正在尝试使用 gradle 脚本从 JIRA 获取项目 ID。我编写了一些方法,基本上尝试获取 JSON 输出并获取该特定项目的项目 ID,但现在我无法从中获取项目 ID。下面是我用来获取 JSON 结果的方法。它工作正常,但没有给我确切的 JSON 输出。我的项目名称是 TESTPROJECT,id 是 12345。我正在寻找当您有新项目并且需要使用 JIRA 请求方法获取该项目的项目 ID 时。我粘贴了下面的 JSON 输出,它没有 {} ""所以不完全是 JSON 输出。有人可以告诉我如何使用下面的方式获取项目 id 还是我做错了什么?

buildscript {
repositories {
mavenLocal()
mavenCentral()

}
dependencies {
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
classpath 'org.apache.httpcomponents:httpmime:4.2.1'
classpath 'commons-io:commons-io:2.4'
}
}

ext {
jiraURL = "https://test.test.com/"
jiraProject = 'TESTPROJECT'
jiraUser = "test"
jiraPassword = "*****"
}


import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType

def String authHeader() {
String userAndPassword = "${jiraUser}:${jiraPassword}"
String authHeader = 'Basic ' + userAndPassword.getBytes().encodeBase64().toString()
return authHeader
}

/*
* Get the Jira project details
*/
def Map getJiraprojectDetails(String projectName) {
Map json = jiraProjectRequest(
"project/${projectName}",
groovyx.net.http.Method.GET,
null,
'Get JIRA version failure - getJiraprojectDetails()'
)
existingProjectId = json.key.id[0]

if (existingProjectId == null || existingProjectId == 0)
throw new GradleException("Project was not found in JIRA")

return existingProjectId
}
def Map jiraProjectRequest(String path, groovyx.net.http.Method method, Map jsonBody, String failMessage) {
Map jsonResult = null
def jira = new HTTPBuilder("${jiraURL}/rest/api/2/${path}", ContentType.JSON)
jira.request(method) { req ->
headers.'Authorization' = authHeader()
requestContentType = ContentType.JSON
if (body != null)
body = jsonBody

response.success = { resp, json ->
println "$json"
jsonResult = json
println "$jsonResult"
}

response.failure = { resp ->
String message = "${failMessage}: ${resp.status} - ${resp.statusLine.reasonPhrase}"
throw new GradleException(message)
}
}

return jsonResult
}

def void makeNewversion() {
def projectName = "${jiraProject}"
println "$projectName"
projectid = getJiraprojectDetails(projectName)
}


task createJiraVersion() {
doLast {
if (project.hasProperty('createVersion')) {
makeNewversion()
}
}
}

JSON 输出

[jira] Response data: -----
[jira] [expand:description,lead,url,projectKeys, self:https://test.test1.com/rest/api/2/project/48352, id:48352, key:TESTPROJECT, description:, lead:[self:https://test.test1.com/rest/api/2/user?username=C56765, key:C56765, name:C56765, avatarUrls:[48x48:https://test.test1.com/secure/useravatar?ownerId=C56765&avatarId=42213, 24x24:https://test.test1.com/secure/useravatar?size=small&ownerId=C56765&avatarId=42213, 16x16:https://test.test1.com/secure/useravatar?size=xsmall&ownerId=h156765&avatarId=42213, 32x32:https://test.test1.com/secure/useravatar?size=medium&ownerId=C56765&avatarId=42213], displayName:karry test, active:true], components:[], issueTypes:[[self:https://test.test1.com/rest/api/2/issuetype/1, id:1, description:A problem which impairs or prevents the functions or performance of the product or its related artifacts. It can be related to software, hardware or both., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31313&avatarType=issuetype, name:Defect, subtask:false, avatarId:31313], [self:https://test.test1.com/rest/api/2/issuetype/66, id:66, description:Created by JIRA Software - do not edit or delete. Issue type for a big user story that needs to be broken down., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31317&avatarType=issuetype, name:Epic, subtask:false, avatarId:31317], [self:https://test.test1.com/rest/api/2/issuetype/67, id:67, description:Created by JIRA Software - do not edit or delete. Issue type for a user story., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31325&avatarType=issuetype, name:Story, subtask:false, avatarId:31325], [self:https://test.test1.com/rest/api/2/issuetype/10600, id:10600, description:, iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31324&avatarType=issuetype, name:Initiative, subtask:false, avatarId:31324], [self:https://test.test1.com/rest/api/2/issuetype/2, id:2, description:A new feature of the product., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31321&avatarType=issuetype, name:New Feature, subtask:false, avatarId:31321], [self:https://test.test1.com/rest/api/2/issuetype/4, id:4, description:An enhancement to an existing feature., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31320&avatarType=issuetype, name:Improvement, subtask:false, avatarId:31320], [self:https://test.test1.com/rest/api/2/issuetype/25, id:25, description:A formal request to change an existing, baselined project artifact. , iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31330&avatarType=issuetype, name:Change Request, subtask:false, avatarId:31330], [self:https://test.test1.com/rest/api/2/issuetype/3, id:3, description:A task that needs to be done., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31328&avatarType=issuetype, name:Task, subtask:false, avatarId:31328], [self:https://test.test1.com/rest/api/2/issuetype/26, id:26, description:A risk is an uncertain future event or condition, with a probability of occurrence, and a potential for loss., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31318&avatarType=issuetype, name:Risk, subtask:false, avatarId:31318], [self:https://test.test1.com/rest/api/2/issuetype/87, id:87, description:An impediment is something that makes it difficult to do or complete something in a project and thus requires action. It may be a project risks that has occurred and typically represents something upon which a decision and actions are needed. The decision may not necessarily change the scope, schedule or cost of the project but the lack of a decision would affect the schedule., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31318&avatarType=issuetype, name:Impediment, subtask:false, avatarId:31318], [self:https://test.test1.com/rest/api/2/issuetype/8, id:8, description:An item discussed in a meeting that requires further action or work., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31328&avatarType=issuetype, name:Action Item, subtask:false, avatarId:31328], [self:https://test.test1.com/rest/api/2/issuetype/83, id:83, description:, iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31314&avatarType=issuetype, name:Root Cause Analysis, subtask:false, avatarId:31314], [self:https://test.test1.com/rest/api/2/issuetype/58, id:58, description:Any issue, solution or improvement learned in a project that should be shared with other people and/or projects., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31324&avatarType=issuetype, name:Lesson Learned, subtask:false, avatarId:31324], [self:https://test.test1.com/rest/api/2/issuetype/39, id:39, description:A summary of a meeting including participants, decisions, and resulting actions., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=39910&avatarType=issuetype, name:Meeting Minutes, subtask:false, avatarId:39910], [self:https://test.test1.com/rest/api/2/issuetype/55, id:55, description:An item for managing a review consisting of one or more review findings., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31323&avatarType=issuetype, name:Review, subtask:false, avatarId:31323], [self:https://test.test1.com/rest/api/2/issuetype/43, id:43, description:Used to track and manage Process Compliance and Configuration Audits on a project., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=42820&avatarType=issuetype, name:Audit, subtask:false, avatarId:42820], [self:https://test.test1.com/rest/api/2/issuetype/63, id:63, description:Used for requesting the approval of a particular work product and collecting approval from multiple individuals., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31316&avatarType=issuetype, name:Approval Request, subtask:false, avatarId:31316], [self:https://test.test1.com/rest/api/2/issuetype/5, id:5, description:A task that needs to be done., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31328&avatarType=issuetype, name:Sub-Task, subtask:true, avatarId:31328], [self:https://test.test1.com/rest/api/2/issuetype/35, id:35, description:An item discussed in a meeting that requires further action or work., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31328&avatarType=issuetype, name:Action Item (Sub-Issue), subtask:true, avatarId:31328], [self:https://test.test1.com/rest/api/2/issuetype/56, id:56, description:A defect, question, suggestion or other issue resulting from the review of a document or other artifact. , iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31323&avatarType=issuetype, name:Review Finding, subtask:true, avatarId:31323], [self:https://test.test1.com/rest/api/2/issuetype/62, id:62, description:, iconUrl:https://test.test1.com/images/icons/issuetypes/documentation.png, name:Mitigation Plan, subtask:true], [self:https://test.test1.com/rest/api/2/issuetype/61, id:61, description:An issue (non-compliance, recommendation) found during an audit., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31323&avatarType=issuetype, name:Audit Finding, subtask:true, avatarId:31323], [self:https://test.test1.com/rest/api/2/issuetype/64, id:64, description:A sub-issue type used for capturing the approval (or disapproval) of an individual for a particular work product., iconUrl:https://test.test1.com/secure/viewavatar?size=xsmall&avatarId=31316&avatarType=issuetype, name:Approval, subtask:true, avatarId:31316]], assigneeType:PROJECT_LEAD, versions:[[self:https://test.test1.com/rest/api/2/version/98423, id:98423, name:TCD 1.2.3, archived:false, released:false, projectId:48352]], name:Test PAAS, roles:[CCB:https://test.test1.com/rest/api/2/project/48352/role/10020, L1 Support:https://test.test1.com/rest/api/2/project/48352/role/10050, Developers:https://test.test1.com/rest/api/2/project/48352/role/10001, Approvers:https://test.test1.com/rest/api/2/project/48352/role/10052, Administrators:https://test.test1.com/rest/api/2/project/48352/role/10002, Watcher:https://test.test1.com/rest/api/2/project/48352/role/10030, Bulkcloners:https://test.test1.com/rest/api/2/project/48352/role/10070, Testers:https://test.test1.com/rest/api/2/project/48352/role/10010, Users:https://test.test1.com/rest/api/2/project/48352/role/10000, L2 Support:https://test.test1.com/rest/api/2/project/48352/role/10051], avatarUrls:[48x48:https://test.test1.com/secure/projectavatar?avatarId=39413, 24x24:https://test.test1.com/secure/projectavatar?size=small&avatarId=39413, 16x16:https://test.test1.com/secure/projectavatar?size=xsmall&avatarId=39413, 32x32:https://test.test1.com/secure/projectavatar?size=medium&avatarId=39413], projectCategory:[self:https://test.test1.com/rest/api/2/projectCategory/18032, id:18032, name:Solutions, description:TO-12179], projectTypeKey:software]

最佳答案

下面是获取项目id的代码

/*
* Get the Jira project ID for a project like 'ABC'
*/
def int getJiraProjectid(String jiraProject) {

int existingProjectId = 0

def json = [jiraprojectRequest()]
json.each {
existingProjectId = it."id".toInteger()
//println "$existingVersionId"
}
if (existingProjectId == 0)
throw new GradleException("Existing project ${jiraProject} was not found in JIRA")

return existingProjectId
}

def Map jiraprojectRequest() {
Map jsonResultList = null
def jira = new HTTPBuilder("${jiraURL}/rest/api/2/project/${jiraProject}", ContentType.JSON)
jira.request(groovyx.net.http.Method.GET) { req ->
headers.'Authorization' = authHeader()

response.success = { resp, json ->
project.getLogger().info('[jira] Response data: -----')
project.getLogger().info("[jira] $json")
project.getLogger().info('\n--------------------')
jsonResultList = json
}

response.failure = { resp ->
throw new GradleException("Get JIRA versions failure - jiraGetVersions(): ${resp.status} - ${resp.statusLine.reasonPhrase}")
}
}
return jsonResultList
}

关于java - 为什么 httpbuilder 没有给出准确的 JSON 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47819511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com