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.");
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
- Add code to transition
- Perform transition: label will not be added
- Perform transition again: label will be added
- Delete label
- Perform transition again: label will not be added
- Perform transition again: label will be added.
(don't work)
labelManager.setLabels(null, issue.id, markers.getIdAsLong(), labels.toSet(), false, false)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still happening with all latest versions of JIRA, Code Runner, Script Runner.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi!
What version of Scriptrunner are you using? What JIRA version?
Cheers!
Dyelamos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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# ***************");
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.