Forums

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

Throw an error if the issue does not have components

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 5, 2023

I am implementing a fragment with a REST endpoint and I would like to display an error if the issue does not have any components. The button is called "MounaGenerate" and here is my implementation of the fragment below: 

Capture.PNG

Here is my implementation of the REST endpoint: 

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

@BaseScript CustomEndpointDelegate delegate

mounaGenerate(httpMethod: "GET", groups: ["jira-users"]) {

            def log = Logger.getLogger("atlassian-jira.log")

           

              def components = issue.components

              log.warn("MOUNA COMPONENTS "+components)

              if(components.empty) {

                      log.warn("MOUNA EMPTY")

                        throw new WorkflowException("Issue does not have components")

              }

}

I am getting the following error on my log: 

2023-09-05 14:30:09,456+0200 https-openssl-nio-443-exec-6 ERROR mouh 870x1330103x2 1ic193i 10.250.160.111 /rest/scriptrunner/latest/custom/mounaGenerate [c.o.s.r.rest.common.UserCustomScriptEndpoint] Script endpoint failed on method: GET mounaGenerate
2023-09-05 14:30:09,456+0200 https-openssl-nio-443-exec-6 ERROR mouh 870x1330103x2 1ic193i 10.250.160.111 /rest/scriptrunner/latest/custom/mounaGenerate [c.o.s.r.rest.common.UserCustomScriptEndpoint]

 

Ideally, I would like a new page to be shown like the following that says that there is an error because the issue does not have any components:

Capture2.PNG

How can I do this? 

1 answer

1 accepted

0 votes
Answer accepted
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 6, 2023

The issue object needs to be accessed in a different way: 

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

@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) {

                      log.warn("MOUNA EMPTY")

                        throw new WorkflowException("Issue does not have components")

              }else{

                          log1.warn("MOUNA componentNames NOT EMPTY")

              }

}

Suggest an answer

Log in or Sign up to answer