Automate a Jira Issue XML export - perhaps via a workflow transition

Gordon Rutherford April 19, 2021

Hi

Would anyone have any suggestions on the best way to do this? I am attempting to replace a legacy ticketing system with Jira. The legacy system generated an XML per ticket that could be read by a media asset manager (MAM) and create a pre defined folder structure in the MAM based on the XML.

Could this be achieved in ScriptRunner during a workflow transition perhaps?

Thanks

Gordon

 

 

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 22, 2021

I'm not familiar with the MAM or the XML structure, but sure... you could do that with scriptrunner.

As part of designing the code for this in the workflow post function, you would need to know all the Jira fields and custom fields that you want to map to the XML.

The best way to achieve that would be with the MarkupBuilder.

Something like this

import groovy.xml.MarkupBuilder
import com.atlassian.jira.component.ComponentAccessor
def customFields = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.mkp.xmlDeclaration(version:"1.0")
xml.root{
issuenode{
key{ xml.mkp.yield issue.key}
fields{
summary issue.summary
reporter issue.reporter.name
assignee issue.assignee.name
customFields.each{cf->
def val = cf.getValue(issue)
if(val){
"$cf.name"{xml.mkp.yield val}
}
}
}
}
}
writer.toString()

Will output an XML that looks like this

<?xml version='1.0'?> 
<root>
<issuenode>
<key>JSP-1922</key>
<fields>
<summary>blah blah</summary>
<reporter>xxx</reporter>
<assignee>xxx</assignee>
<My CustomField>the value</My CustomField>
<Another CF>value</Another CF>
<!-- etc for the rest of te custom fields -->
</fields>
</issuenode>
</root>

Obviously, you need to shape your code to output the right XML layout etc.

And then you need to decide what to do with that XML ... either store it to a local file or send it as an HTTP request to another system.

Gordon Rutherford April 23, 2021

Hi Peter-Dave

Really appreciate your time and effort to suggest this answer! It will take me some time to test with a developer but will update you on progress.

Thanks again

Regards

Gordon

Karin_Gleichauf December 12, 2022

Hi @Peter-Dave Sheehan ,

I created an automation rule that composes raw xml  as content of an e-mail as well as I can send the xml via webrequest. Is it possible to store the xml as well automatically in a local file? How to do so?

Best regards

Karin

0 votes
Gordon Rutherford December 13, 2022

Hi Karin

I actually achieved it after input from Adaptavist and a web session Q&A.

This script will place a word document on the local machine Jira is running on.

It is running as part of a custom script post function

Hope this helps

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.TrustedRequest
import com.atlassian.sal.api.net.TrustedRequestFactory
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import groovyx.net.http.URIBuilder

@PluginModule
TrustedRequestFactory trustedRequestFactory

final issueKey = 'issue.key'
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issueKey)
assert issue

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
def host = new URIBuilder(baseUrl).host
def xmlEndpointUrl = "/si/jira.issueviews:issue-xml/${issue.key}/${issue.key}.xml"

def request = trustedRequestFactory.createRequest(Request.MethodType.GET, "${baseUrl}$xmlEndpointUrl") as TrustedRequest
request.addTrustedTokenAuthentication(host, loggedInUser.username)
def result = request.execute()

log.warn result

new File("/tmp/${issue.key}.doc").withWriter { w ->
    w.write(result)
}

Suggest an answer

Log in or Sign up to answer