Updating a field (ex. label) when checking all issues against a JQL query
This script will set the labels fields to a certain value for all the issues that satisfy a defined JQL query.
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
// list of elements to consider for matching
def searchlist = ["foo", "bar", "bar"]
def labelName = 'Partner'
def labelManager = ComponentAccessor.getComponent(LabelManager)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def searchService = ComponentAccessor.getComponent(SearchService)
for (searchItem in searchlist){
def jqlSearch = "company = $searchItem"
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)
}
}
}