3

I am trying to get the description of an issue from JIRA to put it in a Confluence Storage Format template in order to create a page in Confluence. But I could't find a way to render the description raw data to a storage format recognizable format. Here is a concrete example : For an issue in JIRA with following description :

enter image description here

The description string I get by calling com.atlassian.jira.issue.Issue.getDescription() is :

{color:#14892c}Recently Updated{color}
h1. *_As you and your team create content this area will fill up and display the latest updates._*

If I don't make it wrong, the string I got is its wiki template representation. Insert it directly in Storage format will not be recognized by the template engine so will not be properly rendered.

I have tried using <ac:rich-text-body> to enclose the string but it doesn't work. Seems to I have to convert the wiki representation to HTML, or XHTML. How can I achieve this in Java code?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
qingl97
  • 327
  • 4
  • 14

2 Answers2

5

To convert the JIRA wiki markup to HTML rendered output from JIRA:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.ComponentManager;

public String renderWikiMarkup(Issue issue) {
    RendererManager rendererManager = ComponentManager.getComponent(RendererManager.class);
    JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");
    String output = renderer.render(issue.description, issue.getIssueRenderContext());
    return output;
}
qingl97
  • 327
  • 4
  • 14
  • Thanks the information! This was a good place to start. When I display this to the console, I get a correctly formatted link. However, when I make an object which takes exactly the same content and is sent to a velocity template, it produces an link which looks like `[1]link title ---------------------------------------------------------------------------------------- [1] http://example.com`. I assume something changes about the IssueRendererContext when it gets passed to the velocity renderer. Any ideas how to prevent that? – KendallV Dec 08 '16 at 16:29
1

Here is fully working solution for Atlassian Jira 8.0.0 or later.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.RendererManager;
import com.atlassian.jira.issue.fields.renderer.JiraRendererPlugin;
import com.atlassian.jira.issue.fields.renderer.IssueRenderContext;

public String renderWikiMarkupOfDescription(String descriptor, Issue issue) {
    RendererManager rendererManager = ComponentAccessor.getComponentOfType(RendererManager.class);
    JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");
    String output = renderer.render(descriptor, new IssueRenderContext(issue));
    return output;
}

public String renderWikiMarkupOfEnvironment(String environment, Issue issue) {
    RendererManager rendererManager = ComponentAccessor.getComponentOfType(RendererManager.class);
    JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");
    String output = renderer.render(environment, new IssueRenderContext(issue));
    return output;
}

And usage:

renderWikiMarkupOfDescription(issue.getDescription(), issue);
renderWikiMarkupOfEnvironment(issue.getEnvironment(), issue);

And screenshots to prove this working 100 % success:

Description Rendered to HTML 1 Description Rendered to HTML 2 Environment Rendered to HTML 1

Matti Kiviharju
  • 411
  • 3
  • 8
  • 18