unknown
unknown

Reputation: 1913

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.

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?

Issue:1 Initially it was giving me the output in below format:

NTTB-2141
NTTB-2144
NTTB-2140

But now it is giving me the output json fromat way.

Code which I am trying from the groovy console:

@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)
    }

}

Upvotes: 0

Views: 1824

Answers (1)

Hugues M.
Hugues M.

Reputation: 20477

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).

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.

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. You will have to select the properties you need, and those depend on your Jira configuration. 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).

Upvotes: 1

Related Questions