Jira macro get rendered html of description field

I swear I once saw in Atlassian docs that when requesting fields via Rest, one could add a “.html” or similar suffix to obtain the field value as rendered HTML, but can find that page anymore.

However, so far I always used the Jira Macro to get a list of issues, their key, priority, title etc and that works pretty well.

For a new task, I’d like to retrieve the description too, but I saw that adding “description” to the field list, the raw HTML is retrieved from Jira, not very useful esp. if the issue contains code blocks, images, and so forth.

Is there a way to get the rendered description?

I’m using the Jira Macro to ultimately embed a list of issues in a document to be sent externally by email.

Hi. Several ideas come to mind.

  1. You could implement a JIRAFieldDisplayer, see https://extensions.xwiki.org/xwiki/bin/view/Extension/JIRA%20Macro#HExtensibility
  2. You could use JIRA REST Integration (Extension.JIRA REST Integration) - XWiki (linked from JIRA Macro (XWiki.org)) to do whatever you want.

Hope it helps

Thank you. Going the Java way would require a lot of time, so I’d like to keep it as last option; I’m trying the wiki component option described there.

After the following changes, I got the example link working.

  1. I added the XObject properties to the same page where the Jira Macro is used, I don’t know if that makes any difference;

  2. the ComponentClass object had to have the scope set to Global instead of Current Wiki, otherwise it wouldn’t be applied, it seems (but this may be a side effect of #1)

  3. the new type usage {{jira ... fields="..., somefield:lien, ..."}}...{{/jira}} had to be changed to
    {{jira ... fields="..., somefield!lien, ..."}}...{{/jira}}, so using !, because : is to specify the field label

  4. the code block:

    def field = xcontext.method.input.get(0)
    def issue = xcontext.method.input.get(1)
    String text = xcontext.method.defaultDisplayer.displayField(field, issue).get(0).getProtectedString()
    

    to

    def field = xcontext.method.input.get(0)
    def issue = xcontext.method.input.get(1)
    def parameters = xcontext.method.input.get(2)
    String text = xcontext.method.defaultDisplayer.displayField(field, issue, parameters).get(0).getProtectedString()
    

    otherwise the following exception would come out:

    org.xwiki.component.wiki.WikiComponentRuntimeException: Failed to convert result [Failed to execute the [groovy] macro. Cause: [No signature of method: org.xwiki.contrib.jira.macro.internal.displayer.field.DefaultJIRAFieldDisplayer.displayField() is applicable for argument types: (org.xwiki.contrib.jira.macro.JIRAField, org.jdom2.Element) values: [id = [summary], label = [Summary], type = [lien], [Element: <item/>]] Possible solutions: displayField(org.xwiki.contrib.jira.macro.JIRAField, org.jdom2.Element, org.xwiki.contrib.jira.macro.JIRAMacroParameters)].
    

    I guess because the corresponding method has recently got a third parameter: jira/JIRAFieldDisplayer.java at master · xwiki-contrib/jira · GitHub

How do I pass text to the HTML macro before assigning it to xcontext.method.output.value?

def field = xcontext.method.input.get(0)
def issue = xcontext.method.input.get(1)
String text = xcontext.method.defaultDisplayer.displayField(field, issue).get(0).getProtectedString()

def reference = new ResourceReference(text, ResourceType.URL)
def labelBlocks = [new WordBlock("Lien")]
xcontext.method.output.value = [new LinkBlock(labelBlocks, reference, true)]

Any help on this?

Are you asking how to create a HTML macro from code?

If so, it’s new MacroBlock("html", ...).

Almost, I saw that the description field from Jira issues is already retrieved in HTML, hence I guess that I need to send it to the HTML macro before returning from the method code.

Copying from the examples in the extension page, I got that:

xcontext.method.output.value = [new WordBlock(text)]

renders like this:

immagine

But

xcontext.method.output.value = [new MacroBlock("xwiki-rendering-syntax-html5", [:], "<div>Hello!</div>", true)]

Renders to nothing (I see an empty string) - I actually do not recall from where I got that string…

immagine

I tried “html” too:

xcontext.method.output.value = [new MacroBlock("html", [:], text, true)]

But again that renders to nothing.

Actually I’m not sure that the macro transformation is executed for wiki components.

Just in case it isn’t, you can instead use a RawBlock: new RawBlock(text, Syntax.XHTML_1_0) (for example).

1 Like

Yes! That worked!

immagine

Images in Jira issues do not get rendered, because they generate a 302 to Jira authentication page (which makes sense), do you know if there’s any setting in Jira or way to attach images or anything that can help with this?

Indeed, not easy if your jira instance is protected. You could ask on the jira support forums if there’s a way to get the image embedded in the returned content (using Data URI for example).

An alternative (complex) would be to parse the returned HTML to find the URLs and then issue several HTTP requests (using the jira credentials provided) to download the images and replace the <img> URL attribute to be something like <img src="data:image/gif;base64,.... However, this will be complex, costly and slow IMO.

Yet another option is to parse the HTML to find img tags and modify the URLs to add some auth tokens (Manage API tokens for your Atlassian account | Atlassian Support). But again, complex, costly and slow.

Thanks for the support and the advices :slight_smile:

May I edit the extension page to change the example as per my above post?

sure, go ahead! Thanks :slight_smile: