Customising Macro Variables
Introduction
Some groovy knowledge required. With the introduction of the new version of Label Tools for ScriptRunner Confluence, we allow users the ability to customise macro variables.
We have introduced variables such as $fullname which can be used in the macro. When the page is rendered $fullname will be resolved to the users full name as specified in Confluence. By using ScriptRunner for Confluence it is possible to change the behaviour of $fullname or simply to specify your own variable. For example, you could create a variable $lastCommentDate which could resolve to the date of the last comment on the page.
Instructions
We will be looking at a simple Label Tools macro "Add Label", and how to add our own custom variables. Supplied variable labels are $username, $fullname, $year, $month, $day and $parent.
-
Navigate to your script root. Default is <Confluence>/home/scripts. More information about script roots.
-
Create the package com.onresolve.scriptrunner.canned.confluence.macros in your script root.
-
Navigate to the package specified above and inside the macros folder create a new groovy class "AddLabelMacroModified.groovy" that extends the old class and overrides the setCustomVariables method. A descriptive class name is recommended.
-
Populate the class:
package com.onresolve.scriptrunner.canned.confluence.macros
import java.time.Instant
class AddLabelMacroModified extends AddLabelMacro {
@Override
protected Map<String, String> setCustomVariables() {
def customVariables = [:]
customVariables.put("\$ppap","PenPineappleApplePen")
customVariables.put("\$epoch", String.valueOf(Instant.now().toEpochMilli()))
return customVariables
}
}
-
Disable the old macro in the Script Macros section in Confluence Administration. Enable the new macro in the same section. The names of the macros will be the same as to not break any macros already used.
-
Use the AddLabel macro on a page and edit it with the configured variables
-
Notice the resulting labels on the page, the static specified string and a dynamic value (time since epoch in milliseconds).
Macro class names that are extendable
-
AddLabelMacro
-
ChooseLabelMacro
For how-to questions please ask on Atlassian Answers where there is a very active community. Adaptavist staff are also likely to respond there.
Ask a question about ScriptRunner for JIRA, for for Bitbucket Server, or for Confluence.