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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

1 vote
Answer accepted

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'?

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

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

 

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?

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

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 _cPrime_ this is what im trying to do

example.png

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

@Alexey Matveev _cPrime_ 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

@Charlie _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
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

81 views 0 9
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you