JMWE Cloud: How to query the Assets API using Nunjucks
Summary
Some use cases in Jira Misc Workflow Extensions (JMWE) for Cloud require querying Assets (formerly Insight) data. Because Asset data is stored externally to your Jira site, you must use the Atlassian Assets API (api.atlassian.com) rather than your standard Jira instance URL. This article provides the Nunjucks script and configuration steps required to perform an Assets Query Language (AQL) query via an API call.
Environment
Jira Service Management Cloud
Jira Misc Workflow Extensions (JMWE) for Cloud
Assets (formerly Insight)
Prerequisites
Before configuring the script, ensure you have the following information:
User Email: The email address of the Atlassian account that will perform the request.
Atlassian API Token: An API token generated for the account above. See
Manage API tokens for your Atlassian account | Atlassian Support.Assets Workspace ID:
Open your browser and navigate to:
https://<yourinstance>.atlassian.net/rest/servicedeskapi/assets/workspaceLocate and copy the
workspaceIdvalue.
Solution
To query Assets using Nunjucks, you can use the callRest filter within a scripted post function (such as Build-your-own (Nunjucks scripted) post function).
1. Configure the script
Add the desired JMWE post function to your workflow transition.
In the script editor, paste the following Nunjucks template:
{% set email = "your-email@example.com" %}
{% set token = "your-api-token" %}
{% set wsid = "your-workspace-id" %}
{% set query = "Key IN ('XX-01')" %}
{{ "https://:email::key@api.atlassian.com/jsm/assets/workspace/:wsid/v1/object/aql"
| callRest(
params={"emai":email, "key":token, "wsid":wsid},
verb="POST",
body={"qlQuery": query }
) }}2. Update variables
email: Enter the user email associated with the API token.
token: Enter the API token you generated.
wsid: Enter the Workspace ID obtained in the prerequisites.
query: Enter your AQL string. See
Use Jira Query Language (JQL) with Assets' AQL | Assets | Atlassian Support for more information on using AQL. Example:
Key IN ('XX-01')searches for a specific asset key.