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
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'?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mark Markov im getting the following error, and when i've executed it, it doesnt change the parent label:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@[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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @cr
Try adding this line at the beginning of your script:
import com.atlassian.jira.user.ApplicationUser;
Hope this helps
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It will not work. Issue does not exist yet, when additional issue action is executed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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".
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.