Groovy Script Update Help!

Elif Alverson February 27, 2017

Hello, 

I am asked to update the script that  Thanos Batagiannis [Adaptavist] has provided. Please see it below.

We would like to have subtask 1 and 2 with descriptions as it stated below ( samples)  every time we receive new employee ticket. Is it possible to have subtask 1 and 2 with the descriptions ?

Your help is greatly appreciated.

Description for Subtask 1:

To Do List 1

  • Active Application Add-ins     Lists the extensions that are registered and currently running in your Office program.
  • Inactive Application Add-ins     Lists the add-ins that are present on your computer but are not currently loaded. For example, smart tags or XML Schemas are active only when the document that references them is open. Another example is the COM add-ins that are listed in the COM Add-ins dialog box. If the check box for a COM add-in is selected, the add-in is active. If the check box for a COM add-in is cleared, the add-in is inactive. To learn how to open the COM Add-in dialog box, see the section called Turn off or manage the installed add-ins.
  • Document Related Add-ins     Lists template files that are referenced by currently open documents.
  • Disabled Application Add-ins     Lists add-ins that were automatically disabled because they are causing Office programs to crash.

Description for Subtask 2:

To Do list 2:

  • Active Application Add-ins     Lists the extensions that are registered and currently running in your Office program.
  • Inactive Application Add-ins     Lists the add-ins that are present on your computer but are not currently loaded. For example, smart tags or XML Schemas are active only when the document that references them is open. Another example is the COM add-ins that are listed in the COM Add-ins dialog box. If the check box for a COM add-in is selected, the add-in is active. If the check box for a COM add-in is cleared, the add-in is inactive. To learn how to open the COM Add-in dialog box, see the section called Turn off or manage the installed add-ins.
  • Document Related Add-ins     Lists template files that are referenced by currently open documents.
  • Disabled Application Add-ins     Lists add-ins that were automatically disabled because they are causing Office programs to crash.

Here is the script below;

import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
  
def parentIssue = event.issue
if (parentIssue.getIssueType().getName() != 'New Employee')
    return
  
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summariesList = ["Subtask 1",
                     "Subtask 2",
                     "Notify the Risk Team of New Employee",
                     "Subtask 3"]
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
  
Issue Notify_the_Risk_Team_of_New_Hire
  
//here we fill an array with all required watchers
List<ApplicationUser> newWatchers = new ArrayList<>()
newWatchers.add(userManager.getUserByName("susie"))
newWatchers.add(userManager.getUserByName("elif"))
newWatchers.add(userManager.getUserByName("luiz"))
  
  
summariesList.each { subTaskSummary ->
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(subTaskSummary)
    newSubTask.setParentObject(parentIssue)
    newSubTask.setPriorityId(constantManager.getPriorities().find {
        it.getName() == "Major"
    }?.id)
  
    newSubTask.setProjectObject(parentIssue.getProjectObject())
    newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
        it.getName() == "Sub-task"
    }.id)
    // Add any other fields you want for the newly created sub task
  
    //if the summary is Notify¬_the_Risk_Team_of_New_Hire then
    if (subTaskSummary.equals("Notify the Risk Team of New Hire")) {
        def assignee = userManager.getUserByName("susie")
        newSubTask.setAssignee(assignee)
        Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
        Notify_the_Risk_Team_of_New_Hire = issueManager.createIssueObject(user, newIssueParams) as Issue
        subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    } else {
        Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
        issueManager.createIssueObject(user, newIssueParams)
        subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    }
  
    log.info "Issue with summary ${newSubTask.summary} created"
}
  
if (Notify_the_Risk_Team_of_New_Hire) {
    newWatchers.each {
        watcherManager.startWatching(it, Notify_the_Risk_Team_of_New_Hire)
    }
}

1 answer

1 accepted

1 vote
Answer accepted
Jonny Carter
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.
February 28, 2017

It's pretty straightforward to set the description on an issue. In your case, after you create the newSubTask variable as an issue, just use the .setDescription method (or Groovier yet .description=):

newSubTask.description = """My Multiline
Description
here"""

Since you're iterating through a list of summaries, you may need to change your data structure a bit so that you can specify both the description and the summary. A simple Map would probably do it:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
def parentIssue = event.issue
if (parentIssue.getIssueType().getName() != 'New Employee')
    return
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def bigDescription = """
* Active Application Add-ins: Lists the extensions that are registered and currently running in your Office program.
* Inactive Application Add-ins: Lists the add-ins that are present on your computer but are not currently loaded. For
 example, smart tags or XML Schemas are active only when the document that references them is open. Another example
 is the COM add-ins that are listed in the COM Add-ins dialog box. If the check box for a COM add-in is selected, the
  add-in is active. If the check box for a COM add-in is cleared, the add-in is inactive. To learn how to open the COM Add-in dialog box, see the section called [Turn off or manage the installed add-ins|https://support.office.com/en-us/article/Enable-or-disable-add-ins-in-Office-programs-3e533d0f-a6cf-44b7-b6b4-3d9185b5e025?ui=en-US&rs=en-US&ad=US&fromAR=1#bm2].
* Document Related Add-ins: Lists template files that are referenced by currently open documents.
* Disabled Application Add-ins: Lists add-ins that were automatically disabled because they are causing Office programs to crash.
"""
def summariesList = ["Subtask 1" : bigDescription,
                     "Subtask 2" : bigDescription,
                     "Notify the Risk Team of New Employee" : "",
                     "Subtask 3": ""]
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
Issue Notify_the_Risk_Team_of_New_Hire
//here we fill an array with all required watchers
List<ApplicationUser> newWatchers = new ArrayList<>()
newWatchers.add(userManager.getUserByName("susie"))
newWatchers.add(userManager.getUserByName("elif"))
newWatchers.add(userManager.getUserByName("luiz"))

summariesList.each { subTaskSummary, subTaskDescription ->
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(subTaskSummary)
    newSubTask.setParentObject(parentIssue)
    newSubTask.setPriorityId(constantManager.getPriorities().find {
        it.getName() == "Major"
    }?.id)
    newSubTask.description = subTaskDescription

    newSubTask.setProjectObject(parentIssue.getProjectObject())
    newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
        it.getName() == "Sub-task"
    }.id)
    // Add any other fields you want for the newly created sub task
    //if the summary is Notify¬_the_Risk_Team_of_New_Hire then
    if (subTaskSummary.equals("Notify the Risk Team of New Hire")) {
        def assignee = userManager.getUserByName("susie")
        newSubTask.setAssignee(assignee)
        Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
        Notify_the_Risk_Team_of_New_Hire = issueManager.createIssueObject(user, newIssueParams) as Issue
        subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    } else {
        Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
        issueManager.createIssueObject(user, newIssueParams)
        subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    }
    log.info "Issue with summary ${newSubTask.summary} created"
}
if (Notify_the_Risk_Team_of_New_Hire) {
    newWatchers.each {
        watcherManager.startWatching(it, Notify_the_Risk_Team_of_New_Hire)
    }
}

A few things of note:

  1. Since both descriptions were the same, I just put them in a variable to reuse. If you actually want different descriptions for subtasks 1 and 2, change the code accordingly.
  2. I'm using JIRA's text formatting markup in the code to make the description contain a bulleted list and a hyperlink. You can read more on the text formatting syntax at https://jira.atlassian.com/secure/WikiRendererHelpAction.jspa?section=all .
  3. Note how we're iterating through a list of key/value pairs, but Groovy can smartly treat that as two arguments to the closure we pass to .each. Neat, huh?

 

Suggest an answer

Log in or Sign up to answer