Set labels on creation (from parent)

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.

  • Getting the parent as MutableIssue. => Same problem, still getting an Unmodifiable Set
  • Using the LabelManager's setLabel. => Need the issue ID in the arguments which doesn't exist yet

 

Any ideas how to solve this? This is JIra 6.4.12

3 answers

1 accepted

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

Hi,

You need to use LabelManager

setLabels(com.atlassian.crowd.embedded.api.User remoteUser,
                   Long issueId,
                   Long customFieldId,
                   Set<String> labels,
                   boolean sendNotification,
                   boolean causeChangeNotification)

 

LabelManager.setLabels(user, issue.getId(), customFieldId, false, false)

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?

Yes, it would be better to use IssueInputParameters

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&lt;ProjectComponent&gt; 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&lt;Label&gt; labels = parent.getLabels();
	Set&lt;String&gt; 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

Hi, is this not a built in function within JIRA? That the labels from the parent get inherited to the sub-task?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

713 views 6 13
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