Bulk add label to filtered issues

Alexey Paveliev
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.
January 31, 2013

Is there a workaround to https://jira.atlassian.com/browse/JRA-24118 ? BTW Please vote!

I need to add a label to some issues based on issue navigator search result. Bulk edit erases old labels and adds new which does not work for me.

Is there coding-free option?

2 answers

1 accepted

15 votes
Answer accepted
Henning Tietgens
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.
January 31, 2013

Get Script Runner plugin and execute following Groovy script:

import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.bc.JiraServiceContextImpl

filterId = 10000
labelName = 'Label'

cm = ComponentManager.instance
labelManager = ComponentManager.getComponentInstanceOfType(LabelManager.class)
searchRequestService = cm.searchRequestService
searchProvider = cm.searchProvider

user = cm.jiraAuthenticationContext.getLoggedInUser()
ctx = new JiraServiceContextImpl(user)
sr = searchRequestService.getFilter(ctx, filterId)
searchResult = searchProvider.search(sr?.getQuery(), user, PagerFilter.unlimitedFilter)
searchResult.issues.each{issue ->
    labelManager.addLabel(user, issue.id, labelName, false)
}
"Done."

Adapt filterId and labelName to your needs.

Henning

 

Sorin Sbarnea (Citrix)
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.
August 28, 2013

Is this going to send any email notifcations or not? I will it will not send them.

Henning Tietgens
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.
August 28, 2013

It depends on the last parameter for addLabel(). If it's false like here, no notifications are send. If it's true, notifications are send.

Sorin Sbarnea (Citrix)
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.
August 28, 2013

Thank you. Due to the nature of the problem we decided to implement a SMTP filter for changes made by a service account jira user.

Jacob Gilley December 18, 2014

Thanks for the script, Mr Tietgens!

Tej February 3, 2018

This is actually cool thank you Tietgens!

How to convert the script rather than using filter id by using JQL search in the script?

Tej February 3, 2018

I see this error!error.png

Henning Tietgens
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.
February 3, 2018

I updated the script from my old answer, there was some html code from the conversion from Atlassian Answers to Community.

Here is a solution using JQL search:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.label.LabelManager

def jqlSearch = "project = ABC"
def labelName = 'Label'

def labelManager = ComponentAccessor.getComponent(LabelManager)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def searchService = ComponentAccessor.getComponent(SearchService)

if (user) {
def parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
searchResult.issues.each{issue ->
labelManager.addLabel(user, issue.id, labelName, false)
}
} else {
return "Invalid JQL $jqlSearch"
}
} else {
Return "No logged in user found!"
}

"Done."

 It's typed without without verification, so test first on a staging environment :-)

Henning

Tej February 3, 2018

Henning thanks alot for help

when I tried old script by using filter id adding label, it throwing an import error

Import error.png

Henning Tietgens
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.
February 4, 2018

Yes, ComponentManager shouldn't be used anymore. Try the new script or try to replace ComponentManager by ComponentAccessor like in the new script.

Tej February 5, 2018

I replaced ComponentManager by ComponentAccessor, but still

Error

startup failed: Script273.groovy: 2: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 2, column 1. import com.atlassian.jira.ComponentAccessor ^ 1 error 

when I used JQL  2nd script its 

Error

startup failed: Script275.groovy: 23: unexpected token: No logged in user found! @ line 23, column 9. Return "No logged in user found!" ^ 1 error

Henning Tietgens
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.
February 6, 2018

Ok, some typing errors and a missing import in the JQL script, here the updated version:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.label.LabelManager

def jqlSearch = "project = ABC"
def labelName = 'Label'

def labelManager = ComponentAccessor.getComponent(LabelManager)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def searchService = ComponentAccessor.getComponent(SearchService)

if (user) {
def parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
searchResult.issues.each{issue ->
labelManager.addLabel(user, issue.id, labelName, false)
}
} else {
return "Invalid JQL $jqlSearch"
}
} else {
return "No logged in user found!"
}

"Done."
Henning Tietgens
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.
February 6, 2018

And here the filter id script for current Jira versions:

import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.bc.JiraServiceContextImpl

def filterId = 10000
def labelName = 'Label'

def labelManager = ComponentAccessor.getComponent(LabelManager)
def searchRequestService = ComponentAccessor.getComponent(SearchRequestService)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def ctx = new JiraServiceContextImpl(user)
def sr = searchRequestService.getFilter(ctx, filterId)
def searchResult = searchProvider.search(sr?.getQuery(), user, PagerFilter.unlimitedFilter)
searchResult.issues.each{issue ->
labelManager.addLabel(user, issue.id, labelName, false)
}
"Done."

Henning

Like Azfar Masut likes this
Tej February 6, 2018

Henning thanks a lot for your help, appreciate your work (y)

Awesome guy! 

Azfar Masut
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.
December 3, 2019

Love you @Henning Tietgens !

rithwik_poleneni February 21, 2020

@Henning Tietgens 

does this code work for python 3 ?

Henning Tietgens
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.
February 23, 2020

No, this is Groovy not Python.

2 votes
Greg Reeve October 17, 2013
@Henning Thanks for your example. I used it and modified to create a script to remove a single label.

Wanted to post it in case anyone else is looking to bulk rename labels. I'd suggest add new label then remove old.

import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.bc.JiraServiceContextImpl
 
filterId = 10602
labelName = 'Label'
 
cm = ComponentManager.instance
labelManager = ComponentManager.getComponentInstanceOfType(LabelManager.class)
searchRequestService = cm.searchRequestService
searchProvider = cm.searchProvider
 
user = cm.jiraAuthenticationContext.getLoggedInUser()
ctx = new JiraServiceContextImpl(user)
sr = searchRequestService.getFilter(ctx, filterId)
searchResult = searchProvider.search(sr?.getQuery(), user, PagerFilter.unlimitedFilter)

searchResult.issues.each{issue ->
    labelManager.setLabels(user, issue.id, (labelManager.getLabels(issue.id).collect{l -> l.getLabel()}.minus(labelName)) as Set, false, true)
}

"Label" + labelName + " removed from issues in filter " + filterId.toString()

Suggest an answer

Log in or Sign up to answer