ScriptRunner Listener - Setting an additional action to add Label

Hi, i have a custom scriptrunner listener that adds a subtask when a certain condition is true. I'm trying to add a label of "Testing" to this subtask in the 'Additional issue actions' part of the listener. The code i've been trying to use is:

 

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

ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class)
labelManager.addLabel(user, issue.id, 'Testing', false)

 

I'm receiving the following error:

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script51.groovy: 4: unable to resolve class ApplicationUser 
 @ line 4, column 17.
   ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
                   ^

1 error

3 answers

1 accepted

Accepted Answer
1 vote

Hello @craig rayner

The problem here is that issue.id of subtask doesnt exist yet.

Try use setLabes method instead.

import com.atlassian.jira.issue.label.Label

issue.setLabels([new Label(null,null, "Testing")] as Set)

 

@Mark Markov thank you very much that did the trick. Do you know if there is also a way to set the parent label to 'Testing'?

Mark Markov Community Champion Aug 08, 2018

like this

import com.atlassian.jira.issue.label.Label

issue.setLabels([new Label(null,null, "Testing")] as Set)
sourceIssue.setLabels([new Label(null,null, "Testing")] as Set)

@Mark Markov im getting the following error, and when i've executed it, it doesnt change the parent label:

jira3.png

Mark Markov Community Champion Aug 08, 2018

Oh, it is not mutable object. This should work.

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.Label

issue.setLabels([new Label(null,null, "Testing")] as Set)
((MutableIssue) sourceIssue).setLabels([new Label(null,null, "Testing")] as Set)

@Mark Mark I have executed using the code above but it still doesn't set the label to Testing in the parent issue. This is post execution, it sets all the subtasks correctly to 'testing', but not the parent.

jira4.png

 

Mark Markov Community Champion Aug 08, 2018

Forgot to update source issue :)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.Label

issue.setLabels([new Label(null,null, "Testing")] as Set)
((MutableIssue) sourceIssue).setLabels([new Label(null,null, "Testing")] as Set)
ComponentAccessor.getIssueManager().updateIssue(null, sourceIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

@Mark Markov apologies for pestering you. With that code im getting an error on the last line, also tried executing and it still didnt add the label to the parent:

 

jira5.png

@Mark Markov do you have any update as i still cant get that piece of code to work?

Mark Markov Community Champion Aug 09, 2018

This should work 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.label.Label

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(sourceIssue.getKey())
issue.setLabels([new Label(null,null, "Testing")] as Set)
parentIssue.setLabels([new Label(null,null, "Testing")] as Set)
issueManager.updateIssue(null, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

thanks @Mark Markov that works. would it be possible to change that code so it adds a parent label instead of overwriting any that are there? I currently have a label on the parent set to X, and when that code is triggered, it overwrites it and sets it to 'testing'

 

Edit: I've managed to do it with this code, but im sure theres an easier way of doing it:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.label.Label

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(sourceIssue.getKey())
def parentLabel = parentIssue.getLabels()
def String firstLabel = parentLabel ? parentLabel.first() : null

issue.setLabels([new Label(null,null, "Testing")] as Set)
parentIssue.setLabels([new Label(null,null, firstLabel), new Label(null,null, "Testing")] as Set)

issueManager.updateIssue(null, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
0 votes
Alexey Matveev Community Champion Aug 08, 2018

Hello,

Change your script to this one:

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

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class)
labelManager.addLabel(user, issue.id, 'Testing', false)

I've copied the code in but now i'm getting the following error:

2018-08-08 11:52:54,491 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-08-08 11:52:54,491 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, script: com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CreateSubTask
com.atlassian.jira.util.dbc.Assertions$NullArgumentException: issueId should not be null!
 at com.atlassian.jira.util.dbc.Assertions.notNull(Assertions.java:25)
 at com.atlassian.jira.issue.label.DefaultLabelManager.addLabel(DefaultLabelManager.java:87)
 at com.atlassian.jira.issue.label.LabelManager$addLabel$0.call(Unknown Source)
 at Script58.run(Script58.groovy:6)

 

For a bit of background - My listener will create a subtask using the following code when a custom field value "Testing team required" is set to Yes. This will then create a subtask.

issue.issueType.name == 'Story' && changeItems.any {
it.field == 'Testing Team Required' && it.newstring == "Yes"

 

I've added the code you pasted to the 'Additional issue actions' but now the whole listener is failing with the above error

@Alexey Matveev this is what im trying to do

example.png

Alexey Matveev Community Champion Aug 08, 2018

It will not work. Issue does not exist yet, when additional issue action is executed. 

@Alexey Matveev it does exist. I've got a story already, where my testing team required flag is set to no. When i set it to yes it creates a subtask, and when i set it to no it deletes the subtask it created. So i need this to work for existing story's where no subtask currently exists, and when yes it clicked, it creates the subtask with the label "testing".

jira 1.png

Currently, if i remove the code i've added, and set the flag from no to yes, it will add the subtasks i want (as below)

jira2.png

0 votes

Hi @cr

Try adding this line at the beginning of your script:

import com.atlassian.jira.user.ApplicationUser;

 Hope this helps

@Carlos Aparicio [DEISER] I'm getting the following error when attempting to execute the listener:

2018-08-08 11:52:54,491 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-08-08 11:52:54,491 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, script: com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CreateSubTask
com.atlassian.jira.util.dbc.Assertions$NullArgumentException: issueId should not be null!
 at com.atlassian.jira.util.dbc.Assertions.notNull(Assertions.java:25)
 at com.atlassian.jira.issue.label.DefaultLabelManager.addLabel(DefaultLabelManager.java:87)
 at com.atlassian.jira.issue.label.LabelManager$addLabel$0.call(Unknown Source)
 at Script58.run(Script58.groovy:6)

 

example.png

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

496 views 0 9
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