Restrict list of issue types

Gorzech November 3, 2015

Hi

I created following script but it does not seem to work. No errors in Log though:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor


def componentManager = ComponentManager.getInstance()

FormField issuetype = getFieldById("issuetype");
def it=issuetype.getValue();


def constantsManager = ComponentAccessor.getConstantsManager()
Map allowedIssueTypes =constantsManager.getAllIssueTypeObjects().findAll {
    it.name in ["Bug","Epic"]
}.collectEntries {
    [(it.id): it.name]
}
//println(allowedIssueTypes.toString())
// issuetype.setHelpText(allowedIssueTypes.toString());

issuetype.setFieldOptions(allowedIssueTypes)

The content of allowedIssueTypes is: [1:Bug, 10000:Epic]

 

I am running JIRA v6.4.6.

I don't care about the condition.

What I want to do is I want to limit the number of issue Types to Bug and Epic for all users in certain group (the condition is not relevant) I am using behavior attached to Issue Type field

What am I doing wrong? Thanks!

 

1. Script runner 3.1.4
2. JIRA v6.4.6
3. Within Behaviour pugin mapped to all issue types in the project and Associated to Issue Type.
4. There is no error going to the log. But when I add the line: issuetype.setFieldOptions(allowedIssueTypes)
I am not seeing any effect. Even the println(allowedIssueTypes.toString()) is not working :(

2 answers

3 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2015

Hi Gorzech,

I have a piece of code which has been is used in a JIRA 6.3.5 instance to allow only the Issue type of Bug or Story to be selected when an issue is any other state of the workflow other than create.

 

This is done using the Behaviours plugin by applying the following code below as a behaviour to to the Issue Type field. I add a condition which states Except - Workflow Action = Create on the behaviour for that field.

You could of course change the condition to make this code run at a different time or remove the conditions so that it always runs.

 

import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

if (getActionName() != "Create") {
    def issuevalue = getFieldById("issuetype")
    log.debug("The Current Issue Type ID is " +issuevalue.getValue())
    def constantsManager = ComponentAccessor.getConstantsManager()
    def allowedIssueTypes = constantsManager.getAllIssueTypeObjects().findAll {
        it.name in ["Bug","Story"]
    }.collectEntries {
        [(it.id): it.name]
    }
    getFieldById(ISSUE_TYPE).setFieldOptions(allowedIssueTypes)
}

 

I hope this helps.

Kristian

Gorzech November 4, 2015

Hi Kristian Thanks for your reply. Unfortunately your code is nearly the same as mine and does not work for me (even with condition commented out) :( Did you test this one? What is the content of allowedIssueTypes variable? Thanks Gorzech

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2015

Hi Gorzech, As mentioned this script is used in Behaviours inside Jira 6.3.5 and works. Can you please post the following details so we can try to debg further: 1.) What version of Script Runner you are using. 2.) What version of Jira you are using. 3.) Where and how you have configured this code inside your Jira instance. 4.) An extract of the error that is outputed to the atlassian-jira.log. Thanks Kristian

Gorzech November 5, 2015

