Check if issue contains specific label with Groovy Script

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

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-pro... )

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.

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.

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-pro...

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

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

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

Suggest an answer

Log in or Register to answer

Stay in touch

Be the first to know what's trending on Atlassian Community