Groovy Script assistance

Norm Moore December 13, 2023

Hi All,

I'm looking for some help with a groovy script, I've converted the script from Python to Groovy, but when I put the script into Scriptrunner it gives me an error that I'm unfamiliar with. 

Screenshot 2023-12-13 111114.png

Above is the error I'm getting. Below is the snippet from the script where the error appears on line 82 "def fixStartDate(jiraTicket, s_startDate, s_endDate, startDate) {

Screenshot 2023-12-13 112148.png

Any help would be appreciated!

Norm

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 19, 2023

Hi @Norm Moore

Could you please share more information, i.e. what are you trying to do with your Script, and what is the expected result? What ScriptRunner component are you trying to run your Script on?

Please share a full copy of your code so I can review it and try to help.

Also, from the screenshot that you have provided, it seems like you are trying to create a method where the formal parameters do not have any specific type set to them. This will not work.

You need to modify it to something like:-

def fixStartDate(String jiraTicket, Date s_startDate, Date s_endDate, Date startDate) {
// Your code
}

Also, the variable time that you are using in your code, has no form of declaration. This will not work. You need to specify a variable type for the time variable or alternatively, if you want it to be dynamic, you need to declare it as:-

def time = ""

In addition to that, there is no such method called strptime in Java or Groovy. If you are using this to format the Date, then you will need to use something like:-

def pattern = 'yyyy-MM-dd'T'HH:mm:ss'
def formatter = DateTimeFormatter.forPattern(pattern)
def localDateTime = formatter.parseLocalDateTime(text)

You can refer to this StackOverflow discussion for more information.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

Norm Moore December 19, 2023

Hi Ram,

Thank you for getting back to me, I've attached the original code (Python) and the converted code (Groovy). 

I was trying to run this through SR Console

The end goal with the code is to align the Start Date and End Date within our Jira tickets, that are missing the dates. If the ticket is associated to a sprint, we wanted to have the dates align with the sprint dates. If the ticket was standalone, we wanted to have the dates align with the created and resolved dates. 

Thank you for your assistance!

 

Python Code

import json, time, re, sys, logging
import requests

def get_earliest_date(dates_list):
if not dates_list:
return ''
earliest_date = dates_list[0]
if len(dates_list) == 1:
return earliest_date
earliest_date = time.strptime(earliest_date, '%Y-%m-%d')
for date in dates_list[1:]:
date = time.strptime(date, '%Y-%m-%d')
if date < earliest_date:
earliest_date = date
return time.strftime('%Y-%m-%d', earliest_date)


def get_latest_date(dates_list):
if not dates_list:
return ''
latest_date = dates_list[0]
if len(dates_list) == 1:
return latest_date
latest_date = time.strptime(latest_date, '%Y-%m-%d')
for date in dates_list[1:]:
date = time.strptime(date, '%Y-%m-%d')
if date > latest_date:
latest_date = date
return time.strftime('%Y-%m-%d', latest_date)


issues = "[LIST OF TICKETS RETRIEVED WITH JQL FILTER AND REST API]"

for issue in issues:
jiraTicket = {}

jiraTicket['key'] = issue.get('key')
startDate = issue['fields'].get('customfield_13106')
endDate = issue['fields'].get('customfield_13107')

jiraTicket['dueDate'] = issue['fields'].get('duedate')
if jiraTicket['dueDate']:
jiraTicket['dueDate'] = jiraTicket['dueDate'][:10]

jiraTicket['created_date'] = issue['fields'].get('created')
if jiraTicket['created_date']:
jiraTicket['created_date'] = jiraTicket['created_date'][:10]

jiraTicket['last_updated'] = issue['fields'].get('updated')
if jiraTicket['last_updated']:
jiraTicket['last_updated'] = jiraTicket['last_updated'][:10]

resolutiondate = issue['fields']['resolutiondate']
if resolutiondate:
resolutiondate = resolutiondate[:10]
jiraTicket['resolution_date'] = resolutiondate

