I'm trying to copy the labels from a parent issue to a subtask on creation using a scriptrunner Post Function.
I'm doing the same for the compinents and Due Date fields which is working fine.
import com.atlassian.jira.ComponentManager; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.label.LabelManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.issue.label.Label; import com.atlassian.jira.bc.project.component.ProjectComponent; import com.atlassian.jira.issue.issuetype.IssueType; import java.sql.Timestamp; ComponentManager componentManager = ComponentManager.getInstance(); IssueType issueType = issue.getIssueTypeObject(); if(issueType.isSubTask()) { // Only do this for subtasks Issue parent = issue.getParentObject(); /** * LABELS */ Set<Label> labels = parent.getLabels(); issue.SetLabels(labels); }
Above code throws this error:
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.SetLabels() is applicable for argument types: (java.util.Collections$UnmodifiableSet)
Apparently I'm getting an Unmodifiable Collection from the parent
things I tried so far:
Creating a new Set which throws the following:
No signature of method: com.atlassian.jira.issue.IssueImpl.SetLabels() is applicable for argument types: (java.util.HashSet)
Strange because HashSet is an implementation of Set right? Can't create a new Set of course.
Any ideas how to solve this? This is JIra 6.4.12
Hi Charlie,
Is issue.setLabels() and not issue.SetLabels(), therefore the script below will do the trick (tested in a JIRA v6.4.7)
if (issue.isSubTask()) { def parent = issue.getParentObject() def parentLabels = parent.getLabels() def parentDueDate = parent.dueDate //set parent labels issue.setLabels(parentLabels) //set due date issue.setDueDate(parentDueDate) //set whatever you can set in a MutableIssue ... }
The variable issue is of type MutableIssue
Regards
Thanks Thanos!
Of course your solution works. Should have seen that Captial S
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.
yes thanks, in that case I have to place my post function after Creates the issue originally in the Post Functions order because the issueId is a required argument.
Doing that I can't set the components and dueDate anymore. Meaning I have to use the IssueInputParameters?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, it would be better to use IssueInputParameters
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Couldn't get that working.
Full script:
import com.atlassian.jira.ComponentManager; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.label.LabelManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.issue.label.Label; import com.atlassian.jira.bc.project.component.ProjectComponent; import com.atlassian.jira.issue.issuetype.IssueType; import java.sql.Timestamp; import com.atlassian.jira.issue.label.LabelManager; import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.bc.issue.IssueService; import com.atlassian.jira.bc.issue.IssueService.IssueValidationResult; import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult; import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult; import com.atlassian.jira.issue.IssueInputParameters; import com.atlassian.jira.issue.IssueInputParametersImpl; ComponentManager componentManager = ComponentManager.getInstance(); LabelManager labelManager = ComponentAccessor.getComponent(LabelManager); IssueService issueService = ComponentAccessor.getComponent(IssueService); User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); IssueType issueType = issue.getIssueTypeObject(); if(issueType.isSubTask()) { // Only do this for subtasks Issue parent = issue.getParentObject(); IssueInputParameters issueInputParameters = new IssueInputParametersImpl(); /** * COMPONENT */ Collection<ProjectComponent> components = parent.getComponentObjects(); def componentIds = components.collect{it.id}; issueInputParameters.setComponentIds(*componentIds); /** * DUE DATE */ Timestamp dueDate = parent.getDueDate(); issueInputParameters.setDueDate(dueDate.toString()); UpdateValidationResult updateValidationResult = issueService.validateUpdate(user, issue.id, issueInputParameters); if(updateValidationResult.isValid()){ issueService.update(user, updateValidationResult); } else if(!updateValidationResult.isValid()) { log.error(updateValidationResult.getErrorCollection().getErrorMessages()); } /** * LABELS */ Set<Label> labels = parent.getLabels(); Set<String> labelStrings = labelManager.getLabels(parent.id).collect{it.getLabel()} labelManager.setLabels(user, issue.id, labelStrings, false, false); }
the log.error line prints empty strings. Meaning the update is not valid without any error message. Makes it difficult to debug
Got the whole thing working by setting components and duedate as first post function before Create the issue originally (directrly in MutableIssue)
And setting the labels with Label Manager after it as third Post Function
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.