Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Setting label via post-function not working reliably

Julian Rdnz
Contributor
January 29, 2018

Hi all,

I have a script which generates a value <Year>-<CalendarWeek> and adds it as label. This is done as post-function during a transition using "Code Runner" plugin.

Unfortunately, this doesn't work reliably. If you perform the transition on an issue the first time, it doesn't set a label. Do it the second time and the label will be set.

What's the trick here?

import java.util.Set
import java.util.Calendar;
import java.text.DecimalFormat;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.label.*
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category


LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class);

JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();

def issueManager = ComponentAccessor.getIssueManager()

DecimalFormat df = new DecimalFormat("00.##");
Calendar cal = Calendar.getInstance();
int CalendarWeek = cal.get(Calendar.WEEK_OF_YEAR);
int CalendarYear = cal.get(Calendar.YEAR);
String CalendarWeekLabel = String.valueOf(CalendarYear) + "-" + df.format(CalendarWeek);

com.atlassian.jira.user.ApplicationUser myUser = jiraAuthenticationContext.getLoggedInUser();
$log.warn( CalendarWeekLabel + "to set as label.");
labelManager.addLabel(myUser, $issue.getId(), CalendarWeekLabel, false);
$log.warn( CalendarWeekLabel + "set as label.");

4 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Max Paw November 17, 2018

This issue is still there.

Daniel Yelamos [Adaptavist]
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.
November 19, 2018

Can you give us more info? Scriptrunner version and JIRA version? 

Also include what snippet of code you are using to set the label.

I might try to reproduce this sometime today.

Thank you and apologies for the problems caused by this!

DY

Max Paw December 5, 2018

Hi.
Jira DataCenter 7.12.1, script runner 5.4.39
You can see bellow that there is a solution to use issue.setLabels()
Code check fails, but it works.

But if you try to set Labels to custom label field it works the way @Julian Rdnz  said:

  1. Add code to transition
  2. Perform transition: label will not be added
  3. Perform transition again: label will be added
  4. Delete label
  5. Perform transition again: label will not be added
  6. Perform transition again: label will be added.

(don't work)

labelManager.setLabels(null, issue.id, markers.getIdAsLong(), labels.toSet(), false, false)
1 vote
Julian Rdnz
Contributor
March 27, 2018

Still happening with all latest versions of JIRA, Code Runner, Script Runner.

  1. Add code to transition
  2. Perform transition: label will not be added
  3. Perform transition again: label will be added
  4. Delete label
  5. Perform transition again: label will not be added
  6. Perform transition again: label will be added.

Very strange.

Scriptrunner Script:

import java.util.Set
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.label.*
import com.atlassian.jira.security.JiraAuthenticationContext
import java.util.Calendar;
import java.text.DecimalFormat;

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class);

JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();

com.atlassian.jira.user.ApplicationUser myUser = jiraAuthenticationContext.getLoggedInUser();


DecimalFormat df = new DecimalFormat("00.##");
Calendar cal = Calendar.getInstance();
int CalendarWeek = cal.get(Calendar.WEEK_OF_YEAR);
int CalendarYear = cal.get(Calendar.YEAR);
String CalendarWeekLabel = String.valueOf(CalendarYear) + "-" + df.format(CalendarWeek);


labelManager.addLabel(myUser, issue.getId(), CalendarWeekLabel, false);
Thanos Batagiannis [Adaptavist]
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.
March 27, 2018

Hi Julian Radünz | MSP AG

What is the order of that post function ? Do you get any errors in your application logs when you perform the transition and the label is not added ?

A last thing is that post function during the Create action  ?

Julian March 27, 2018

Please proper tag people, thanks

Thanos Batagiannis [Adaptavist]
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.
March 27, 2018

Sorry Julian, just realised that this may be a bug with the community and their mention system.

I had to double check before mention someone, too much trust to the community's system failed me. 

Apologies

Thanos

Julian Rdnz
Contributor
March 27, 2018

Hi Thanos,

the post-function is the last one. I already moved it to other positions; no change.

There are no errors in application logs. For testing purposes, I added log.warn in the postfunction. The log.warn results will be visible in the logs every time. This means to me that the post-function will be performed correctly at every time, but - I don't know why - its result will have no effect on the labels field.

The post-function is currently not in the "create" action, but once it works, it should be placed there.

Best regards & thanks for your help,

Julian

Julian Rdnz
Contributor
April 16, 2018

New discoveries:

After performing the post function, the label in fact will always be set. When I perform the post-function and don't see the label afterwards, I do a full system reindex. After this is finished, the label is visible.

It's not possible to find a workaround by triggering the reindex for this issue in another post function or do a project reindex.

Neta Elyakim
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.
April 16, 2018

Re-index issue:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils

def issue = ComponentAccessor.getIssueManager().getIssueObject("issuekey")
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def issueManager = ComponentAccessor.getIssueManager()

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
//log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);
Julian Rdnz
Contributor
April 17, 2018

Sorry, this doesn't lead to success.

0 votes
Thanos Batagiannis [Adaptavist]
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.
November 19, 2018

Hi @Max Paw & @Julian Rdnz

Even tho I am not able to reproduce the problem, I believe that indeed there may be a threading issue with using the LabelManager in a post function that causes that problem. 

So let's try a different way then. Can you please use the following script in order to set new labels

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager

def issue = issue as MutableIssue

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def newLabel = ComponentAccessor.getComponent(LabelManager).addLabel(user, issue.id, "a_label", false)
issue.setLabels([newLabel].toSet())

