Forums

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

Show page after clicking on a button

Mouna Hammoudi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2023

I would like a web page to be shown after clicking on a button if the issue does not have any components in Jira. I am using a web fragment and a REST api. Here is my code but it is not working Anyone knows how this could be fixed?

package SuperFeature 

import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue;
import javax.ws.rs.core.Response
import org.apache.log4j.Logger
import groovy.transform.BaseScript
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import javax.ws.rs.core.MultivaluedMap
import com.atlassian.jira.component.ComponentAccessor
import javax.ws.rs.core.Response
import com.opensymphony.workflow.WorkflowException
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

mounaGenerate(httpMethod: "GET", groups: ["jira-users"])
{
MultivaluedMap queryParams, String body ->
def log1 = Logger.getLogger("atlassian-jira.log")
def issueId = queryParams.getFirst("issueId") as Long

Issue myissue = ComponentAccessor.getIssueManager().getIssueObject(issueId)
def issueKey = myissue.getKey()
def componentNames = myissue.getComponents()*.name

log1.warn("MOUNA componentNames "+componentNames)


if(componentNames.empty) {

log1.warn("MOUNA EMPTY")


def dialog =
"""<html>
<head>
<title>Hello World</title>
</head>
<body id="jira" class="aui-layout aui-theme-default">
<h1>Hello world</h1>
</body>
</html>
"""

Response.ok().type(MediaType.TEXT_HTML).entity(dialog.toString()).build()











}else{
log1.warn("MOUNA componentNames NOT EMPTY")

}
}

 

1 answer

0 votes
Patricia Modispacher _appanvil_
Atlassian Partner
September 8, 2023

Hi @Mouna Hammoudi

Your code seems mostly correct, but there might be a few improvements needed.

Here's a revised version of your script with comments explaining the changes:

 

package SuperFeature

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import org.apache.log4j.Logger

@BaseScript CustomEndpointDelegate delegate

mounaGenerate(httpMethod: "GET", groups: ["jira-users"]) { MultivaluedMap queryParams, String body ->

def log1 = Logger.getLogger("atlassian-jira.log")
def issueId = queryParams.getFirst("issueId") as Long

Issue myissue = ComponentAccessor.getIssueManager().getIssueObject(issueId)
def issueKey = myissue.getKey()
def componentNames = myissue.getComponents()*.name

log1.warn("MOUNA componentNames " + componentNames)

if (componentNames.isEmpty()) {
log1.warn("MOUNA EMPTY")

// Create an HTML page to show
def dialog =
"""<html>
<head>
<title>Hello World</title>
</head>
<body id="jira" class="aui-layout aui-theme-default">
<h1>Hello world</h1>
</body>
</html>
"""

// Return the HTML page as a response
return Response.ok().type(MediaType.TEXT_HTML).entity(dialog.toString()).build()
} else {
log1.warn("MOUNA componentNames NOT EMPTY")
// You can add further logic here if needed
return Response.ok().build()
}
}

 

 

 

Key points to note:

- Use return when returning a Response object.
- Ensure that the HTML page is constructed correctly within the dialog variable.


The return Response.ok().build() statement is used when the issue has components but can be adjusted based on your specific needs.


Make sure your endpoint configuration, URL, and permissions are correctly set up in Jira for this script to work as expected.

 

Good Luck! :) 

Mouna Hammoudi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2023

@Patricia Modispacher _appanvil_ It is not working, even though I have used the return

statement.  Do you know what needs to be done in order to fix the problem?

Patricia Modispacher _appanvil_
Atlassian Partner
September 10, 2023

Hi @Mouna Hammoudi !

 

So sorry to heat that, I'm not sure, tbh. I would recommend to verify that the endpoint for your REST API is correctly configured in Jira. Ensure that the URL, HTTP method, and permissions are set up correctly.

 

One last try with this code snippet:

import com.atlassian.jira.component.ComponentAccessor
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response

def issueKey = params.issueKey // Get the issue key from the request
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey)
def componentNames = issue.components*.name

if (componentNames.isEmpty()) {
def htmlContent =
"""
<html>
<head>
<title>Web Page Title</title>
</head>
<body>
<h1>Hello, this is your web page!</h1>
<!-- Add your HTML content here -->
</body>
</html>
"""
return Response.ok(htmlContent, MediaType.TEXT_HTML_TYPE).build()
} else {
return Response.ok("Issue has components").build()
}

 

 

Does this work?

 

Otherwise, you might just go to the Atlassian support directly. https://support.atlassian.com/contact/#/ Although the response time might take a few days. 

Suggest an answer

Log in or Sign up to answer