Simple JIRA DataCenter REST endpoint not working - jira

I am trying to perform a REST endpoint with its corresponding web fragment but I am getting the error:
script function failed on issue: issue: SF-347, user: mouh, fieldId: __init__, file: <inline script>, edit behaviour: https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/8, https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/17, https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/21
Here is my error log here below:
2022-08-16 20:39:37,512+0200 https-openssl-nio-443-exec-278 ERROR mouh 1239x60320x27 mu7qce 10.248.27.109 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: issue: SF-347, user: mouh, fieldId: __init__, file: <inline script>, edit behaviour: https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/8, https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/17, https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/21
2022-08-16 20:39:36,371+0200 https-openssl-nio-443-exec-278 WARN mouh 1239x60311x26 mu7qce 10.248.27.109 /browse/SF-347 [c.s.j.p.r.rest.api.MicroDashBoard] getiTracReleaseInfo for user "mouh" took 0 ms
2022-08-16 20:39:36,371+0200 https-openssl-nio-443-exec-278 WARN mouh 1239x60311x26 mu7qce 10.248.27.109 /browse/SF-347 [c.s.j.p.r.rest.api.MicroDashBoard] getiTracReleaseInfo for user "mouh" - "green" -0
2022-08-16 20:39:36,371+0200 https-openssl-nio-443-exec-278 WARN mouh 1239x60311x26 mu7qce 10.248.27.109 /browse/SF-347 [c.s.j.p.r.rest.api.MicroDashBoard] getiTracReleaseInfo for user "mouh" took 0 ms
2022-08-16 20:39:36,371+0200 https-openssl-nio-443-exec-278 WARN mouh 1239x60311x26 mu7qce 10.248.27.109 /browse/SF-347 [c.s.j.p.r.rest.api.MicroDashBoard] getiTracReleaseInfo for user "mouh" - "yellow" -0
2022-08-16 20:39:36,371+0200 https-openssl-nio-443-exec-278 WARN mouh 1239x60311x26 mu7qce 10.248.27.109 /browse/SF-347 [c.s.j.p.r.rest.api.MicroDashBoard] getiTracReleaseInfo for user "mouh" took 0 ms
2022-08-16 20:39:36,371+0200 https-openssl-nio-443-exec-278 WARN mouh 1239x60311x26 mu7qce 10.248.27.109 /browse/SF-347 [c.s.j.p.r.rest.api.MicroDashBoard] getiTracReleaseInfo for user "mouh" - "red" -0
I don't understand why I have this error my code is super simple. Here is the way in which I have created my web fragment:
This is a screenshot of the custom web item fragment I have created:
This is the corresponding REST endpoint I have created:
I am specifying the file MounaBulkSummary.groovy to be the one containing the code for the REST endpoint.
Here is the content of the file MounaBulkSummary.groovy
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 com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import javax.ws.rs.core.Response
import org.apache.log4j.Logger
def log = Logger.warn("atlassian-jira.log")
log.warn("MOUNA BULK")
#BaseScript CustomEndpointDelegate delegate
MounaBulkSummary(httpMethod: "GET") { MultivaluedMap queryParams ->
log.warn("MOUNA BULK")
def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
def issue = ComponentAccessor.getIssueManager().getIssueObject(queryParams.getFirst("issueId") as Long)
def baseUrl = ScriptRunnerImpl.getOsgiService(ApplicationProperties).getBaseUrl(UrlMode.ABSOLUTE)
def redirectUrl = "https://secapps.eur.ad.sag:9888/FPIA/?iTrac=${issue.key}&reporter=${user.key}&baseURL=${baseUrl}"
Response.temporaryRedirect(URI.create(redirectUrl)).build()
}

