Groovy sample scripts

Groovy sample scripts

ISO formatted date

import java.text.SimpleDateFormat SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd") def today = format.format( new Date() )

RFC3339 date

import java.text.SimpleDateFormat //2020-10-03T10:00:00Z SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") def today = format.format( new Date() )

 

Add 10 minutes

import java.text.SimpleDateFormat import groovy.time.TimeCategory //2020-10-03T10:00:00Z SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") def currentDate = new Date() def after30Mins use( TimeCategory ) { after30Mins = currentDate + 10.minutes } def today = format.format(after30Mins)

Subtract 10 minutes

import java.text.SimpleDateFormat import groovy.time.TimeCategory //2020-10-03T10:00:00Z SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") def currentDate = new Date() def after30Mins use( TimeCategory ) { after30Mins = currentDate - 10.minutes } def today = format.format(after30Mins)

more options on time are:

Extract insight key from custom field value.

When you do a GET on an issue containing insight custom field value it will return an array of values like this [“My object (thekey-123)“,”My other object (thekey-456)”] to extract the key from that use a groovy variable with the lines below.

The action structure will look like this:

  1. Add a GET request on the issue, this will return the issue as a json representation

  2. add an iterator with $.fields.customfield_10234 (the id of the custom field that holds the insight values)

  3. Add a POST create issue request with the body below

    { "fields": { "project": { "id": "7543" }, "summary": "The summary", "issuetype": { "id": "3" }, "customfield_11230":"{{issue.key}}", "customfield_10234": [{"key": "{{extractInsightKey}}"}] } }

 

 

Groovy variable {{extractInsightKey}} It will return thekey-123 and thekey-456

def regexp = /\(([^\)]+)\)/ def matcher = response =~ regexp return matcher.getAt(0)[1]

 

Adding a custom field option

The groovy code adds a new custom field option, add it to a groovy variable and use the response to set line 9, value variable.

import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.util.I18nHelper; def customFieldManager = ComponentAccessor.getCustomFieldManager() def optionsManager = ComponentAccessor.getOptionsManager() def value = "HELLO WORLD!" def updField = customFieldManager.getCustomFieldObject("customfield_10214") def fieldConfig = updField.getRelevantConfig(issue) def currentOptions = optionsManager.getOptions(fieldConfig) def newSeqId = currentOptions*.sequence.max() - 1 def option = optionsManager.createOption(fieldConfig, null, newSeqId, value)