sprint_name = []
sprint_ID = []
sprint_state = set()
sprint_startDate = []
sprint_endDate = []
sprint_completeDate = []

if issue['fields']['customfield_10800']:
for s in issue['fields']['customfield_10800']:
if not s:
continue
s = re.sub('^com\.[^\[]*\[', '', s)
s_startDate = re.search('(?<=startDate=)[^,]*', s).group(0)
if s_startDate == '<null>':
s_startDate = ""
if s_startDate:
s_startDate = s_startDate[:10]
sprint_startDate.append(s_startDate)
s_endDate = re.search('(?<=endDate=)[^,]*', s).group(0)
if s_endDate == '<null>':
s_endDate = ""
if s_endDate:
s_endDate = s_endDate[:10]
sprint_endDate.append(s_endDate)

s_startDate = get_earliest_date(sprint_startDate)
jiraTicket['sprint_startDate'] = s_startDate
s_endDate = get_latest_date(sprint_endDate)
jiraTicket['sprint_endDate'] = s_endDate

# fix start date
created = time.strptime(jiraTicket['created_date'], '%Y-%m-%d')

if s_startDate:
s_startDate = time.strptime(s_startDate, '%Y-%m-%d')

if s_endDate:
s_endDate = time.strptime(s_endDate, '%Y-%m-%d')

if startDate:
startDate = startDate[:10]
startDate = time.strptime(startDate, '%Y-%m-%d')
# correct start date
if s_startDate and s_endDate:
if startDate < s_startDate:
startDate = s_startDate
elif startDate > s_endDate:
startDate = s_startDate
elif startDate < created:
startDate = created
else:
# fill in start date
if s_startDate:
startDate = s_startDate
else:
startDate = created
if type(startDate) == time.struct_time:
startDate = time.strftime('%Y-%m-%d', startDate)
jiraTicket['startDate'] = startDate

# fix end date
if resolutiondate:
endDate = resolutiondate
elif s_endDate:
if not endDate:
endDate = s_endDate
else:
endDate = endDate[:10]
endDate = time.strptime(endDate, '%Y-%m-%d')
if endDate > s_endDate:
endDate = s_endDate
elif not endDate:
endDate = jiraTicket['last_updated']
if type(endDate) == time.struct_time:
endDate = time.strftime('%Y-%m-%d', endDate)
jiraTicket['endDate'] = endDate

 

jiraTicket['key'] = issue.get('key')
startDate = issue['fields'].get('customfield_13106')
endDate = issue['fields'].get('customfield_13107')

jiraTicket['dueDate'] = issue['fields'].get('duedate')
if jiraTicket['dueDate']:
jiraTicket['dueDate']=jiraTicket['dueDate'][:10]

jiraTicket['created_date'] = issue['fields'].get('created')
if jiraTicket['created_date']:
jiraTicket['created_date'] = jiraTicket['created_date'][:10]

jiraTicket['last_updated'] = issue['fields'].get('updated')
if jiraTicket['last_updated']:
jiraTicket['last_updated'] = jiraTicket['last_updated'][:10]

resolutiondate = issue['fields']['resolutiondate']
if resolutiondate:
resolutiondate = resolutiondate[:10]
jiraTicket['resolution_date'] = resolutiondate

sprint_name = []
sprint_ID = []
sprint_state = set()
sprint_startDate = []
sprint_endDate = []
sprint_completeDate = []

if issue['fields']['customfield_10800']:
for s in issue['fields']['customfield_10800']:
if not s:
continue
s = re.sub('^com\.[^\[]*\[', '', s)
s_name = re.search('(?<=name=)[^,]*', s).group(0)
sprint_name.append(s_name)
s_ID = re.search('(?<=rapidViewId=)[^,]*', s).group(0)
sprint_ID.append(s_ID)
s_state = re.search('(?<=state=)[^,]*', s).group(0)
sprint_state.add(s_state)
s_startDate = re.search('(?<=startDate=)[^,]*', s).group(0)
if s_startDate == '<null>':
s_startDate = ""
if s_startDate:
s_startDate=s_startDate[:10]
sprint_startDate.append(s_startDate)
s_endDate = re.search('(?<=endDate=)[^,]*', s).group(0)
if s_endDate == '<null>':
s_endDate = ""
if s_endDate:
s_endDate=s_endDate[:10]
sprint_endDate.append(s_endDate)

