Set labels on creation (from parent)

Charlie Misonne
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2016

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

2 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
April 22, 2016

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

Charlie Misonne
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2016

Thanks Thanos!

Of course your solution works. Should have seen that Captial S

sai_kiran January 28, 2020

What should be updated if I need update the label only for few stories with label "X" and the stories with other labels can be ignored.

2 votes
Aleks Yenin (Polontech)
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.
April 18, 2016
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)

Charlie Misonne
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2016

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?

Aleks Yenin (Polontech)
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.
April 18, 2016

Yes, it would be better to use IssueInputParameters

Charlie Misonne
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2016

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

0 votes
Reni October 24, 2016

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