- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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/
可能是发布这个的错误地方,但我一直在摆弄异步http构建器,试图让基本的密码查询工作。它可以与 Http Builders 一起使用,但无法使其与异步版本一起使用。 @Grab(group='o
我正在尝试安装 HTTPBuilder像这样: @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder
我试图了解 HTTP Builder 的简化 GET。我通过类似于 REST GET 请求的简单 GET 请求成功了。 def client = new HTTPBuilder('http://pok
我正在尝试使用 Groovy HTTPBuilder 编写一个集成测试,该测试将验证正文中是否返回正确的错误消息以及 HTTP 409 状态消息。但是,我无法弄清楚如何在失败的情况下实际访问 HTTP
嗨,我想知道如何发布一个urlencoded字符串并使用HTTPBuilder读取xml响应?我想在Grails应用程序中使用它。 REST插件是不可选项。我尝试了在http://groovy.cod
我正在使用以下代码对外部系统执行 HTTP POST。问题是外部系统在使用下面的代码时总是得到一个“空”内容类型。有没有办法在使用 HTTPBuilder 时设置内容类型。 我尝试了执行相同请求的其他
以下代码不会对用户进行身份验证(没有发生身份验证失败,但由于缺乏权限导致调用失败): def remote = new HTTPBuilder("http://example.com") remote
我的httpbuilder帖子和json的响应处理遇到一些问题 在我的服务范围内,我有: def jsonDataToPost = '{"accountNumber" : ' + accNo + ',
我想与具有基于URL的API的Web服务进行通信。例如,我有以下URL: http://api.bla.com/aaa/bbb.ashx?Action=GetSecurityToken&vendorI
我试图使用httpBuilder在groovy中构建一个api使用者。我的后端api返回内容类型为application / json的json输出。但是,当我将httpbuilder对象中的内容类型
是 HTTPBuilder在 Grails 中线程安全吗? 如果 HTTPBuilder 连接到 Grails 服务类,使用它是否安全?还是应该在每次调用时实例化? 关于 Grails 中的 HTTP
为什么某些导入适用于项目中的常规 groovy 文件 compile group: 'org.codehaus.groovy.modules.http-builder', name: 'http-bu
我一直在使用HTTPBuilder作为获取站点 HTML 内容的一种方式。例如,这就是我使用它的方式: def http = new HTTPBuilder(url) def root = http.
def login(String email, String password, String apiKey) throws HttpResponseException { def postB
我需要通过代理才能到达我的目标服务器。看来我可以用这样的代码来做到这一点: def http = new HTTPBuilder( 'http://www.somesite.com') http.se
这个问题与 this question 密切相关.不同之处在于我想遵循模拟客户端的推荐方法。因此,我定义了以下 HTTPBuilder: protected readUrl() { def h
我正在尝试发出一个简单的 HTTP POST 请求,但我不知道为什么以下失败。我尝试按照示例 here ,而且我不明白我哪里出错了。 异常 java.lang.NullPointerException
我有一些 HTTPBuilder 代码的行为会有所不同,具体取决于我是否重用相同的 HTTPBuilder 对象来对同一个 REST 服务执行两个不同的请求: def http = new HTTPB
我需要下载并保存文件。我正在尝试使用 HTTPBuilder,因为它具有简单的 API 并且支持 cookie。我写了以下代码: //create new httpBuilder and set co
我有一个 NiFi StandardSSLContextService,它给了我一个自定义的 SSLContext。现在我想知道如何在 Groovy(通过 ExecuteScriptProcessor
我是一名优秀的程序员,十分优秀!