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

How to automate Label Manager with Groovy Script Runner? Edited

Yves Martin Jun 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 Community Leader Jun 13, 2018 • edited

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 Jul 31, 2018 • edited

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 Community Leader Jul 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 Aug 02, 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

679 views 7 0
Join discussion

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