Groovy examples

On creating a Bug set its Affects Version/s to the most recently released version

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the Bug's workflow.
  • Select the Affects Version/s field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def versions = issue.getAvailableOptions("versions").findAll{
      it.isReleased()
    }
    def versionMap = [:]
    if (versions)
    {
      versions.each(){
        if(it.getReleaseDate())
        {
          versionMap.put(it?.getReleaseDate(),it.getName())
        }
      }
    }
    if(versionMap.min{it.key}?.getValue())
    {
      return versionMap.min{it.key}?.getValue()
    }
    else
    {
      return versions = []
    }
  • Place the post-function after Creates issues originally built-in post-function.

Set the Affects Version/s of the issue to Affects Version/s of all its linked issues

  • Add the Set field value to constant or Groovy expression post-function to the transition
  • Select the Affects Version/s field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    Set versions = []
    issue.get("issuelinks").each(){
      versions += it.getDestinationObject().get("versions")
    }
    return versions
  • Select Ignore empty values option

Assign the issue to the Project lead, if the issue is unassigned on creation

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Assignee field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    if(!issue.get("assignee"))
    {
      issue.get("project")?.getProjectLead()
    }
  • Select Ignore empty values option

  • Place the post-function after the Creates issue originally built-in post-function.

On creating an issue, pick the component of the issue from a cascading field that carries the Main and Sub-components

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue's workflow.

  • Select the Component/s field.

  • Select Groovy expression in the Value type.
  • Write the following content in it.

    import com.atlassian.jira.issue.fields.CustomField;
    import com.atlassian.jira.component.ComponentAccessor;
    CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Cascade");
    def value = issueObject.getCustomFieldValue(customField);
    return (value?.get(null)?.getValue() + "-" + value?.get("1"))
  • Place the post-function after the Creates issue originally built-in post-function.

Set the Component/s of the issue to components whose lead is the current user

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Components field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def components = issue.getAvailableOptions("components")
    def newComponents = components.findAll{
      it?.getLead().getKey() == currentUser.getKey()
    }
    return newComponents

On the creation of a sub-task add its summary to the description of its parent

  • Add the Set field value of linked issues post-function to the Create transition.
  • Select the Description field.
  • Select is subtask of in the Issue link type
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    org.codehaus.groovy.runtime.NullObject.metaClass.toString = {return ''}
    linkedIssue.get("description").toString() + "\n" + issue.get("summary")
  • Place the post-function after the Creates issue originally built-in post-function.

Set the due date to today's date

  • Add the Set field value to constant or Groovy expression post-function to the transition.
  • Select the Due date field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    new Date()

Set the due date to issue created plus five days

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Due Date field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    issue.get("created") + 5

On creating a Bug with High priority set its Fix Version/s to the upcoming release

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the Bug's workflow.
  • Select the Fix Version/s field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def versions = issue.getAvailableOptions("fixVersions")
    def versionMap = [:]
    if (versions)
    {
      versions.each(){
        if(it.getReleaseDate())
        {
          versionMap.put(it?.getReleaseDate(),it.getName())
        }
      }
    }
    if(versionMap.min{it.key}?.getValue())
    {
      return versionMap.min{it.key}?.getValue()
    }
    else
    {
      return versions = []
    }
  • Select the Conditional execution and write the following code

    issue.get("priority").getName() == "Critical"
  • Place the post-function after Creates issues originally built-in post-function.

Assign to an unreleased version whose release date is nearest to the issue's due date

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the Bug's workflow.
  • Select the Fix Version/s field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def versions = issue.getAvailableOptions("fixVersions")
    def duedate = issue.get("duedate")
    def versionMap = [:]
    
    if (duedate)
    {
      versions.each{
        if(it.getReleaseDate())
        {
          if(it.getReleaseDate() <= duedate)
          {
            diff = duedate - it.getReleaseDate()
            versionMap.put(diff,it.getName())
          }
          else
          {
            diff = it.getReleaseDate() - duedate
            versionMap.put(diff,it.getName())
          }
        }
      }
    }
    if(versionMap.min{it.key}?.getValue())
    {
      return versionMap.min{it.key}?.getValue()
    }
    else
    {
      return versions = []
    }
  • Place the post-function after the Creates issue originally built-in post-function.

Set the Original estimate of the issue to the difference of its creation date and Due date

  • Add the Set field value to constant or Groovy expression post-function to the transition.
  • Select the Original Estimate field.
  • Select Groovy expression in the Value type.
  • Write the either of the content in the Value section.

    if(issue?.get("duedate"))
    {
      Long days = issue?.get("duedate") - issue.get("created")
      return (days*8*60*60)
    }
    if(issue?.get("duedate"))
    {
      return issue?.get("duedate") - issue.get("created") + "d"
    }

