Check if issue contains specific label with Groovy Script

Vladimir Pekin February 11, 2015

Hello,

I'm trying to write a post-function which will change issue description if some conditions are met during issue creating.

I'm stuck on the following stage: I need to check whether specific label is in issue's labels. In particular i want to check if issue contains label "Internal Project" despite it can contain a lot of other labels (e.g. labels for the issue are "Critical", "Internal project", "High LoE").

Could you advise me how I can check it?

Also please advice where I can download JIRA libs for work in Intellij IDEA if you know. This is much easier to work with project on PC.

My code is:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
//get IssueManager for getting of issues
IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
//get customFieldManager
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
//retrieve current issue (works for workflow post-functions)
//MutableIssue issue = issue
//Check whether the issue contains "Internal Project label"
???
//retrieve an issue which we want to use for getting additional description
MutableIssue issueForDescription = issueManager.getIssueObject("FOO-5")
//amend the issue description if issue is not subTask
if (!issue.isSubTask()) {
if (issue.description == null) {
issue.description = issueForDescription.description
} else {
issue.description = issue.description + issueForDescription.description
}
//If the issue already created and you change the issue object after that, you have to update the issue manually. Do it with following code. This is not needed in post-functions!
user = componentManager.jiraAuthenticationContext.getLoggedInUser()
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
//result parameter is used for debugging in Script console
result = "Issue description is updated."
} else {
result = "Issue is subtask. Issue isn't updated."
}
} else {
result = "Create multiply issues field doesn't exist for this issue or is not filled. Nothing was updated."
}

3 answers

1 accepted

4 votes
Answer accepted
Cesare Jacopo Corzani
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.
February 11, 2015

I would suggest you to install the Atlassian SDK ( https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project/install-the-atlassian-sdk-on-a-linux-or-mac-system )

Then checkout and import this project using IDEA:

https://bitbucket.org/jamieechlin/sr-scripts-plugin/

After doing that, IDEA should know where Maven from SDK is installed.

You can retrieve this info with the command from the shell:

 

atlas-version

 

In the output you can see this line:

 

ATLAS Maven Home: /usr/share/atlassian-plugin-sdk-5.0.3/apache-maven-3.2.1

 

Override the IDEA Maven configuration with that new location.

Screen Shot 2015-02-12 at 10.29.30.png

 

After that go to your project root (where the pom.xml is) and start the command "atlas-debug" from terminal.

When JIRA is started you can run a remote debug with IDEA to localhost port 8005

You may also want to specify a folder in which you have your scripts:

atlas-debug --jvmargs -Dplugin.script.roots=ABSOLUTE_PATH/scripts

This prevents uploading your code every time you change it.

Vladimir Pekin February 12, 2015

Thanks you very much for this detailed manual! I've made steps by "After that go to your project root (where the pom.xml is) and start the command "atlas-debug" from terminal." and got working project. When I typed atlas-debug I've got: me@MYPC /e/Repository/sr-scripts-plugin (master) $ atlas-debug sh.exe": atlas-debug: command not found This is not critical for me.

Cesare Jacopo Corzani
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.
February 12, 2015

Probably your SDK commands are not on path. sh.exe? I never did it on windows, try to take a look at that: https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project/install-the-atlassian-sdk-on-a-windows-system

Vladimir Pekin February 12, 2015

I was performing this from Git Bash, from cmd.exe it works. Thank you for your help!

2 votes
Cesare Jacopo Corzani
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.
February 12, 2015

That is equivalent to:

def containsLabel = "Client_implementation-" in issue.getLabels()*.label

I would suggest you to take a look at that:

http://groovy.codehaus.org/Collections

2 votes
Vladimir Pekin February 12, 2015

Finally I was able to read and check labels with following code:

Set issueLabels = issue.getLabels();
boolean containsLabel = false;


for (String label : issueLabels) {
    if (label.contains("Client_implementation-")){
        containsLabel = true;
    }

To get methods I was using https://docs.atlassian.com/jira/latest/com/atlassian/jira/issue/IssueManager.html

Joe Pursel March 23, 2020

The solution provided in this comment has an extra '{' when inserted into Scriptrunner. I was able to use the revised code below to check for a specific label (as a condition to a post function) when an issue is created from a parent issue and the labels are copied from the parent to the child task.

Set issueLabels = issue.getLabels();
boolean containsLabel = false;


for (String label : issueLabels) 
    if (label.contains("Client_implementation-")){
        containsLabel = true;
    }

The { after 

(String label : issueLabels) { 

is not needed.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events