How to set Label for specific task using script?

Hi,

I have a script that create two substaks and I'm trying to set a label on my "subtask1".

Before I have a different script that was setting the label, even with permission "Edit Issue" denied for normal users, but now that I created theses differents scripts it is not possible to set this label for subtask1.

SCRIPT 1 THAT CREATE MY SUBTASK:

def subTask1 = SubTask.createSubTask(issue, "Execute Maintenance Window", "SPB", "O&M task", dtMWDate, dtStartDate, dtMWDate, userOm)
SubTask.setLabel(subTask1, "MW")

def subTask2 = SubTask.createSubTask(issue, "Close RFC Ticket", "SPB", "O&M task", dtMWDate+14, dtStartDate, null, userOm)

SCRIPT 2 TO SET LABEL:

static void setLabel(Issue issue, String strLabel) {
      Logger log = Logger.getInstance("tools.SubTask")
      log.setLevel(org.apache.log4j.Level.INFO)

      def user                   = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
    LabelService labelService    = ComponentAccessor.getComponent(LabelService)

    AddLabelValidationResult validationResult = labelService.validateAddLabel(user, issue.getId(), strLabel)
    if (!validationResult.errorCollection.hasAnyErrors()) {
        labelService.addLabel(user, validationResult, false)
        log.info "Label '${strLabel}' set to issue ${issue}."
    } else {
        log.error ${validationResult.errorCollection}
    }
}

LOG ERROR:

2017-05-25 16:33:32,822 http-nio-8081-exec-24 ERROR supplier.team 993x16314x1 kg958x 10.192.63.100 /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: SDP-7765, actionId: 1, file: TaskCreationDispatcher.groovy
groovy.lang.MissingMethodException: No signature of method: static SubTask.$() is applicable for argument types: (SubTask$_setLabel_closure3) values: [SubTask$_setLabel_closure3@12ee538c]
Possible solutions: is(java.lang.Object), run(), run(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;)
	at SubTask.setLabel(SubTask.groovy:235)
	at SubTask$setLabel$1.call(Unknown Source)
	at SI_-_Software_Version_Upgrade.run(SI - Software Version Upgrade.groovy:45)
	at TaskCreationDispatcher.run(TaskCreationDispatcher.groovy:29)

How can I do it?

 

1 answer

Hi Artur,

Assuming that you have a custom post function that creates two subtasks and this post function is the first one, or at least before the "Save Issue step". Then a script like the one below will create two subtasks and add the label_a and label_labels in each of your subtasks. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager

def parentIssue = issue as MutableIssue
if (parentIssue.isSubTask())
    return

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summariesList = ["summary1", "summary 2"]

def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary ->
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(subTaskSummary)
    newSubTask.setParentObject(parentIssue)
    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

    Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
    def subtask = issueManager.createIssueObject(user, newIssueParams)
    subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    log.info "Issue with summary ${newSubTask.summary} created"

    ComponentAccessor.getComponent(LabelManager).setLabels(user, subtask?.id, ["label_a", "label_b"].toSet(), false, false)
}

Please let me know if this does the trick.

regards, Thanos 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

64 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you