I've run an automation rule over a month ago and have now noticed that the labels were replaced by default instead of being appended to the existing values. Is there a way to append the previous value to the issues now?
Either through ScriptRunner or Automation?
What you need is possible, but I am afraid I don't have the full code here. I can point you in the right direction.
You can get the change history for a Jira issue in DC like so:
import com.atlassian.jira.component.ComponentAccessor
Issues.search('project = WEB').each{ issue ->
def changeHistory = ComponentAccessor.changeHistoryManager.getChangeHistories(issue)
changeHistory.forEach{ changeItem ->
log.error(changeItem.authorDisplayName)
log.error(changeItem.changeItems.toString())
}
}
So, you will need to write code to go through all changeItems, check to see if the author was Automation. If it was, check to see if there is a change item for field Labels, and if so, if there is anything listed in the oldValue that was not in the newValue (I.E removed).
You would then need to gather all of these values, and append them onto the label field for that issue.
Does this help?
Kind regards,
Bobby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Bobby Bailey
This has really helped! I have made the below based on what you have shared and am currently reviewing the results (code indentations are gone for some reason here).
Change home_directory at the end of the script accordingly
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Created - 17th December 2024, 01:38 pm
//Last Modified - 17th December 2024, 04:50 pm
//Authors - Rinaldi Michael
//References -
//https://community.atlassian.com/t5/Jira-questions/Restore-labels-in-Jira-issue/qaq-p/2896803#U2897156
//https://community.atlassian.com/t5/App-Central-questions/Add-a-Label-when-creating-issue-via-ScriptRunner-mail-handler/qaq-p/1192047
//https://docs.atlassian.com/software/jira/docs/api/7.6.1/index.html?com/atlassian/jira/issue/label/LabelManager.html
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.lang.String
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.onresolve.scriptrunner.parameters.annotation.*
import com.onresolve.scriptrunner.parameters.annotation.Select
import com.onresolve.scriptrunner.parameters.annotation.meta.Option
//Declare managers
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
UserManager userManager = ComponentAccessor.getComponent(UserManager)
//*************************************//
//*************************************//
//Set variables and values
String printtext =""
//the label that replaced all other labels in the automation rule
@ShortTextInput(description="Type in the label that replaced all other labels through the Automation rule", label="Label that replaced the other labels")
String newLabel
@ShortTextInput(description = 'Type in the JQL query that will then return all the issues that this script will work on or scan', label = 'JQL Query')
String JQL
//Set the user who will perform the action
@UserPicker(label = "User", description = "Select a user")
ApplicationUser user
//Request the user for a preview or execute
@Select(
label = "Preview or Execute",
description = "Choose to preview the script's execution or actually execute the script",
placeholder = 'Preview or Execute',
options = [
@Option(label = "Preview", value = "preview"),
@Option(label = "Execute", value = "execute"),
]
)
String previewExecute
if(previewExecute=="execute")
printtext+="<b><h2><center>Execution</center></h2></b>"
else
printtext+="<b><h2><center>Preview</center></h2></b>"
int issueCount = 1
//*************************************//
//*************************************//
//Loop through all issues in a JQL
Issues.search(JQL).each
{ issue ->
printtext+="<br><b><h3>${issueCount}. ${issue.getKey()}</b></h3><br>"
def changeHistory = ComponentAccessor.changeHistoryManager.getChangeHistories(issue)
//def labelsToRestoreForThisIssue=[]
Set<String> labelsToRestoreForThisIssue = new HashSet<>();
def labelsBeforeChange
def labelsAfterChange
for(int c=0;c<changeHistory.size();c++)
{
def changeItem = changeHistory[c]
if(changeItem.changeItems.field[0]=='labels')
{
//if(changeItem.changeItems.newstring=='test_label_2')
//printtext+=changeItem.changeItems.toString()+"<br>"
labelsBeforeChange = changeItem.changeItems[0].oldstring.toString().split(' ')
labelsAfterChange = changeItem.changeItems[0].newstring.toString().split(' ')
//In this particular case, one label replaced all labels of an issue through Automation
if(labelsAfterChange.size()==1 && labelsAfterChange[0]==newLabel)
{
printtext+="<b>Instance No. ${c+1}</b><br>"
printtext+="Label/s before change -> "+labelsBeforeChange+"<br>"
printtext+="Label/s after change -> "+labelsAfterChange+"<br>"
if(labelsBeforeChange!=null || labelsBeforeChange.size()!=0)
{
for(int s=0;s<labelsBeforeChange.size();s++)
{
if(labelsBeforeChange[s]!="")
labelsToRestoreForThisIssue.add(labelsBeforeChange[s])
}
}
}
}
else
continue;
}
if(labelsToRestoreForThisIssue==null || labelsToRestoreForThisIssue.size()==0 )
{
printtext+="<br><b>No Label/s to restore for issue ${issue.getKey()}</b><br>"
}
else if(previewExecute=="execute")
{
printtext+="<br><b>Label/s restored for issue ${issue.getKey()}</b><br>"
//restore labels
for(int l=0;l<labelsToRestoreForThisIssue.size();l++)
{
labelManager.addLabel(user, issue.getId(), labelsToRestoreForThisIssue[l].toString(), false)
printtext+="${l+1}. ${labelsToRestoreForThisIssue[l].toString()}<br>"
}
}
else
{
printtext+="<br><b>Label/s that will be restored for issue ${issue.getKey()}</b><br>"
for(int l=0;l<labelsToRestoreForThisIssue.size();l++)
{
printtext+="${l+1}. ${labelsToRestoreForThisIssue[l].toString()}<br>"
}
}
printtext+="<br><b>****************************</b>"
issueCount++
}
//*************************************//
//*************************************//
@ShortTextInput(description = 'Enter any file name. This will create a new file in Script Editor with this script\'s output', label = 'FileName')
String fileName
new File("/home_directory/scripts/${fileName}.groovy").withWriter('utf-8')
{
writer -> writer.writeLine printtext.replaceAll("<br>","\n").replaceAll("<b>","").replaceAll("</b>","").replaceAll("<h2>","").replaceAll("</h2>","").replaceAll("<h3>","").replaceAll("</h3>","").replaceAll("<center>","").replaceAll("</center>","")
}
//*************************************//
//*************************************//
return "<i>Output saved into Script Editor with provided filename -> <b>${fileName}.groovy</b></i><br><br>"+printtext
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Rinaldi James Michael very impressive! Please let me know how you get on with this, I would be interested to know (and happy to hear it works!)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Bobby Bailey
It worked! The script I've shared is fairly specific to my use-case but it can be easily modified for other use cases as well. Thank you so much for sharing the ChangeHistory snippet :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Rinaldi James Michael thank you for the update! I will bookmark this to see if its something we could generalise into a snippet, if that is ok with you?
Glad we were able to solve your challenge :-)
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.
If you know which labels were removed, you can bulk-update those issues and add the right labels.
You need to add both/several at the same time though. Append is not possible. Scriptrunner can do it though.
https://confluence.atlassian.com/jirakb/edit-and-rename-labels-in-jira-cloud-1116305331.html
More on append here:
Regards
Aaron
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Aaron Pavez _ServiceRocket_
Unfortunately I only know the issues that were modified but they all had different labels.
Is there a Scriptrunner script for Jira Data Center?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry, responded in wrong location
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.