~ read.

Creating a custom JIRA Wiki Renderer plugin

Today i finished one tricky plugin that does really easy job, but it took me a while until i finally got a nice and solid solution for my problem. The task was simple - in the description and comments fields make an automatic replacement of specific text, like wiki : TestPage into http://hostname.com/wiki/title=TestPage . But on the issue view page it shouldn't look like plain url, but instead should look like hyperlink . At first i tried to put some javascript into "Description" and "Comment" fields describtion ( sorry for tautology), but i didn't succeed much there. So, i came up to this issue which actually helped me to develop my own Jira plugin that was doing rendering properly and exactly how i wanted it. So, here's what you need to do :

  • First you need to create a new jira plugin project. You can find instructions here
  • Then you need to create a new class :
package com.biercoff.custom.renderer;

import com.atlassian.event.api.EventPublisher;  
import com.atlassian.jira.issue.fields.renderer.IssueRenderContext;  
import com.atlassian.jira.issue.fields.renderer.wiki.AtlassianWikiRenderer;  
import com.atlassian.jira.plugin.renderer.JiraRendererModuleDescriptor;  
import com.atlassian.jira.util.velocity.VelocityRequestContextFactory;

/**
 * A custom text renderer for JIRA that transforms text like wiki: TestPage into
 * <a href="http://hostname.com/wiki/index.php?title=TestPage">TestPage</a>
 */
public class CustomRenderer extends AtlassianWikiRenderer {

    public static final String TYPE = "fixed-text-renderer";

    private JiraRendererModuleDescriptor jiraRendererModuleDescriptor;

    public CustomRenderer(EventPublisher eventPublisher,
            VelocityRequestContextFactory velocityRequestContextFactory) {
        super(eventPublisher, velocityRequestContextFactory);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.atlassian.jira.issue.fields.renderer.
     JiraRendererPlugin#getDescriptor()
     */
    public JiraRendererModuleDescriptor getDescriptor() {
        return jiraRendererModuleDescriptor;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.atlassian.jira.issue.fields.renderer.
     JiraRendererPlugin#getRendererType()
     */
    public String getRendererType() {
        return TYPE;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.atlassian.jira.issue.fields.
     renderer.JiraRendererPlugin#init(com.atlassian.
     jira.plugin.renderer.JiraRendererModuleDescriptor)
     */
    public void init(JiraRendererModuleDescriptor jiraRendererModuleDescriptor) {
        this.jiraRendererModuleDescriptor = jiraRendererModuleDescriptor;

    }

    /*
     * (non-Javadoc)
     *
     * @see com.atlassian.jira.issue.fields.
     renderer.JiraRendererPlugin#render(java.lang.String,
     *      com.atlassian.jira.issue.
     fields.renderer.IssueRenderContext)
     */
    public String render(String s,
    IssueRenderContext issueRenderContext) {
    String replacedResult = renderTextWithCustomWikiLink(s);
        return super.render(replacedResult, issueRenderContext);
    }

    /**
     * transforms text like wiki: TestPage into
     * <a href="http://hostname.com/wiki/index.php?title=TestPage">
     TestPage</a>
     * @param text
     * @return
     */
    private String renderTextWithCustomWikiLink(String text) {
        String result = text.replaceAll("wiki: ([^\\s]+)",
        "[$1 |http://hostname.com/wiki/index.php?title=$1]");
        return result.toString();
    }

    public String renderAsText(String s, 
    IssueRenderContext issuerendercontext) {
        return s;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.atlassian.jira.issue.fields.
     renderer.JiraRendererPlugin#transformForEdit
     (java.lang.Object)
     */
    public Object transformForEdit(Object obj) {
        return obj;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.atlassian.jira.issue.
     fields.renderer.JiraRendererPlugin#transformFromEdit
     (java.lang.Object)
     */
    public Object transformFromEdit(Object obj) {
        return obj;
    }

}
  • After that you have one more thing to adjust in your project and you're good to go :

    • Open atlassian-plugin.xml that is located in src/main/java/resources and
    • Add next section to it :
    <jira-renderer system="true" key="custom-renderer" 
    name="Custom Text Renderer"
    class="com.biercoff.custom.renderer.CustomRenderer">
<description>A renderer that will renderer content as plain text in fixed width  
font by wrapping it into tags. Lines will be wrapped where necessary and links  
and JIRA issue keys will be automatically hyperlinked.</description>  
        <resource type="velocity" name="edit"
        location="templates/plugins/renderers/wiki/wiki-renderer-edit.vm"/>
    </jira-renderer>

After that you need to add adjustments in your Jira :

  • Go to Settings->Issues->Field Configuration (or just type twice G and then enter Field Configuration into search field)
  • Select your Field configurationa and find field that you want to use your custom renderer ( In my case it was Description and Comments)
  • Go to Settings->Add-ons->All addons
  • Find Wiki Renderer Macros Plugin and enable HTML module in it

After this manipulation everything should work fine and on issue create or edit hyperlinks will be rendered correctly.

comments powered by Disqus
comments powered by Disqus