Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to automate Label Manager with Groovy Script Runner?

Yves Martin
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.
June 13, 2018

 Hello

I would like to migrate JIRA standard "label" field values to codecentric Label Manager thanks to Adaptavista Script Runner.

Codecentric documents its REST API but I am interested in "internal API" so that I can loop over issues in Groovy script...

Where could I find some groovy script examples for "Label Manager"?

Thank you in advance

Yves

2 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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.
June 13, 2018

Hello @Yves Martin

Did you try to copy values as it is?

codecentric labels must extends jira labels, so it must work.

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.builder.JqlClauseBuilder
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
import com.atlassian.jira.issue.search.SearchResults
import org.slf4j.Logger
import org.slf4j.LoggerFactory;

Logger log = LoggerFactory.getLogger('update.labels')

def changeHolder = new DefaultIssueChangeHolder();
SearchService searchService =ComponentAccessor.getComponent(SearchService.class);
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def cf_new = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('New Labels')
JqlClauseBuilder builder = JqlQueryBuilder.newClauseBuilder()
Query query = builder.project('IM').and().labels().not().labelsIsEmpty().buildQuery()
SearchResults searchResult = searchService.search(user,query, PagerFilter.getUnlimitedFilter())
List<Issue> res = searchResult.getIssues()
String size = res.size()
log.debug('Issues count: {}', size)
log.debug('update started')
int i = 1
res.each {
cf_new.updateValue(null, it, new ModifiedValue(it.getCustomFieldValue(cf_new), it.getLabels()),changeHolder);
log.debug('Issue {} was updated. {} of {} issues', it.getKey(), i, size)
i++
}
log.debug('update ended')
0 votes
Yves Martin
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.
July 31, 2018

For the record, I have map each label to custom field specific value:

def labels = [  ] as Set
issue.getLabels().each {
    labels += customField.getCustomFieldType()
.getSingularObjectFromString(it.toString())
}
def changeHolder = new DefaultIssueChangeHolder()
customField.updateValue(null, issue,
new ModifiedValue(issue.getCustomFieldValue(customField), labels),
changeHolder)

But I am not satisfied with it, as no "change group" / history is visible on issue yet.

If interested, refers to https://community.atlassian.com/t5/Jira-questions/ScriptRunner-How-to-build-HistoryMetadata-for-an-issue/qaq-p/856609

Mark Markov
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.
July 31, 2018

Hello @Yves Martin
If you want store changes in change history, you should use set methods of MutableIssue and IssueManager update method that stores changes


issue.setLabels("your labels")
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Please check this article Three-ways-to-update-an-issue-in-Jira-Java-Api

You find all you need there :)

Yves Martin
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 2, 2018

Thanks for that reference. "customField.updateValue" is the only method that does not save a history changeset on issue.

Suggest an answer

Log in or Sign up to answer