Set the priority of the issue to Highest if the issue belongs to the "Customer Portal" component

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Priority field.
  • Select Groovy expression in the Value type.
  • Write Highest in the Value section.

  • Select the Conditional execution and write the following content:

    issue.get("components")?.first()?.getName() == "Customer Portal"
  • Place the post-function after the Creates issue originally built-in post-function.

On the creation of a Story, change its priority to that of the Epic if the Epic's priority is lower than the current priority of the Story.

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the Story workflow.
  • Select the Priority field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    if (issue?.getEpic()?.get("priority")?.getName() <= issue.get("priority").getName())
    {
      return issue?.getEpic()?.get("priority")
    }
  • Select Ignore empty value option

  • Place the post-function after the Creates issues originally built-in post-function.

Assign a reopened issue to the last person who commented it

  • Add the Set field value to constant or Groovy expression post-function to the Reopen transition of the issue workflow.
  • Select the Assignee field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def comments = issue.get("comment")
    if(comments)
    {
      return comments.last().getAuthorApplicationUser()
    }

Assign to the first security level of the project that can be set for an issue created in this project by the current user

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Security level field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def security = issue.getAvailableOptions("security")
    if (security)
    {
      return security.first()
    }
  • Place the post-function after the Creates issue originally built-in post-function.

On the creation of a Story add the reporter of its Epic to the Watchers

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Watchers field.
  • Select Groovy expression in the Value type.
  • Select Add source value(s) to destination field option
  • Write the following content in the Value section.

    issue.getEpic()?.get("reporter")
  • Place the post-function after the Creates issue originally built-in post-function.

Epic has a Story and the task has a sub-task. When the sub-task is reopened we create a bug in another project linking it to the sub-task. On creation of the Bug, set the Epic link field value to the same field of the Task

  • Add the Create/Clone issue post-function to the Reopen transition of the sub-task workflow.
  • Select the project from Project
  • Select the Bug in Issue type
  • Select is caused by in Link to new issue
  • Select the Epic Link field in Set fields of new issue.
  • Select Set field value from Groovy.
  • Write the following content in the Value section.

    if(issue.getLinkedIssues("is caused by"))
    {
      issue.getLinkedIssues("is caused by").first().getParentObject().get("Epic Link")
    }


Copy Jira service desk customers in a multi-user picker field to Request Participants ignoring the reporter of the issue

  • Add the Set field value to constant or Groovy expression post-function to the transition of the issue workflow.
  • Select the Request Participants field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def newUsers = issue.get("MUP").findAll{
      it.getName() != issue.get("reporter").getName()
    }
    return newUsers

Auto select the installation tasks in a Task checklist after the Installations are done

  • Add the Set field value to constant or Groovy expression post-function to the transition that leads to Installations complete status
  • Select the Tasks checklist field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    return issue.getAvailableOptions("Tasks checklist").findAll{
      it.getValue().contains("Installation")
    }

If a bug is raised with a major outage and is critical select all the customers as impacted

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Customers field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    issue.getAvailableOptions("Customers")
  • Select the Conditional execution check box and write the following code.

    issue.get("priority").getName() == "Critical" && issue.get("summary").contains("outage")
  • Place the post-function after the Creates issue originally built-in post-function.

Update a custom group picker field to the first group the assignee belongs to

  • Add the Set field value to constant or Groovy expression post-function to the transition.
  • Select the Single group picker field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.security.groups.GroupManager
    if(issue.get("assignee"))
    {
      return ComponentAccessor.getGroupManager().getGroupsForUser(issue.get("assignee")?.getName())?.first()
    }

Update a custom multi-group picker field to all the groups the current user belongs to

  • Add the Set field value to constant or Groovy expression post-function to the transition.
  • Select the Multi group picker field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.security.groups.GroupManager
    return ComponentAccessor.getGroupManager().getGroupsForUser(currentUser.getName())

On creating an issue, Copy the component leads of the selected components to a Multi-user picker field

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Multi-user picker field.
  • Select the Add source value(s) to destination field option
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    def leads = []
    issue.get("components").each{
      if(it.getComponentLead())
      {
         leads+=it?.getComponentLead()
      }
    }
    return leads
  • Select Ignore empty values option.

  • Place the post-function after the Creates issue originally built-in post-function.

Save the author of the "Approve" transition in a custom single-user picker field

  • Add the Set field value to constant or Groovy expression post-function to the "Approve" transtion.
  • Select the Single-user picker type field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    currentUser


Set a date picker field of a Bug to the Fix Version/s release date

  • Add the Set field value to constant or Groovy expression post-function to the Create transition of the issue workflow.
  • Select the Date picker field.
  • Select Groovy expression in the Value type.
  • Write the following content in the Value section.

    if (issue.get("fixVersions"))
    {
      return issue.get("fixVersions").last().getReleaseDate()
    }