Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

ScriptRunner Listener - Setting an additional action to add Label

craig rayner August 8, 2018

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

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Mark Markov
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.
August 8, 2018

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)

 

craig rayner August 8, 2018

@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
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.
August 8, 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)
craig rayner August 8, 2018

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

jira3.png

Mark Markov
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.
August 8, 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)
craig rayner August 8, 2018

@[deleted] 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
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.
August 8, 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)
craig rayner August 8, 2018

@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

craig rayner August 8, 2018

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

Mark Markov
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.
August 9, 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)
craig rayner August 10, 2018

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
Charly _DEISER_
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.
August 8, 2018

Hi @cr

Try adding this line at the beginning of your script:

import com.atlassian.jira.user.ApplicationUser;

 Hope this helps

craig rayner August 8, 2018

@Charly _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

0 votes
Alexey Matveev
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.
August 8, 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)
craig rayner August 8, 2018

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

craig rayner August 8, 2018

@Alexey Matveev this is what im trying to do

example.png

Alexey Matveev
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.
August 8, 2018

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

craig rayner August 8, 2018

@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

TAGS
AUG Leaders

Atlassian Community Events