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

Complex validation ScriptRunner

Daniel Alonso January 26, 2022

Hi,

This is my scenario:

I have some custom fields that control the percentage of an author over a song, those fields are:

SXNY - Song #X, Name Y -> X could go from 1 to 8, and Y goes from 1 to 10.

I created this script on the Script Console that returns 'true' or 'false' if the sum of the percentage of each song is different that 100 %

def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body as Map


def customFields = get("/rest/api/2/field") //Function to get all custom field Ids
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>


def song_author_id = ""
def song_author_value = ""
def song_perc_id = ""
def song_perc_value = ""
def next_song_id = ""
def next_song_value = ""

for (int song_number = 1; song_number<=8; song_number++){
//logger.info(song_number.toString())

def song_percentages = []
song_author_id = customFields.find { it.name == "S${song_number}N1" }?.id
song_author_value = issue.fields.get(song_author_id)
logger.info("Song number: ${song_number}")

next_song_id = customFields.find { it.name == "Add Song ${song_number + 1}" }?.id
next_song_value = issue.fields.get(song_author_id)

def author_num = 1
while (song_author_value != null){
song_perc_id = customFields.find { it.name == "PS${song_number}N${author_num}" }?.id
song_percentages.add(issue.fields.get(song_perc_id))
author_num ++
song_author_id = customFields.find { it.name == "S${song_number}N${author_num}" }?.id
song_author_value = issue.fields.get(song_author_id)
logger.info("S${song_number}N${author_num}")
//logger.info(song_author_value)
}
def total_percentages = 0
for (song_percentage in song_percentages){
total_percentages = total_percentages + song_percentage
}
if (total_percentages != 100) {
logger.info("Total percentage in Song ${song_number} is not 100%, please validate, current value: ${total_percentages}")
return false
}
else if(next_song_value == "No"){
return false
}
else{
logger.info("Song ${song_number} looks good, starting analysis on next song")
}
}
return true

 I then realized that I cannot use this kind of script on Workflow validation and it's limited to using Jira Expressions, can someone help me 'translate' this to Jira Expressions? Looks very complex to use some of the examples that I've seen.

Thanks in advance.

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events