I found my mistake, I needed to use call in the URL. Here is the code for the REST API:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue;
import javax.ws.rs.core.MultivaluedMap
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 com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import javax.ws.rs.core.Response
import com.atlassian.jira.component.ComponentAccessor
#BaseScript CustomEndpointDelegate delegate
def log = Logger.getLogger("atlassian-jira.log")
long iTracSuperFeatureSummaryUpdaterIssueTypeID= 10200;
int iTracSuperFeatureSummaryUpdaterProjectID= 23520;
String iTracSuperFeatureSummaryUpdaterURL= "http://itrac-service:8080/bulksummaryupdater/bulksplitter?key=:1:&reporter=:2:";
callMounaBulkSummary(httpMethod: "GET", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body ->
log.warn("MOUNA BULK")
def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
Issue issue = ComponentAccessor.getIssueManager().getIssueObject(queryParams.getFirst("issueId") as Long)
def project = issue.getProject()
def baseUrl = ScriptRunnerImpl.getOsgiService(ApplicationProperties).getBaseUrl(UrlMode.ABSOLUTE)
log.warn("MOUNA BULK "+user+" "+issue+" "+baseUrl+" "+project +" "+project.get("id"))
iTracSuperFeatureSummaryUpdaterURL= iTracSuperFeatureSummaryUpdaterURL.replaceAll(":1:", issue.getKey())
iTracSuperFeatureSummaryUpdaterURL= iTracSuperFeatureSummaryUpdaterURL.replaceAll(":2:", user.getUsername())
def projectID=project.get("id")
int projectIDInt= projectID as int
if(iTracSuperFeatureSummaryUpdaterProjectID== projectIDInt && iTracSuperFeatureSummaryUpdaterIssueTypeID==Long.valueOf(issue.getIssueTypeId()) ){
Response.temporaryRedirect(URI.create(iTracSuperFeatureSummaryUpdaterURL)).build()
}
}
Other code for as similar REST endpoint:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue;
import javax.ws.rs.core.MultivaluedMap
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 com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import javax.ws.rs.core.Response
import com.atlassian.jira.component.ComponentAccessor
#BaseScript CustomEndpointDelegate delegate
def log = Logger.getLogger("atlassian-jira.log")
int iTracSuperFeatureSplitterIssueTypeID= 10200;
int iTracSuperFeatureSplitterProjectID= 23520;
String iTracSuperFeatureSplitterURL= "http://itrac-service:8080/featuregenerator/bulksplitter?key=:1:&reporter=:2:";
callMounaBulkSplitter(httpMethod: "GET", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body ->
log.warn("MOUNA BULK")
def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
Issue issue = ComponentAccessor.getIssueManager().getIssueObject(queryParams.getFirst("issueId") as Long)
def project = issue.getProject()
def baseUrl = ScriptRunnerImpl.getOsgiService(ApplicationProperties).getBaseUrl(UrlMode.ABSOLUTE)
log.warn("MOUNA BULK "+user+" "+issue+" "+baseUrl+" "+project +" "+project.get("id"))
iTracSuperFeatureSplitterURL= iTracSuperFeatureSplitterURL.replaceAll(":1:", issue.getKey())
iTracSuperFeatureSplitterURL= iTracSuperFeatureSplitterURL.replaceAll(":2:", user.getUsername())
def projectID=project.get("id")
int projectIDInt= projectID as int
if(iTracSuperFeatureSplitterProjectID== projectIDInt && iTracSuperFeatureSplitterIssueTypeID==Long.valueOf(issue.getIssueTypeId()) ){
Response.temporaryRedirect(URI.create(iTracSuperFeatureSplitterURL)).build()
}
}

Related

REST endpoint works as in line script and doesn't work when mentioned as file-Cannot open URL: bundle://200.0:1/rest_CreateMultipleSubtasks.groovy

