Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using ScriptRunner to display a flag and open a web page

adrianprocter February 4, 2019

Using ScriptRunner, I'd like to be able to add a new menu item to a Jira ticket, that when clicked will do the following

1) Display a flag acknowledging that the menu item has been pressed

2) Launch a custom URI - in a new window or tab

 

Right now, I am hard-coding the custom URI, but ultimately it will read in a value that is stored in a field value within the Jira ticket.

So far, I have created the RESTendpoint, like this, based on the example from https://scriptrunner.adaptavist.com/4.3.9/jira/fragments/WebItem.html#_acting_on_the_current_page_issue

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript

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


@BaseScript CustomEndpointDelegate delegate

testtool(httpMethod: "GET") { MultivaluedMap queryParams ->
def customURI = 'http://server/?test_suite=test_suite_name&run'
def flag = [
type : 'success',
title: "Test Tool Launched",
close: 'auto',
body : "The test suite has been launched in a new window/tab "+customURI
]
Response.ok(JsonOutput.toJson(flag)).build()
}

When I test the end point, it works.  I created a Web Item, that adds the button to the operations-top-level, and is set to "Run code and show a flag".  The link is set to /rest/scriptrunner/latest/custom/testtool?issueID=${issue.id}.

When I click the button, the flag is shown, with the correct customURI.  So far, everything is going to plan.

Now, I change the RESTEndPoint to open the customURI instead:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript

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


@BaseScript CustomEndpointDelegate delegate

testtool(httpMethod: "GET") { MultivaluedMap queryParams ->
def customURI = 'http://server/?test_suite=hris_prod_compare_demo_tables&run'
def flag = [
type : 'success',
title: "Test Tool Launched",
close: 'auto',
body : "The test suite has been launched in a new window/tab "+customURI
]
Response.temporaryRedirect(URI.create(customURI)).build()
// Response.ok(JsonOutput.toJson(flag)).build()
}

And when I call the RESTEndPoint manually, it correctly redirects me to the customURI location.

And this is now where i get stuck - when I press the web-item button now, nothing happens.  I would have expected it to have opened up the customURI that i entered, but that does not seem to be happening.  

Second, even if I enable the 

Response.ok(JsonOutput.toJson(flag)).build()

no flag is now shown, but I'm not sure if that is because the link to opening up the customURI is not working.

Third, I have not yet found any syntax that would actually open up the customURI link (once I get it working) in a new tab/window, so that it does not interfere with the operation of Jira.

 

Can someone point me in the right direction for figuring this out?  

 

Update: By changing the web-item from "Run code and display a flag" to "Navigate to this link", the code now works to open up the customURI.  I would still like it to open the link in a new window/tab, and for a flag to be displayed to show that an action has occurred.

1 answer

0 votes
adrianprocter February 4, 2019

For now, I have created a work-around by creating a new Script field and adding it to the correct scrren.  The code is as follows, and is of type HTML template.

 

import com.atlassian.jira.component.ComponentAccessor
def customURI='http://server?test_suite=';
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10206");
def value = (String)issue.getCustomFieldValue(customField);
customURI = customURI + value + '&run'
return "<a href='"+customURI+"' target='_blank'>Launch Test Suite for "+value+"</a>";

Suggest an answer

Log in or Sign up to answer