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

How to add an entry for a Custom Label in a Post Function Script

Wayne Cranford July 3, 2019

Environment:
JIRA v7.4.4, 
ScriptRunner 5.3.5

Using "Clone an issue, and link" to create up to 50 product specific issues, and need to automatically populate a custom label field named "Agile Team" with the team that is responsible for that product area.  Already using Additional issue action" to customize a numbers of fields (Summary, Component, a Cascading Select, Watcher, Assignee) , based on the specific product area,  but cannot find a recipe for populating a Custom Label field. Below is an example of what is already working.

Thanks,

Wayne

import com.atlassian.jira.ComponentManagerimport com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.watchers.WatcherManager

def project30 = issue.getProjectObject()
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def userUtil = ComponentAccessor.getUserUtil()

MutableIssue issue = issue

def projectComponentManager30 = ComponentAccessor.getProjectComponentManager()
def newcomponent30= projectComponentManager30.findByComponentName(project30.getId(), "Ethernet RG")
 issue.setComponent([newcomponent30])

String SUMMARY_VALUE30 = issue.summary
String newSummary30 = "Security Vulnerability (OEM RG/AP (Inteno)) - "+SUMMARY_VALUE30

def customFieldManager3011 = ComponentAccessor.customFieldManager
def optionsManager3011 = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager
def productCat3011 = customFieldManager3011.getCustomFieldObjectsByName("Product Category")[0]
def fieldConfig = productCat3011.getRelevantConfig(issue)
def options = optionsManager3011.getOptions(fieldConfig)
def parentOption3011 = options.find { it.value == "Ethernet RG" }
def childOption3011 = parentOption3011?.childOptions?.find { it.value == "" }
def newValues = [:]
newValues.put(null, parentOption3011)
newValues.put("1", childOption3011)
issue.setCustomFieldValue(productCat3011, newValues)

issue.assigneeId = "rmenakat"
issue.summary = newSummary30
checkLink = {link -> false};

 

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
David Yu
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.
September 27, 2024

It's easier now with scriptrunner HAPI:

 

issue.set {
setLabels {
add('test')
}
}


0 votes
Peter-Dave Sheehan
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.
July 3, 2019

I believe you will have to use the LabelManager

import com.atlassian.jira.issue.label.LabelManager
def lm = ComponentAccessor.getComponent(LabelManager)
def teams = ["YourAgileTeam"]

lm.setLabels(currentUser,issue.id,agileTeamCf.id, teams.toSet(),false,false)
Wayne Cranford July 3, 2019

Peter-Dave, Thank you for the response.  I have tried this and received a "cannot find matching method" error. When I executed, I received a "issueId should not be null".  I am not a software developer by training, and have been able to rely on examples others have provided but I will continue to try using your recommendation and reference materials to hit upon a solution.  Here is what I tried:

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

def lm = ComponentAccessor.getComponent(LabelManager)
def teams = ["TeamBeacon"]

lm.setLabels(currentUser,issue.id,16791,teams.toSet(),false,false)  

 

Thanks again

Peter-Dave Sheehan
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.
July 3, 2019

I didn't attempt to include all the code you might need.

For example, you need to define and get the current user

 

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

Also, I'm assuming you already have issue define in the context binding (if you are doing this in a post function.
If not, then you need to acquire an issue object.

If you are putting this in a create transition post function, then you need to put your post function after the "Create the issue originally" and before the re-Index step.

 

The following works from my console:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
def issue = ComponentAccessor.issueManager.getIssueObject("JSP-4066")
def lm = ComponentAccessor.getComponent(LabelManager)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
lm.setLabels(currentUser, issue.id, 15900, ['test2'].toSet(),false,false)
Like # people like this
Wayne Cranford July 3, 2019

Appreciate the follow-up  and i did assume not all aspects of the solution was included. I always start from what has been provided then move towards a final solution.  This is the first time I have dealt with Labels and there are not a lot of discussions I could find online pointed me in the correct direction.

Up until now I have not used "def currentUser", but will look to roll into to the script.  

For this use-case, the issue is already created, and this is a post function in a workflow transition. 

Unfortunately my last "programming" was Fortran in 1984, using punch-cards, so I rely heavily on recipes/examples to make things work, 

 

Thanks again, and I will update this once I have a solution or specific questions.

TAGS
AUG Leaders

Atlassian Community Events