I created a confluence template in which I want to insert a chart(pie) showing the status of tickets related to a specific project. I want the chart macro could retrieve the number of different tickets by their type in JIRA automatically so that each time when the user create a page based on this template, they don't need to fill in the chart data manually.
I know that in JIRA Report macro one can retrieve this kind of information easily. But how can I access this data in the report result in the chart macro? Or do I have to implement another own custom macro? If so, do I have to write some Java or Javascript code, or just using the macro template language is enough?
I am a newbie to confluence. Any ideas would be helpful.
Problem solved. The Confluence Page template is written in Storage Format and rendered by in Confluence internally before returned to client. There is a way to declare variables in the template and then feed them data by adding entries in the template context in Java or Javascript.
For instance, the JIRA chart macro is inserted in the template simple-template.xml below :
<ac:structured-macro ac:name="jira" ac:schema-version="1">
<ac:parameter ac:name="server">Your Company JIRA</ac:parameter>
<ac:parameter ac:name="jqlQuery"><at:var at:name="vJql" /></ac:parameter<name />
<ac:parameter ac:name="count">true</ac:parameter>
<ac:parameter ac:name="serverId"><at:var at:name="vServerId" /></ac:parameter>
</ac:structured-macro>
Two vars vJql
and vServerId
are declared using syntax <at:var at:name="varName"/>
. These vars are accessible in the template context provided by a class that extends class com.atlassian.confluence.plugins.createcontent.api.contextproviders.AbstractBlueprintContextProvider
. To bind the context provider with the template, one need to config the template declaration in atlassian-plugin.xml by adding the element context-provider
:
<content-template key="simple-template"
template-title-key="delivery.blueprint.template.title" i18n-name-key="new.template.blueprint.name">
<resource name="template" type="download" location="/templates/simple-template.xml" />
<context-provider class="com.company.atlassian.plugins.confluence.SimpleTemplateContextProvider" />
</content-template>
Within the class, feed the vars by returning a context containing entries for the vars :
private final String VAR_PROJECT_KEY = "jira-project";
private final String VAR_VERSION = "jira-fix-version";
private final String VAR_JQL = "vJql";
private final String VAR_SERVER_ID = "vServerId";
@Override
protected BlueprintContext updateBlueprintContext(BlueprintContext context) {
try {
String projectKey = (String) context.get(VAR_PROJECT_KEY);
String version = (String) context.get(VAR_VERSION);
String jql = "project = \'" + projectKey + "\' AND fixVersion = " + version;
String serverId = ResourceBundle.getBundle("simple-template-example").getString("jira.serverid");
context.put(VAR_JQL, jql);
context.put(VAR_SERVER_ID, serverId);
return context;
} catch (Exception e) {
e.printStackTrace();
return context;
}
}
Done.