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

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

Setting label via post-function not working reliably

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

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);

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  ?

Please proper tag people, thanks

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

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

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.

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);

Sorry, this doesn't lead to success.

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

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

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()

This issue is still there.

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

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)

Hi!

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

Cheers!

Dyelamos

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.

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# ***************"); 

 

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. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

694 views 17 19
Read article

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