Using Mosaic Templates with ScriptRunner for Jira Cloud
Create and populate your Mosaic Confluence templates by leveraging ScriptRunner for Jira.
This use case will show you how to:
- Extract a Mosaic template's storage format
- Encapsulate that format in reusable Groovy utility functions
- Dynamically populate template content (for example, to-do lists derived from Jira issues)
- Create Confluence pages via REST using the generated storage format
The end result is a reusable, scriptable mechanism for generating Mosaic-based pages without relying on manual template selection in the Confluence UI.
Prerequisites
Before proceeding, ensure you have:
- Administrator access to the relevant Jira and Confluence instances.
- ScriptRunner for Jira Cloud installed and configured.
- Permission to create pages in the target Confluence space.
- Familiarity with:
- Groovy scripting
- Jira JQL
- Confluence REST APIs
- Confluence storage format (XHTML)
High-Level Architecture
At a high level, the solution works as follows:
- A Mosaic template is created manually in Confluence.
- The template's storage format is retrieved and treated as the canonical structure.
- Groovy utility functions encapsulate that structure and expose parameters (for example, user name and to-do items).
- Jira issues are queried via JQL and transformed into domain objects (e.g. TodoItem).
- The populated storage format is sent to Confluence via REST to create a page.
Although the following example uses hard-coded template content, the same approach can be extended to dynamically retrieve template content via the Confluence REST API.
Step 1: Create and Inspect a Mosaic Template
- In Confluence, create a page using Mosaic and design the layout you want (for example, a table-based to-do list). Alternatively take a look at our pre-made templates.
- Save the page or template.
- Retrieve the storage format for that page by using the Confluence REST API
?expand=body.storage, or by inspecting it via ScriptRunner/REST tooling.
This storage format becomes the basis for your template logic.
In the example implementation, the storage format is hard-coded. For a production-ready solution, you should retrieve this dynamically so changes to the Mosaic template do not require code updates.
Step 2: Define Domain Objects
Create simple domain objects to represent the data you want to inject into the Mosaic template.
For example:
- MosaicTodoItem
- Priority
- Task summary
- Description
- Additional metadata as required
These objects decouple Jira issue data from the Confluence storage format.
Step 3: Build a Template Utility Class
Create a Groovy utility class (for example, MosaicTemplates) that:
- Accepts structured data (such as a list of TodoItem objects)
- Injects that data into the Mosaic storage format
- Returns a fully-formed Confluence storage document
Example Responsibility
todoList(userName, todoItems) -> String (storage format)- Iterates over the list of to-do items
- Maps each item into the table rows defined by the Mosaic template
- Preserves all required Mosaic markup and macros
This approach ensures the Mosaic layout remains intact while allowing dynamic content.
Step 4: Query Jira and Map Issues to Template Data
Using ScriptRunner for Jira:
Step 5: Create the Confluence Page via REST
Once you have the populated storage format:
Step 6: Execute via the Script Console
During development and testing:
Optional Enhancements
This basic example can be enhanced in several ways, for example:
Dynamic template retrieval
Fetch the Mosaic template storage format at runtime via REST.
Page metadata parameters
Add parameters for:
- Space key
- Parent page
- Labels
Higher-level abstractions
Wrap page creation logic into a single function:
createMosaicTodoPage(spaceKey, parentPageId, userName, jql)Shared libraries
Package utilities into a shared ScriptRunner library.
Result
When executed, the script:
- Retrieves Jira issues
- Converts them into structured to-do items
- Generates Mosaic-compatible storage format
- Creates a Confluence page that visually matches the original Mosaic template
This provides a repeatable, quick, automated mechanism for generating rich Confluence content directly from Jira data.
Summary
By combining ScriptRunner for Jira with Confluence's storage format, you can:
- Treat Mosaic templates as code
- Abstract layout concerns from business logic
- Generate consistent, high-quality pages programmatically
This approach scales well for teams that rely heavily on Jira-driven reporting and structured Confluence documentation.