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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,552,028
Community Members
 
Community Events
184
Community Groups

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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 31, 2020

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

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 01, 2020

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?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 01, 2020

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
AUG Leaders

Atlassian Community Events