How to Fetch Label values through groovy

Venkata Sagar July 13, 2022

Hello,

I need to fetch values of label(System field) through  groovy script. Can you please help me.

Thanks in Advance 

1 answer

0 votes
Radek Dostál
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 13, 2022

And why is groovy script the needed way? What values are you trying to fetch? All of them? Some?

Labels is a fully implemented field shown both in issue results and searches.

Venkata Sagar July 13, 2022

Hello @Radek Dostál 

Thanks for the Reply!!

We are implementing the customization like based on the priority  field auto populating the due date field . And we need  to find the label value through groovy script. because based on the label value the customization  should works .

For suppose if  label value is A the customization should work. We need to fetch only one value from labels.

For field customization we have script and we kept in the post function and it is working as expected. but we stucked for finding the label values through groovy script. 

Thanks 

Sagar 

Radek Dostál
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 13, 2022

Post-functions have 'issue' as binding variable (assuming that you are using scriptrunner), meaning you can use https://docs.atlassian.com/software/jira/docs/api/8.20.8/com/atlassian/jira/issue/Issue.html#getLabels--

import com.atlassian.jira.issue.label.Label

Set<Label> labels = issue.getLabels()

if (labels.find {
it.getLabel() == "MyLabel"
}) {
//if labels contains MyLabel, do something here
}

 

So this should be a relatively simple check. Just to remember that Label itself is a class and to compare it's "human readable" value you need to use Label#getLabel() as in the example, which is the string representation of the label as you see in webUI.

Venkata Sagar July 17, 2022

Hi @Radek Dostál  , Thanks you , The code is working fine!! We have Tested in Post Function for Existing ticket it Worked , But The Problem we are facing is We are unable to create issues when we select label PDE_Support 

Script :

import static com.atlassian.jira.issue.IssueFieldConstants.LABELS
import com.atlassian.jira.issue.label.Label;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import java.sql.Timestamp;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import groovy.time.*
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.config.PriorityManager
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import com.atlassian.sal.api.user.UserManager
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY


def constantsManager = ComponentAccessor.getConstantsManager()

def userUtil = ComponentAccessor.getUserUtil()
log.debug ComponentAccessor.getComponent(UserManager)

def priority = issue.getPriority()
def pname=priority.name.toString()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

Issue issue = issue

def duedate = customFieldManager.getCustomFieldObject("customfield_47440")


Calendar cal = Calendar.getInstance();
def temp = cal.getTimeInMillis();
def add = 0;
def x = 1000*24*60*60L
def mydueDate = new Timestamp(cal.getTimeInMillis());

Set<Label> labels = issue.getLabels()

if (labels.find {issue.getLabel() == "PDE_Support"}) {

if (pname == "0")
{
add = 1*x
temp = temp+add
mydueDate.setTime(temp)

issue.setCustomFieldValue(duedate, mydueDate)

def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

}
else if (pname == "1")
{
add = 14*x
temp = temp+add
mydueDate.setTime(temp)

issue.setCustomFieldValue(duedate, mydueDate)

def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

}
else if (pname == "2")
{
add = 28*x
temp = temp+add
mydueDate.setTime(temp)

issue.setCustomFieldValue(TargetCompletionDate, mydueDate)

def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

}else if(pname == "3")
{
add = 90*x
temp = temp+add
mydueDate.setTime(temp)

issue.setCustomFieldValue(duedate, mydueDate)

def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
else if(pname == "4")
{
add = 180*x
temp = temp+add
mydueDate.setTime(temp)

issue.setCustomFieldValue(duedate, mydueDate)

def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

}

Suggest an answer

Log in or Sign up to answer