Please let me know how this goes. 

Regards, Thanos

Julian Rdnz
Contributor
November 21, 2018

Hi Thanos,

this script seems to work, but:

1) it removes labels which are already set. Expected is that it just adds labels.

2) I rebuilt it to set the calendar week:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.JiraAuthenticationContext

import java.util.Set
import java.util.Calendar;
import java.text.DecimalFormat;

def issue = issue as MutableIssue


DecimalFormat df = new DecimalFormat("00.##");
Calendar cal = Calendar.getInstance();
int CalendarWeek = cal.get(Calendar.WEEK_OF_YEAR);
int CalendarYear = cal.get(Calendar.YEAR);
String CalendarWeekLabel = String.valueOf(CalendarYear) + "-" + df.format(CalendarWeek);

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def newLabel = ComponentAccessor.getComponent(LabelManager).addLabel(user, issue.id, CalendarWeekLabel, false)
issue.setLabels([newLabel].toSet())

Now, start with an issue w/o any labels and perform a transition which has this script added to a post-function.

It will set the label (OK), but if you delete it and set a random different label, next time the transition runs nothing happens.

It seems the LabelManager is not that stable.

By the way, I neither can reproduce the issue on a fresh Jira with a simple workflow. The problem may be related to a timing problem. Much more things do happen in a production Jira during a transition.

Best regards & thanks for your help,

Julian

Max Paw November 24, 2018

By the way, @Thanos Batagiannis [Adaptavist] issue.setLabels() does work, but if you use it it shows code check error.
How to reproduce - well it works if you use script console, but failes if it's within post-function. Latest jira, Data Center here 

@Julian Rdnz  just do not use LabelManager to set Labels in your original script, use issue.setLabels()

Erik Ekengren
Contributor
February 21, 2022

Old thread but it might help someone with the same problems as I had. I was trying to add 1 label (and not remove the already set labels on the issue) when I run into this issue. Use this code and it should work fine:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager
 
def issueManager = ComponentAccessor.issueManager
def labelManager = ComponentAccessor.getComponent(LabelManager)
 
  
def issue = issue as MutableIssue
 
def existingLabels = issue.labels
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def newLabel = ComponentAccessor.getComponent(LabelManager).addLabel(user, issue.id, "MyNewLabel", false) //change MyNewLabel to the label you want to use
issue.setLabels([newLabel + existingLabels].toSet())
0 votes
Daniel Yelamos [Adaptavist]
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 30, 2018

Hi!

What version of Scriptrunner are you using? What JIRA version?

Cheers!

Dyelamos

Julian Rdnz
Contributor
January 30, 2018

Hi,

actually I'm not using Scriptrunner, but the "Code Runner" plugin. But I checked it using Scriptrunner 5.2.2 / JIRA 7.5.1 / no other active third-party-plugins enabled ( some little syntax changes int he script are required ) and have the same problem.

Neta Elyakim
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 1, 2018

Hey , @Daniel Yelamos [Adaptavist]

I have the same issue with setlabel()
ScriptRunner version is 5.3.1

/**
* Created by Neta Elyakim on 24/01/2018.
*/
// log.warn("\n************ setLabelOnSupportToSolutionManagementProject.groovy Script START ************");

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)

def linkMgr = ComponentAccessor.getIssueLinkManager();
List<IssueLink> links = linkMgr.getOutwardLinks(issue.id);
String summary = issue.getSummary();

if(issue.getLabels().toString().contains("already_created")){
// log.warn("\n************ Labels contains 'already_created' (" + issue.getLabels().toString() + ") ************");
return null;
}
else{
if(links.size()){
// log.warn("\n************ Issue Links (" + links + ") ************");
for (int i=0;i<links.size();i++){
def link = links[i];
def linkName = link.issueLinkType.name;
if ("Relates" == linkName){
// log.warn("\n************ Issue Links =(" + linkName + ") check vale -> 'Relates'************");
def targetTicket = link.destinationObject;
def targetsummary = targetTicket.getSummary();
if(targetsummary.startsWith("SM Log:") && targetsummary.substring(8)==summary){
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
// log.warn("******* labels - "+labels+" *******");
labels += 'already_created'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)
issue.store();
//log.warn("******* add 'already_created' label - #PASS# *******");

}
}
}
}
else{
// log.warn("\n************ Issue Links is empty(" + links + ") ************");
return null;
}
}
//log.warn("*************** setLabelOnSupportToSolutionManagementProject.groovy Script #END# ***************"); 

 

Julian Rdnz
Contributor
March 27, 2018

Still doesn't work with latest JIRA and Scriptrunner versions.

 

import java.util.Set
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.label.*
import com.atlassian.jira.security.JiraAuthenticationContext
import java.util.Calendar;
import java.text.DecimalFormat;

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class);

JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();

com.atlassian.jira.user.ApplicationUser myUser = jiraAuthenticationContext.getLoggedInUser();


DecimalFormat df = new DecimalFormat("00.##");
Calendar cal = Calendar.getInstance();
int CalendarWeek = cal.get(Calendar.WEEK_OF_YEAR);
int CalendarYear = cal.get(Calendar.YEAR);
String CalendarWeekLabel = String.valueOf(CalendarYear) + "-" + df.format(CalendarWeek);


labelManager.addLabel(myUser, issue.getId(), CalendarWeekLabel, false);

The script will only work every second time it will be called. 

TAGS
AUG Leaders

Atlassian Community Events