Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Get the html view of description with script runner

Dan27
Contributor
October 15, 2018

Hello,

I need to print the html data of the issue description..

When I printed the description (issue.getDescription()) , I got this:

h5. *+General OS / DB / Browser / Machine Information:+* h3.  bla bla bla *+Scenario Description and steps to recreate the problem:+* h3.  bla bla *+Actual & Expected Results+* h3.  bla *+Log file information:+* h3.  Daniel Test *+Additional info:+* h3.  

How can I print this as html.?

 

Thanks,

Daniel

1 answer

1 accepted

1 vote
Answer accepted
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2018

Hi,

Using the REST API you can do it.

so you need to get your issue data via REST API, like this:

/rest/api/latest/issue/{issuekey}?expand=renderedFields

The important part is the "?expand=renderedFields"

In the JSON response that you will get, you will have "renderedFields" entity, all rich text (wiki render) fields will be there rendered to HTML.

So just call the REST API from your ScriptRunner groovy script as I mentioned

Dan27
Contributor
October 15, 2018

Thank you @Nir Haimov ,

Where can I put this link ? I never used REST API ..

 

Thanks.

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2018

I will guide how to use REST API.

Do you have Jira server or Cloud?

here you want to run the script? as a post-function in your workflow?

Dan27
Contributor
October 15, 2018

No, I would like to run it on script listener.

I have Jira server.

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2018

So in your script listener,

Write this:

//get the base url of Jira
def baseUrl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl");
//rest api request
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(baseUrl + "/rest/api/latest/issue/" + issue.getId() + "?expand=renderedFields");
Credentials credentials = new UsernamePasswordCredentials("username","passwrod");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, credentials);
client.executeMethod(method);
def response = method.getResponseBodyAsString()
method.releaseConnection();

"response" now hold the JSON data you got from the REST API.

Go through the JSON and find "description" field inside "renderedFields".

The value there is by HTML

Dan27
Contributor
October 15, 2018 edited

Thanks! @Nir Haimov

Which imports do I need?

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2018
import com.atlassian.jira.component.ComponentAccessor
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.*;
Dan27
Contributor
October 16, 2018 edited

I got error with those logs:

2018-10-16 07:01:43,102 WARN [common.UserScriptEndpoint]: Script console script failed: java.net.UnknownHostException: jira.wla.bmc.com at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:117) at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:157) at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707) at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387) at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323) at org.apache.commons.httpclient.HttpClient$executeMethod$1.call(Unknown Source) at Script2797.run(Script2797.groovy:43)

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 16, 2018

I'm not sure of the error...

Maybe it's related to security/policy or some other company restrictions?

Dan27
Contributor
October 16, 2018

@Nir Haimov ,

I tried everything.. I used baseurl before and it worked..

Any other help?

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 16, 2018 edited

I don't think the "baseurl" is the issue...

It makes more sense that the issue the REST API method, is there any chance you are blocked from performing REST API calls on your server?

Something your IT/System/Security department did to your servers?

 

I'm adding one of my champion colleges, maybe he will know...

@Alexey Matveev Any idea?

@Alexey Matveev

@Alexey Matveev

Dan27
Contributor
October 16, 2018

Thank you very much for your help @Nir Haimov !

I'm trying to understand it now..

Dan27
Contributor
October 17, 2018

Hi @Nir Haimov ,

We had problems with SLL . Now I succeed to get the JSON and found there the html data. How can I take it?

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 17, 2018 edited

Hi Daniel,

I'm Happy the problem solved.

As i assumed it was SSL problem from your side and not something related to the code.

Any way, from Jira side i gave you the solution.

 

Sorry but i can't teach you how to code...

As this is general question of how to read and use JSON.

I suggest you will use some google for this.

Dan27
Contributor
October 17, 2018

@Nir Haimov , I don't know how to work with JSON.. I only need to get the HTML description.. that was my question from the beginning..

I will be happy if you can write me the line.

 

Thanks.

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 17, 2018

add the following to import:

import groovy.json.*

Add this line after all the code i sent you:

 def parsed = new JsonSlurper().parseText(response)

parsed.renderedFields.description
Basit Rehman
Contributor
January 30, 2019

Hi @Nir Haimov,

I also have some problems calling the REST API and I am not sure if I should create a new question or if it is ok to ask in this thread :D

Anyhow, I created a REST endpoint with scriptrunner that calls the REST API because I need the renderedFields.

My problem are now the credentials. Is it possible to get the credentials of the logged in user or do I have to put username and password in the script. The script will be called by multiple users later so I was wondering if this is somehow possible. Thank you.

Best regards, 

Basit

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 30, 2019

Hi @Basit Rehman,

Please open a new question :)

Mention me there :)

Basit Rehman
Contributor
February 1, 2019

Hi,

I have posted a new question and tagged you there. Thanks! :)

Suggest an answer

Log in or Sign up to answer