It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Bulk add label to filtered issues

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

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

 

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

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.

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.

Thanks for the script, Mr Tietgens!

This is actually cool thank you Tietgens!

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

I see this error!error.png

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

Henning thanks alot for help

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

Import error.png

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

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

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."

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

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

Awesome guy! 

@Henning Tietgens 

does this code work for python 3 ?

No, this is Groovy not Python.

@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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you