I am using this REST endpoint for a fragment and it works perfectly as an inline script. 
Here is my code below and here is a screenshot of the page where I am redirected after clicking on the button. 
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 CreateMultipleSubtasks.Configuration_CreateMultipleSubtasks
import com.atlassian.jira.project.Project
#BaseScript CustomEndpointDelegate delegate
callSubTaskCreator(httpMethod: "GET", groups: ["jira-users"]) {MultivaluedMap queryParams, String body ->
def log1 = Logger.getLogger("atlassian-jira.log")
log1.warn("MOUNA CAMELIA223");
Configuration_CreateMultipleSubtasks conf =new Configuration_CreateMultipleSubtasks()
def MultipleSubtasksHashMap= conf.getSubTaskCreatorHashMap()
String itracCreatemultiplesubtasksProjectCategoriesURL = MultipleSubtasksHashMap["itracCreatemultiplesubtasksProjectCategoriesURL"] as String;
log1.warn("MOUNA CAMELIA223 "+itracCreatemultiplesubtasksProjectCategoriesURL);
 def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
  def issueId = queryParams.getFirst("issueId") as Long
  Issue myissue = ComponentAccessor.getIssueManager().getIssueObject(issueId)
  def issueKey = myissue.getKey()
  def itracCreatemultiplesubtasksProjectCategoriesURL2 = itracCreatemultiplesubtasksProjectCategoriesURL.replaceAll(":1:", issueKey)
  def itracCreatemultiplesubtasksProjectCategoriesURL3 = itracCreatemultiplesubtasksProjectCategoriesURL2.replaceAll(":2:", user.getUsername())
  log1.warn("The rest endpoint has been executed by the user " +user)
  Response.temporaryRedirect(URI.create(itracCreatemultiplesubtasksProjectCategoriesURL3)).build()
 
}
When I specify a file for the code that I refer to in my script editor. Everything fails when I click on the button and I am redirected to an error page as you can see below. 
anyone knows what the problem is?
We need a package declatation at the top of the code so we need package BulkSplitter at the top of the code. We need to go to REST endpoint and execute an update. Otherwise, Jira doesn't recognize our changes.
package BulkSplitter
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 CreateMultipleSubtasks.Configuration_CreateMultipleSubtasks
import com.atlassian.jira.project.Project
#BaseScript CustomEndpointDelegate delegate
callSubTaskCreator(httpMethod: "GET", groups: ["jira-users"]) {MultivaluedMap queryParams, String body ->
def log1 = Logger.getLogger("atlassian-jira.log")
log1.warn("MOUNA CAMELIA223");
Configuration_CreateMultipleSubtasks conf =new Configuration_CreateMultipleSubtasks()
def MultipleSubtasksHashMap= conf.getSubTaskCreatorHashMap()
String itracCreatemultiplesubtasksProjectCategoriesURL = MultipleSubtasksHashMap["itracCreatemultiplesubtasksProjectCategoriesURL"] as String;
log1.warn("MOUNA CAMELIA223 "+itracCreatemultiplesubtasksProjectCategoriesURL);
def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
def issueId = queryParams.getFirst("issueId") as Long
Issue myissue = ComponentAccessor.getIssueManager().getIssueObject(issueId)
def issueKey = myissue.getKey()
def itracCreatemultiplesubtasksProjectCategoriesURL2 = itracCreatemultiplesubtasksProjectCategoriesURL.replaceAll(":1:", issueKey)
def itracCreatemultiplesubtasksProjectCategoriesURL3 = itracCreatemultiplesubtasksProjectCategoriesURL2.replaceAll(":2:", user.getUsername())
log1.warn("The rest endpoint has been executed by the user " +user)
Response.temporaryRedirect(URI.create(itracCreatemultiplesubtasksProjectCategoriesURL3)).build()
}

Jira REST endpoint redirected to error page

I am using a REST endpoint and here is my code: and here are some screenshots. The first screenshot shows the declaration of the condition and the denclaration of the Link: /rest/scriptrunner/latest/custom/callSubTaskCreator?issueId=${issue.id}. The second screenshot shows the declaration of the REST endpoint. I am specifying the link of a file in the script editor. Finally, the last screenshot shows the error page where I am redirected after clicking on the link.
as you can see I am redirected to an error page after I click on the button. Anyone knows what is wrong?
package CreateMultipleSubtasks
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue;
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import org.apache.log4j.Logger
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.project.ProjectManager
#BaseScript CustomEndpointDelegate delegate
callSubTaskCreator(httpMethod: "GET", groups: ["jira-users"]) {
MultivaluedMap queryParams, String body ->
def log = Logger.getLogger("atlassian-jira.log")
log.warn("I AM HERE MOUNA 1");
ConfigurationCreateMultipleSubtasks conf = new ConfigurationCreateMultipleSubtasks()
def subTaskCreatorHashMap= conf.getSubTaskCreatorHashMap()
String itracCreatemultiplesubtasksProjectCategoriesURL = subTaskCreatorHashMap["itracCreatemultiplesubtasksProjectCategoriesURL"];
def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
Issue issue = ComponentAccessor.getIssueManager().getIssueObject(queryParams.getFirst("issueId") as Long)
def itracCreatemultiplesubtasksProjectCategoriesURL2 = itracCreatemultiplesubtasksProjectCategoriesURL.replaceAll(":1:", issue.getKey())
def itracCreatemultiplesubtasksProjectCategoriesURL3 = itracCreatemultiplesubtasksProjectCategoriesURL2.replaceAll(":2:", user.getUsername())
Response.temporaryRedirect(URI.create(itracCreatemultiplesubtasksProjectCategoriesURL3)).build()
 
}

REST endpoint working fine for in line script but failing when using script editor