jiraTicket['sprint'] = ','.join(sprint_name)
jiraTicket['sprintID'] = ','.join(sprint_ID)
#todo: get single value
sprint_state = list(sprint_state)
if sprint_state:
if 'ACTIVE' in sprint_state:
jiraTicket['sprint_state'] = 'ACTIVE'
elif 'FUTURE' in sprint_state:
jiraTicket['sprint_state'] = 'FUTURE'
else:
jiraTicket['sprint_state'] = 'CLOSED'
else:
jiraTicket['sprint_state'] = ""
s_startDate = get_earliest_date(sprint_startDate)
jiraTicket['sprint_startDate'] = s_startDate
s_endDate = get_latest_date(sprint_endDate)
jiraTicket['sprint_endDate'] = s_endDate
#jiraTicket['sprint_completeDate'] = get_latest_date(sprint_completeDate)

# fix start date
created = time.strptime(jiraTicket['created_date'], '%Y-%m-%d')

if s_startDate:
s_startDate = time.strptime(s_startDate, '%Y-%m-%d')

if s_endDate:
s_endDate = time.strptime(s_endDate, '%Y-%m-%d')

if startDate:
startDate = startDate[:10]
startDate = time.strptime(startDate, '%Y-%m-%d')
# correct start date
if s_startDate:
if startDate < s_startDate:
startDate = s_startDate
elif s_endDate:
if startDate > s_endDate:
startDate = s_startDate
elif startDate < created:
startDate = created
else:
# fill in start date
if s_startDate:
startDate = s_startDate
else:
startDate = created
if type(startDate) == time.struct_time:
startDate = time.strftime('%Y-%m-%d', startDate)
jiraTicket['startDate'] = startDate

# fix end date
if resolutiondate:
endDate = resolutiondate
elif s_endDate:
if not endDate:
endDate = s_endDate
else:
endDate = endDate[:10]
endDate = time.strptime(endDate, '%Y-%m-%d')
if endDate > s_endDate:
endDate = s_endDate
elif not endDate:
endDate = jiraTicket['last_updated']
if type(endDate) == time.struct_time:
endDate = time.strftime('%Y-%m-%d', endDate)
jiraTicket['endDate'] = endDate

Groovy Code:

import groovy.json.JsonSlurper
import java.text.SimpleDateFormat

def getEarliestDate(datesList) {
if (!datesList) {
return ''
}
def earliestDate = datesList[0]
if (datesList.size() == 1) {
return earliestDate
}
earliestDate = new SimpleDateFormat('yyyy-MM-dd').parse(earliestDate)
for (date in datesList[1..-1]) {
def parsedDate = new SimpleDateFormat('yyyy-MM-dd').parse(date)
if (parsedDate < earliestDate) {
earliestDate = parsedDate
}
}
return new SimpleDateFormat('yyyy-MM-dd').format(earliestDate)
}

def getLatestDate(datesList) {
if (!datesList) {
return ''
}
def latestDate = datesList[0]
if (datesList.size() == 1) {
return latestDate
}
latestDate = new SimpleDateFormat('yyyy-MM-dd').parse(latestDate)
for (date in datesList[1..-1]) {
def parsedDate = new SimpleDateFormat('yyyy-MM-dd').parse(date)
if (parsedDate > latestDate) {
latestDate = parsedDate
}
}
return new SimpleDateFormat('yyyy-MM-dd').format(latestDate)
}

def issues = "[LIST OF TICKETS RETRIEVED WITH JQL FILTER AND REST API]"

