简体   繁体   English

在groovy中使用JIRA Rest API以HTML格式创建发行说明

[英]Create Release Notes using JIRA Rest API in HTML format in groovy

I am working on a script where I need to create the release notes using JIRA REST API in HTML format for any project.The below four field should come in that release notes. 我正在编写一个脚本,该脚本需要使用JIRA REST API以HTML格式为任何项目创建发行说明。该发行说明中应包含以下四个字段。

Issue Key Module Summary Release Note 发行关键模块摘要发行说明

I am trying the below code but it is giving me only the issue Key field but need all other fields as well and in html file.Could you please suggest me on this? 我正在尝试下面的代码,但它只给我问题Key字段,但也需要所有其他字段以及html文件。您能建议我吗?

Issue:1 Initially it was giving me the output in below format: 问题:1最初,它以以下格式给我输出:

NTTB-2141
NTTB-2144
NTTB-2140

But now it is giving me the output json fromat way. 但是现在它给了我atat方法的输出json。

Code which I am trying from the groovy console: 我从groovy控制台尝试的代码:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
import groovyx.net.http.RESTClient

final String USAGE =
  "Usage: -Djira.username=xxx -Djira.password=xxx -Djira.fixVersion=1.0"

String jiraUsername = 'ABCDEF'
String jiraPassword = '********'
String jiraFixVersion = '3.8.101'

println "Getting issues..."
if (!jiraUsername?.trim()) {
    fail("Empty property: jira.username " + USAGE)
}

if (!jiraPassword?.trim()) {
    fail("Empty property: jira.password " + USAGE)
}

if (!jiraFixVersion?.trim()) {
     fail("Empty property: jira.fixVersion " + USAGE)
}

final String JIRA_SEARCH_URL = "https://jira.test.com/rest/api/latest/"
// see JIRA docs about search:
// https://docs.atlassian.com/jira/REST/latest/#idp1389824
String JQL = "project = NCCB"
JQL += " AND issuetype in standardIssueTypes()"
JQL += " AND status in (Resolved, Closed)"
JQL += " AND fixVersion = \"${jiraFixVersion}\""

def jira = new RESTClient(JIRA_SEARCH_URL)

def query = [:]
query['os_username'] = jiraUsername
query['os_password'] = jiraPassword
query['jql'] = JQL
query['startAt'] = 0
query['maxResults'] = 1000

try {
    def resp = jira.get(path: "search",
                        contentType: "application/json",
                        query: query)
    assert resp.status == 200
    assert (resp.data instanceof net.sf.json.JSON)
    resp.data.issues.each { issue ->
        println issue.key
    }
    println "Total issues: " + resp.data.total
} catch (groovyx.net.http.HttpResponseException e) {
    if (e.response.status == 400) {
        // HTTP 400: Bad Request, JIRA JQL error
        fail("JIRA query failed: ${e.response.data}", e)
    } else {
        fail("Failure HTTP status ${e.response.status}", e)
    }

}

I suspect the code is right, except for that assertion: 我认为该代码是正确的,除了该断言:

assert (resp.data instanceof net.sf.json.JSON)

I get a groovy.json.internal.LazyMap (maybe you have changed versions of Groovy or Jira or something). 我得到了一个groovy.json.internal.LazyMap (也许您已经更改了Groovy或Jira的版本)。

As a result, the assertion fails and Groovy tries to be helpful by giving you a comparison... but it shows you the toString() of the result, which is a huge mess of maps. 结果,断言失败,并且Groovy尝试通过给您一个比较来提供帮助...但是它向您显示了结果的toString() ,这是一堆巨大的地图。

If you remove that assertion, it works for me, and I suspect it will work for you too. 如果删除该断言,它将对我有用,我怀疑它也将对您有用。

Edit: huh... you cannot literally take "all" data and print to html. 编辑:呵呵...您不能从字面上获取“所有”数据并打印为html。 You will have to select the properties you need, and those depend on your Jira configuration. 您将必须选择所需的属性,这些属性取决于您的Jira配置。 Here is an example with only 2 properties that should be universal: 这是一个只有两个应为通用属性的示例:

def resp = jira.get(path: "search",
                    contentType: "application/json",
                    query: query)
assert resp.status == 200
def output = new File('issues.html')
output << "<html><body><ul>"
resp.data.issues.each { issue ->
    def url = "https://yourjirainstance/browse/${issue.key}"
    output << "<li><a href=\"$url\">${issue.key}</a>: ${issue.fields.summary}</li>"
}
output << "</ul></body></html>"
println "Exported ${resp.data.total} issues to ${output.name}"

See here details about what the service will give you. 在此处查看有关该服务将为您提供什么的详细信息。

If you just want an HTML dump, maybe the REST API is not what you want: you can also ask Jira to export results of JQL as a printable output (that will actually be html). 如果只需要HTML转储,也许REST API不是您想要的:您还可以要求Jira将JQL的结果导出为可打印的输出(实际上是html)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM