Static type checking

JM Perrot
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 26, 2023

Hi Guys, 

I'm new to ScriptRunner and want to check out the product. However, when I try to activate scripted field examples, I have a few problems.

The first comes from static type checking:

Screenshot 2023-10-26 at 16.49.53.png

What I understand from this message is that the fourth argument is not declared or something like that but issue is defined just below. For the moment I can't find the solution, there are a lot of docs on the server and the docs on the Cloud don't help me or I've missed something (it's very likely).

The second, perhaps related to the first, is that I can't activate the field (I can't save it).

Could someone tell me what I've done wrong?

 

Thank you very much and have a nice day.

// Fetch the issue object from the key using the Jira Software API
def issue = get("/rest/agile/1.0/issue/${issue.key}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

// Check if the issue is in a sprint and if so if the sprint is an active sprint
if (issue.fields.sprint && issue.fields.sprint.state == 'active') {

// Get the sprint start and end date strings from the API
String sprintStartDate = issue.fields.sprint.startDate
String sprintEndDate = issue.fields.sprint.endDate

// Format the date strings into a nicer format
String formattedSprintStartDate = sprintStartDate.substring(0, 10)
String formatterSprintEndDate = sprintEndDate.substring(0, 10)

// Set the dates in the scripted field
return "Sprint Start Date: ${formattedSprintStartDate} Sprint End Date: ${formatterSprintEndDate}"

// Return a default message if the issue is not in active sprint
} else {
return "The ${issue.key} issue is not currently in an active sprint"
}

 

2 answers

2 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
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.
October 26, 2023

With regards to status type checking errors, those are not always indications of bad code.

It's a failure of the editor/syntax checker to extrapolate and be able to guarantee that your code is 100% valid.

In this case

issue.fields.sprint.state

It definitely knows what "issue" is and knows that "fields" is a valid attribute/method for "issue".

It might even know that fields can contain arbitrary data, but that's as far as it goes. 

If you use issue.fields.summary, that's all there is, the summary is the final value.

So since the syntax checking is not sure what "sprint" is and whether it's more like summary or has other sub-fields. So it can't verify that "state" is a valid attribute for sprint.

I'm not super familiar with the Cloud implementation of Scriptrunner, but if the status type-checking error bothers you, you can try something like this:

// Check if the issue is in a sprint and if so if the sprint is an active sprint
def sprint = issue.fields.sprint as Map
if (sprint && sprint.state == 'active') {

The "as Map" will tell the static type checking that issue.fields.sprint is a Map that should contain additional sub-fields. So subsequent call to sprint.state will be allowed.

JM Perrot
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2023

Hi @Peter-Dave Sheehan

Tanks for your answer, your idea works well for the static checking.

Screenshot 2023-10-30 at 10.02.42.png

I understand the compiler logic better. Thanks for the tip ! 

1 vote
Answer accepted
Brandon Chin October 26, 2023

@JM Perrot Welcome to ScriptRunner! 

Your second issue is common as I experience the same thing when I first created a Scripted Field. You have to first test your script against an issue. You'll need to type for issues to appear. Start with the project key. One you test it then you will be able to save it. That may solve your first problem as I am able to run this example script fine. Let me know if this works for you. Screenshot 2023-10-26 at 11.47.05 AM.png

JM Perrot
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 27, 2023

Hello @Brandon Chin,

Thank you for your reply. This trick for saving work works, thank you! I was stupid, I never tried to type, I was just concentrating on my first problem. ^^

Brandon Chin October 27, 2023

@JM Perrot happy to help!

Given that you are new to ScriptRunner, if you are interested in a Demo feel free to reach out to customersuccess@scriptrunnerhq.com and address it to myself and I would be happy to go through all the features with you!

Like JM Perrot likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events