Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Getting issue key and project name from a REST endpoint

Hello, I am trying to create a rest endpoint script that retrieves an issue key and then writes this key to a separate text file. The idea is that I have a premade text file with "#issueKey" in the text file, and I want to replace #issueKey with the actual issue key of the selected issue.

What I've done is put a web item into an issue so that a button appears in the "operations-restore" section of the issue, and when that button is pressed on, the rest endpoint script will run, retrieving the issue key and project name. However, I can't seem to retrieve the issue key. Here is my code 


import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.ws.rs.core.Response

import javax.ws.rs.core.MultivaluedMap

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.util.json.JSONObject


@BaseScript CustomEndpointDelegate delegate
WriteToDocument(httpMethod: "GET") { MultivaluedMap queryParams, String body ->
String issueKey = queryParams.getFirst("issueKey")

String defaultIssueKey = "#issueKey"

 

File myFile = new File("C:/Users/name/Desktop/scriptrunner.txt")
String fileTest = myFile.text;
fileTest = fileTest.replaceAll(defaultIssueKey, issueKey);

new File ("C:/Users/name/Desktop", "newScriptRunner.txt").withWriter('utf-8') {
writer -> writer.writeLine(fileTest);
}
}

 

When I run this, I get this error 

{"message":null,"stack-trace":"java.lang.NullPointerException\r\n\tat Script75$_run_closure1.doCall(Script75.groovy:34)\r\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint$_doEndpoint_closure2.doCall(UserCustomScriptEndpoint.groovy:225)\r\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint$_doEndpoint_closure2.doCall(UserCustomScriptEndpoint.groovy)\r\n\tat com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy:345)\r\n\tat com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy)\r\n\tat com.onresolve.scriptrunner.runner.ScriptExecutionRecorder.withRecording(ScriptExecutionRecorder.groovy:13)\r\n\tat com.onresolve.scriptrunner.runner.ScriptExecutionRecorder$withRecording$0.call(Unknown Source)\r\n\tat com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl.execute(DiagnosticsManagerImpl.groovy:343)\r\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:215)\r\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.getUserEndpoint(UserCustomScriptEndpoint.groovy:108)\r\n","status-code":"INTERNAL_SERVER_ERROR"}

 

I'm fairly new to ScriptRunner so I'm not even sure how to diagnose this error and I don't have much familiarity with all the imports so I'm kind of stuck here. I would really appreciate if anyone could help me diagnose this. Thanks

1 answer

1 accepted

0 votes
Answer accepted

You probably did some cleanup on your code in order to post ...

The relevant part of the error is this:

java.lang.NullPointerException 
at Script75$_run_closure1.doCall(Script75.groovy:34)

That means that on line 34 of your script (a in-line scripts get automatic names like Scriptxx.groovy), something returned a null value that the compiler didn't know how to handle.

But the code you pasted only includes 31 lines total... so that makes it hard to identify the source of the issue.

But seeing as there is nothing directly wrong with your script, I would check to make sure that the run-as user for the jira app has permission to read/write to the 'C:/Users/name/Desktop' directory.

Thanks for the response, I really appreciate it.

 

I realize now that I did cut out some parts of the code before posting. Those parts however, were commented out and I still received this issue. I looked at line 34 of my script and the line of code that creates the nullpointerexception is 

fileTest = fileTest.replaceAll(defaultIssueKey, issueKey);

 

From this, I can imagine that when I try to retrieve the issueKey, it's returning Null, so there's something wrong with how I am retrieving the issue key? 

I did some further testing and if I were to replace 'issueKey' with a string like the following line of code

fileTest = fileTest.replaceAll(defaultIssueKey, "Test");

this code will run and write a text file to my desktop as I want it to, so I believe the error has nothing to do with the Jira app permissions. 

Thanks again

Issue key appears to be retrieved from the query parameters. 

So that means you should be calling your endpoint with <jirabaseurl>/rest/scriptrunner/latest/custom/WriteToDocument?issueKey=ABC-123

I always add validation to my query parameters for custom rest endpoints:

String issueKey = queryParams.getFirst("issueKey")
if
(!issueKey){
return Response.serverError().entity([error: "Missing parameter: issueKey is required"]).build()
}

Note that getFirst("issueKey") is case sensitive, so WriteToDocument?issuekey=ABC-123 will not work.

Thanks for your help, I realized that the error was caused by my query parameters being written incorrectly. I have one last question, how would I go about retrieving the issue's project name? Could I also retrieve this from the query parameters or is there a function that allows me to do it from within the script?

Yeah, just like any other scriptrunner script, you have full access to all the Jira api. So, using the componentAccessor we can get te issueManager.

Something like this:

String issueKey = queryParams.getFirst("issueKey")
if
(!issueKey){
return Response.serverError().entity([error: "Missing parameter: issueKey is required"]).build()
}
def issue = ComponentAccessor.issueManager.getIssueObject(issueKey)
if(!issue){
return Response.serverError().entity([error: "Bad parameter: issueKey was not found in jira"]).build()
}
def project = issue.projectObject
log.debug "Project Name = $project.name"
log.debug "Project Key = $project.key"

Thanks for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

102 views 1 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you