Hi Kristian Thanks for your reply. Sorry form late reply but as a new user it seems I can only commit one post a day :( To answer your quesions: 1. Script runner 3.1.4 2. JIRA v6.4.6 3. Within Behaviour pugin mapped to all issue types in the project and Associated to Issue Type. 4. There is no error going to the log. But when I add the line: issuetype.setFieldOptions(allowedIssueTypes) I am not seeing any effect. Even the println(allowedIssueTypes.toString()) is not working :( Cheers! Gorzech

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 6, 2015

Hi Gorzech, You need to use log4j statements to write out to the log. Can you please confirm if the enable logging link has been clicked on the Behaviours home screen. Kristian

Gorzech November 8, 2015

Hi Kristian the log works right, but the issue type options are not changed. There is one strange thing in the log as it looks like the code is run twice? Is this normal? I amended your code slightly (removed condition and added another log at the end): import com.atlassian.jira.component.ComponentAccessor import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE //if (getActionName() != "Create") { def issuevalue = getFieldById("issuetype") log.debug("The Current Issue Type ID is " +issuevalue.getValue()) def constantsManager = ComponentAccessor.getConstantsManager() def allowedIssueTypes = constantsManager.getAllIssueTypeObjects().findAll { it.name in ["Bug","Epic"] }.collectEntries { [(it.id): it.name] } getFieldById(ISSUE_TYPE).setFieldOptions(allowedIssueTypes) log.debug("allowedIssueTypes = " +allowedIssueTypes) //} The log output is: 2015-11-09 09:33:11,751 http-bio-8080-exec-8 DEBUG gorz 573x549548x3 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] The Current Issue Type ID is 1 2015-11-09 09:33:11,751 http-bio-8080-exec-8 DEBUG gorz 573x549548x3 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] allowedIssueTypes = [1:Bug, 10000:Epic] 2015-11-09 09:33:20,024 http-bio-8080-exec-25 DEBUG gorz 573x549556x2 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] The Current Issue Type ID is 10000 2015-11-09 09:33:20,025 http-bio-8080-exec-25 DEBUG gorz 573x549556x2 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] allowedIssueTypes = [1:Bug, 10000:Epic] 2015-11-09 09:33:20,518 http-bio-8080-exec-20 DEBUG gorz 573x549561x2 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] The Current Issue Type ID is 10000 2015-11-09 09:33:20,519 http-bio-8080-exec-20 DEBUG gorz 573x549561x2 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] allowedIssueTypes = [1:Bug, 10000:Epic] 2015-11-09 09:34:32,574 http-bio-8080-exec-8 DEBUG gorz 574x549783x2 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] The Current Issue Type ID is 10501 2015-11-09 09:34:32,575 http-bio-8080-exec-8 DEBUG gorz 574x549783x2 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] allowedIssueTypes = [1:Bug, 10000:Epic] 2015-11-09 09:34:36,127 http-bio-8080-exec-8 DEBUG gorz 574x549792x4 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] The Current Issue Type ID is 10501 2015-11-09 09:34:36,127 http-bio-8080-exec-8 DEBUG gorz 574x549792x4 b2oww 10.172.182.23,172.17.38.19 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [jira.groovy.user.FieldBehaviours] allowedIssueTypes = [1:Bug, 10000:Epic] As I said values are right in log but nothing happens in Create window :( The choice is not limitted Thanks for your support Gorzech

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2015

Hi Gorzech, I have tried my code above on Jira 6.4.6 using Script Runner version 3.1.4. I can confirm that it is setting the values of the options map correctly by viewing these in the browser console but these are not getting rendered to the screen for the issue type field. This looks to me like a bug in Jira 6.4 that you should raise with Atlassian. As previously mentioned I use the condition to ensure the code runs only when the worklow action is not create i.e when editing an issue. If you wanted the code to execute at any other time then you would need to edit the condition. Many Thanks Kristian

Bartek Zukowski July 11, 2016

Hi @Gorzech,

any success in solving this problem? I noticed that there is a bug raised in Adaptavist JIRA (SRJIRA-1010) related to this problem but maybe you've found some workaround...

Regards,

Bartek

KyleGershman November 30, 2016

@Kristian Walker (Adaptavist)

I'm assuming that your use case is only after the issue has been initially created and you are limited the ability to change the issue type from the original issue type to Bug or Story.

What I believe the OP is after, as well as I, is to set the valid Issue Types in the Drop-Down on the "Create" issue gadget that appears.

Can you confirm your use case as well as if there are any options to use the initializers on Behavior's to limit the list of issue types on the drop down of the Create issue gadget available from the system toolbar?

0 votes
Jeremy Gaudet
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.
November 4, 2015

What context does this script run in?

Suggest an answer

Log in or Sign up to answer