for (issue in issues) {
def jiraTicket = [:]
jiraTicket['key'] = issue.key
def startDate = issue.fields.customfield_13106
def endDate = issue.fields.customfield_13107
jiraTicket['dueDate'] = issue.fields.duedate ? issue.fields.duedate[0..9] : null
jiraTicket['created_date'] = issue.fields.created ? issue.fields.created[0..9] : null
jiraTicket['last_updated'] = issue.fields.updated ? issue.fields.updated[0..9] : null
def resolutionDate = issue.fields.resolutiondate ? issue.fields.resolutiondate[0..9] : null
jiraTicket['resolution_date'] = resolutionDate
def sprintStartDate = []
def sprintEndDate = []
if (issue.fields.customfield_10800) {
issue.fields.customfield_10800.each { s ->
if (s) {
def parsedS = s.replaceAll('^com\\.[^\\[]*\\[', '')
def sStartDate = parsedS.find(/(?<=startDate=)[^,]*/)
if (sStartDate == '<null>') {
sStartDate = ''
}
if (sStartDate) {
sprintStartDate << sStartDate[0..9]
}
def sEndDate = parsedS.find(/(?<=endDate=)[^,]*/)
if (sEndDate == '<null>') {
sEndDate = ''
}
if (sEndDate) {
sprintEndDate << sEndDate[0..9]
}
}
}
}
/**
* Fix start date
* @Param jiraTicket the Jira ticket object
* @Param s_startDate the start date string
* @Param s_endDate the end date string
* @Param startDate the start date string
*/
def fixStartDate(jiraTicket, s_startDate, s_endDate, startDate) {
def created = time.strptime(jiraTicket['created_date'], '%Y-%m-%d')
if (s_startDate) {
s_startDate = time.strptime(s_startDate, '%Y-%m-%d')
}
if (s_endDate) {
s_endDate = time.strptime(s_endDate, '%Y-%m-%d')
}
if (startDate) {
startDate = startDate.substring(0, 10)
startDate = time.strptime(startDate, '%Y-%m-%d')
// Correct start date
if (s_startDate && s_endDate) {
if (startDate < s_startDate) {
startDate = s_startDate
} else if (startDate > s_endDate) {
startDate = s_startDate
}
} else if (startDate < created) {
startDate = created
}
} else {
// Fill in start date
if (s_startDate) {
startDate = s_startDate
} else {
startDate = created
}
}
if (startDate instanceof time.struct_time) {
startDate = time.strftime('%Y-%m-%d', startDate)
}
jiraTicket['startDate'] = startDate
}
import java.text.SimpleDateFormat
import java.util.Date

// fix end date
def fixEndDate(issue, jiraTicket) {
def resolutiondate = issue['fields']['resolutiondate']
def s_endDate = issue['fields'].get('customfield_13107')
def endDate = null

if (resolutiondate) {
endDate = resolutiondate
} else if (s_endDate) {
if (!endDate) {
endDate = s_endDate
} else {
endDate = endDate[:10]
def dateFormat = new SimpleDateFormat('yyyy-MM-dd')
def endDateDate = dateFormat.parse(endDate)
def sEndDateDate = dateFormat.parse(s_endDate)
if (endDateDate.after(sEndDateDate)) {
endDate = s_endDate
}
}
} else if (!endDate) {
endDate = jiraTicket['last_updated']
}

if (endDate instanceof Date) {
def dateFormat = new SimpleDateFormat('yyyy-MM-dd')
endDate = dateFormat.format(endDate)
}

jiraTicket['endDate'] = endDate
}

def issue = // assign the issue object
def jiraTicket = [:]

jiraTicket['key'] = issue.get('key')
def startDate = issue['fields'].get('customfield_13106')
def endDate = issue['fields'].get('customfield_13107')
jiraTicket['dueDate'] = issue['fields'].get('duedate')?.take(10)
jiraTicket['created_date'] = issue['fields'].get('created')?.take(10)
jiraTicket['last_updated'] = issue['fields'].get('updated')?.take(10)
fixEndDate(issue, jiraTicket)
def resolutiondate = issue['fields']['resolutiondate']?.take(10)
jiraTicket['resolution_date'] = resolutiondate

def sprint_name = []
def sprint_ID = []
def sprint_state = []
def sprint_startDate = []
def sprint_endDate = []
def sprint_completeDate = []

if (issue['fields']['customfield_10800']) {
issue['fields']['customfield_10800'].each { s ->
if (s) {
s = s.replaceAll('^com\\.[^\\[]*\\[', '')
def s_name = s.find(/(?<=name=)[^,]*/)
sprint_name.add(s_name)
def s_ID = s.find(/(?<=rapidViewId=)[^,]*/)
sprint_ID.add(s_ID)
def s_state = s.find(/(?<=state=)[^,]*/)
sprint_state.add(s_state)
def s_startDate = s.find(/(?<=startDate=)[^,]*/)
if (s_startDate == '<null>') {
s_startDate = ""
}
if (s_startDate) {
sprint_startDate.add(s_startDate.take(10))
}
def s_endDate = s.find(/(?<=endDate=)[^,]*/)
if (s_endDate == '<null>') {
s_endDate = ""
}
if (s_endDate) {
sprint_endDate.add(s_endDate.take(10))
}
}
}
}

jiraTicket['sprint'] = sprint_name.join(',')
jiraTicket['sprintID'] = sprint_ID.join(',')

// todo: get single value
if (sprint_state) {
if ('ACTIVE' in sprint_state) {
jiraTicket['sprint_state'] = 'ACTIVE'
} else if ('FUTURE' in sprint_state) {
jiraTicket['sprint_state'] = 'FUTURE'
} else {
jiraTicket['sprint_state'] = 'CLOSED'
}
} else {
jiraTicket['sprint_state'] = ""
}

def getEarliestDate(dates) {
dates.min()
}

def getLatestDate(dates) {
dates.max()
}

jiraTicket['sprint_startDate'] = getEarliestDate(sprint_startDate)
jiraTicket['sprint_endDate'] = getLatestDate(sprint_endDate)
// jiraTicket['sprint_completeDate'] = getLatestDate(sprint_completeDate)

import java.text.SimpleDateFormat
import java.util.Date

// fix start date
def created = new SimpleDateFormat('yyyy-MM-dd').parse(jiraTicket['created_date'])
if (s_startDate) {
s_startDate = new SimpleDateFormat('yyyy-MM-dd').parse(s_startDate)
}
if (s_endDate) {
s_endDate = new SimpleDateFormat('yyyy-MM-dd').parse(s_endDate)
}
if (startDate) {
startDate = startDate.substring(0, 10)
startDate = new SimpleDateFormat('yyyy-MM-dd').parse(startDate)
// correct start date
if (s_startDate) {
if (startDate.before(s_startDate)) {
startDate = s_startDate
}
} else if (s_endDate) {
if (startDate.after(s_endDate)) {
startDate = s_startDate
}
} else if (startDate.before(created)) {
startDate = created
}
} else {
// fill in start date
if (s_startDate) {
startDate = s_startDate
} else {
startDate = created
}
}
if (startDate instanceof Date) {
startDate = new SimpleDateFormat('yyyy-MM-dd').format(startDate)
}
jiraTicket['startDate'] = startDate

// fix end date
if (resolutiondate) {
endDate = resolutiondate
} else if (s_endDate) {
if (!endDate) {
endDate = s_endDate
} else {
endDate = endDate[0..9]
endDate = time.strptime(endDate, '%Y-%m-%d')
if (endDate > s_endDate) {
endDate = s_endDate
}
}
} else if (!endDate) {
endDate = jiraTicket['last_updated']
}
if (endDate instanceof time.struct_time) {
endDate = time.strftime('%Y-%m-%d', endDate)
}
jiraTicket['endDate'] = endDate

jiraTicket['sprint_startDate'] = getEarliestDate(sprintStartDate)
jiraTicket['sprint_endDate'] = getLatestDate(sprintEndDate)
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20
TAGS
AUG Leaders

Atlassian Community Events