Cannot copy labels from issue to linked issues through script

Mihai Mihai October 16, 2019

Hello,

 

I am trying to copy the labels on an existing issue to its linked issues (through a certain link).

I am pretty close, but I get an error I'm not sure how to overcome:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.label.LabelManager
import org.apache.log4j.Logger
import org.apache.log4j.Level

def issue = event.issue
def labelManager = ComponentAccessor.getComponent(LabelManager)
def existingLabels = labelManager.getLabels(issue.id).collect{it.getLabel()}
def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())
def subElements = links.findAll { it.issueLinkType.name.contains('Parent/Child') && it.destinationObject.getProjectObject().getKey() == 'TEST'}


subElements.each { it ->

def sublabelManager = ComponentAccessor.getComponent(LabelManager)
def subexistingLabels = sublabelManager.getLabels(it.getDestinationObject().id)*.label
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

for (eachlabel in existingLabels) {

if (eachlabel && !subexistingLabels*.contains(eachlabel)) {

labelManager.setLabels(loggedInUser, it.getDestinationObject().key, eachlabel, false, false)
}
}
}

 

The error is:

 

2019-10-17 07:15:24,744 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-10-17 07:15:24,744 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.label.DefaultLabelManager.setLabels() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser, java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Boolean) values: [uidq5816(uidq5816), TEST-1001, a2b, false, false]
Possible solutions: setLabels(com.atlassian.jira.user.ApplicationUser, java.lang.Long, java.util.Set, boolean, boolean), setLabels(com.atlassian.jira.user.ApplicationUser, java.lang.Long, java.lang.Long, java.util.Set, boolean, boolean)
	at Script686$_run_closure3.doCall(Script686.groovy:24)
	at Script686.run(Script686.groovy:14)


Any help is appreciated.

Thank you!

 

2 answers

1 accepted

1 vote
Answer accepted
Mihai Mihai October 18, 2019

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.label.LabelManager
import org.springframework.util.StringUtils
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.listener")
log.setLevel(Level.DEBUG)

def issue = event.issue
def labelManager = ComponentAccessor.getComponent(LabelManager)
def existingLabels = labelManager.getLabels(issue.id).collect{it.getLabel()} as Set
def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())
def subElements = links.findAll { it.issueLinkType.name.contains('Parent/Child') && it.destinationObject.getProjectObject().getKey() == 'TEST'}


subElements.each { it ->

def sublabelManager = ComponentAccessor.getComponent(LabelManager)
def subexistingLabels = sublabelManager.getLabels(it.getDestinationObject().id).collect{it.getLabel()} as Set
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

log.debug("labels on current issue: " + existingLabels)
log.debug("subexistingLabels: " + subexistingLabels)

for (eachlabel in existingLabels) {
if (!subexistingLabels.contains(eachlabel)) {
subexistingLabels.add(eachlabel)

labelManager.setLabels(loggedInUser, it.getDestinationObject().id as Long, subexistingLabels, false, false)
}
}

}

0 votes
Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 16, 2019
Mihai Mihai October 16, 2019

Hi,

Not sure how that can help, it seems I'm just having issues with my last line:

 

labelManager.setLabels(loggedInUser, it.getDestinationObject().key, eachlabel, false, false)

 

while in there there is a totally different approach.

 

Thank you.

Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 16, 2019

oh okay anyways if this thing resolves just give out the solution if this is new then it might help others 

thanks.

Suggest an answer

Log in or Sign up to answer