Apps documentation
Theme Extension for Jira Service Desk
Theme Extension for Jira Service Desk
Guides
FAQ
Release notes
Last updated May 25, 2020

Compatibility with ScriptRunner

Theme Extension for Jira Service Desk offers the opportunity to integrate it with external applications

Theme Extension for Jira Service Desk provides its web resource context to enable integration with external apps, such as ScriptRunner.

The web resource context is theme-extension-view-portal.

This allows you to further customize both your Jira and Theme Extension for Jira Service Desk with external apps and scripts.

To integrate Theme Extension for Jira Service Desk with ScriptRunner, you need to:

Dynamically Direct Users to Request Types with ScriptRunner

This use case is an example of integration between Theme Extension for Jira Service Desk and an external app, in this case, ScriptRunner.

The objective is to run a ScriptRunner script in Theme Extension for Jira Service Desk that will check what group a customer is in, A or B, and direct them to to the appropriate request type group via URL.

Steps

Add the following scripts in ScriptRunner:

  1. The ScripRunner script presented below gets the path of the customer and returns the path to which they should be redirected. A REST endpoint is defined.
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.RendererManager
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@WithPlugin("com.atlassian.servicedesk")
@BaseScript CustomEndpointDelegate delegate

getRedirect(httpMethod: "GET") { MultivaluedMap queryParams, String body -> 
    def authContext = ComponentAccessor.getJiraAuthenticationContext();
    def groupManager = ComponentAccessor.getGroupManager();
    def currentPath = queryParams.get("currentPath");
    if(currentPath.get(0).toString().endsWith("theme/portal/1")) {
        if(groupManager.isUserInGroup(authContext.getLoggedInUser(), "Group A")) {
            return Response.ok("/plugins/servlet/theme/portal/1/group/2").build();
        } else if(groupManager.isUserInGroup(authContext.getLoggedInUser(), "Group B")) {
            return Response.ok("/plugins/servlet/theme/portal/1/group/1").build();
        }
    }
    return Response.ok("").build();
}
  1. The ScriptRunner script presented below will execute the script from Step 1 in the backend and check whether a customer should be redirected or not. Customers from group A will be directed to to request types from group 2. Customers from group B will be, in turn, directed to request types from group 1.
function httpGetAsync(url, callback) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

(function () {
    httpGetAsync(AJS.contextPath() + "/rest/scriptrunner/latest/custom/getRedirect?currentPath=" + window.location.pathname,
        function (url) {
            if (!!url) {
                window.location.replace(url);
            }
        }
    )
})();
Info
In order for these scripts to work, you need to define theme-extension-view-portal as web resource in ScriptRunner. For more information, see ScriptRunner documentation.

Dynamically Fill the Announcement Banner with ScriptRunner

This use case is an example of integration between Theme Extension for Jira Service Desk and an external app, in this case, ScriptRunner.

The objective is to run a ScriptRunner script in Theme Extension for Jira Service Desk that will dynamically display custom announcement banners in Help Center, specific to selected request types.

Steps

Add the following Scripts to ScriptRunner:

  1. The ScriptRunner script presented below defines the REST endpoint and returns HTML content for the announcement banner.
getAnnouncementBanner(httpMethod: "GET") { MultivaluedMap queryParams, String body ->
    return Response.ok("<h1>Announcement banner header</h1><p>Announcement banner description</p>").build();
}
  1. The ScriptRunner script presented below inserts the HTML content returned by the script from Step 1 into the announcement banner, identified as announcement-banner-html-content.
function httpGetAsync(url, callback) {
       var xmlHttp = new XMLHttpRequest();
       xmlHttp.onreadystatechange = function () {
           if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
               callback(xmlHttp.responseText);
       }
       xmlHttp.open("GET", url, true);
       xmlHttp.send(null);
   }
   
   (function () {
       const addContent = function () {
           const announcementBanner = document.getElementById("theme-announcement-banner-html-content");
           if (announcementBanner) {
               clearInterval(checkAnnouncementBannerExists);
               httpGetAsync(AJS.contextPath() + "/rest/scriptrunner/latest/custom/getAnnouncementBanner",
                   function (content) {
                       announcementBanner.innerHTML = content;
                   }
               )
           }
       }
       const checkAnnouncementBannerExists = setInterval(addContent, 1000);
   })();
Tip
You can also use ID theme-announcement-banner for the entire announcement banner element.
Info
In order for these scripts to work, you need to define theme-extension-view-portal as web resource in ScriptRunner. For more information, see ScriptRunner documentation.

Need help?

If you can’t find the answer you need in our documentation, raise a support request*.
*Include as much information as possible to help our support team resolve your issue faster.