I am using a REST endpoint in scriptrunner and here is the code that I am using. The code is working fine when using an in line script but fails when specifying the file in scriptrunner where the code lies as shown in the following picture: 
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue;
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.project.Project
import CreateMultipleSubtasks.Configuration_CreateMultipleSubtasks
#BaseScript CustomEndpointDelegate delegate
callSubTaskCreator(httpMethod: "GET", groups: ["jira-users"]) {MultivaluedMap queryParams, String body ->
def log1 = Logger.getLogger("atlassian-jira.log")
log1.warn("MOUNA CAMELIA223");
Configuration_CreateMultipleSubtasks conf =new Configuration_CreateMultipleSubtasks()
def MultipleSubtasksHashMap= conf.getSubTaskCreatorHashMap()
String itracCreatemultiplesubtasksProjectCategoriesURL = MultipleSubtasksHashMap["itracCreatemultiplesubtasksProjectCategoriesURL"] as String;
// String itracCreatemultiplesubtasksProjectCategoriesURL= "http://itrac-sustservice.eur.ad.sag:5555/web/subtaskmgr/Login.jsp?iTrac=:1:&reporter=:2:"
log1.warn("MOUNA CAMELIA223 "+itracCreatemultiplesubtasksProjectCategoriesURL);
 def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser
  def issueId = queryParams.getFirst("issueId") as Long
  Issue myissue = ComponentAccessor.getIssueManager().getIssueObject(issueId)
  def issueKey = myissue.getKey()
  def itracCreatemultiplesubtasksProjectCategoriesURL2 = itracCreatemultiplesubtasksProjectCategoriesURL.replaceAll(":1:", issueKey)
  def itracCreatemultiplesubtasksProjectCategoriesURL3 = itracCreatemultiplesubtasksProjectCategoriesURL2.replaceAll(":2:", user.getUsername())
  log1.warn("The rest endpoint has been executed by the user " +user)
  Response.temporaryRedirect(URI.create(itracCreatemultiplesubtasksProjectCategoriesURL3)).build()
 
}
This is what I get when I move the code into a file in the script editor. I am normally supposed to click on a button and move to a new page and this is what I get instead:
Anyone knows what needs to be changed and how this can be fixed?

Static type checking The variable jiraHelper/issue is undeclared

I am using a condition in my script in the REST endpoint and I am receiving the following errors:
Anyone knows what the problem is and what needs to be fixed?
Here is the code:
package CreateMultipleSubtasks
import org.apache.log4j.Logger
import com.atlassian.jira.plugin.webfragment.model.JiraHelper
import com.atlassian.jira.component.ComponentAccessor
def log1 = Logger.getLogger("atlassian-jira.log")
log1.warn("MOUNA BEFORE")
Configuration_CreateMultipleSubtasks conf = new Configuration_CreateMultipleSubtasks()
def subTaskCreatorHashMap= conf.getSubTaskCreatorHashMap()
String projectKey = subTaskCreatorHashMap["projectKey"];
String issueTypeName = subTaskCreatorHashMap["issueTypeName"];
log.warn("MOUNA PROJECT KEY "+ projectKey+" issuetypename "+issueTypeName)
def val=jiraHelper.project?.key==projectKey && issue.issueType.name==issueTypeName
return val

Simple REST endpoint and fragment not working Script function failed on issue: issue: PIE-68635, user: mouh, fieldId: __init__, file: <inline script>

I am using a really simple REST endpoint and its corresponding fragment and it is not working for me. 
The fragment I created is the following: 
Here is the error that I get printed in the log: 
2022-08-17 00:00:40,405+0200 https-openssl-nio-443-exec-297 ERROR mouh 0x69166x26 nxp314 10.248.27.109 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: issue: PIE-68635, user: mouh, fieldId: __init__, file: <inline script>, edit behaviour: https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/15, https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/17, https://itrac-dev.eur.ad.sag/plugins/servlet/scriptrunner/admin/behaviours/edit/21
2022-08-17 00:00:40,405+0200 https-openssl-nio-443-exec-297 ERROR mouh 0x69166x26 nxp314 10.248.27.109 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl]
Here is the code for the REST endpoint, the code is super simple as you can tell:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
#BaseScript CustomEndpointDelegate delegate
callMounaSubTaskCreator(
    httpMethod: "GET", groups: ["jira-administrators"]
) { MultivaluedMap queryParams, String body ->
    return Response.ok(new JsonBuilder([abc: 42]).toString()).build()
}

Resources