Restrict Priorities by Group

Nick Abdoo September 9, 2016

I have this almost working, but can't seem to get the line highlighted in Red below to work. It is saying that this code was deprecated in v7.0. I have tried several other options, but I'm not able to get this working. 


import com.atlassian.jira.component.ComponentAccessor
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 currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()
if (! userUtil.getGroupNamesForUser(currentUser.name).contains("Group")) {

def allowedPriorities = constantsManager.getPriorityObjects().findAll {
it.id.toInteger() > 2
}.collectEntries {
[(it.id): it.name]
}

getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
}


Here is what I was following: https://scriptrunner.adaptavist.com/4.3.0/jira/recipes/behaviours/restricting-priority-and-resolution.html

 

Appreciate any insight!

-Nick

3 answers

1 vote
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.
September 11, 2016

Hi Nick,

You should change the line in red to become the line below for JIRA 7.

def allowedPriorities = constantsManager.getPriorities().findAll() {

I hope this helps.

Thanks

Kristian

Nick Abdoo September 12, 2016

Hi Kristian,

Thanks for the help! I've changed the line in my script and the script validates fine now. However, it's still not working as expected. I've mapped the serverside script to my test project and tested it out with several groups that I am in. I've also experimented with changing the it.id.toInteger to be greater than 10, which should only show me the Blocker priority. However, when I create an issue, it's still showing me all of the priorities in the drop down.

Any idea what I'm doing wrong?

Thanks,

Nick

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.
September 13, 2016

Hi Nick,

Looking at your script it looks as if the issue is where you are checking for the group for the user.

Have you tried using the example of the Adaptavist website as I have used the example of the website with the changed line above and this works on my local JIRA 7 instance.

Thanks

Kristian

Nick Abdoo September 14, 2016

Hi Kristian,

Do I create this as an initializer or a serverside script?

If I use serverside script, which field do I put this on? I've tried priority and it does not work. I've also tried initialiser and that doesn't work either.

I'm using the "Jira-developers" group that's used in the example as well.

Thanks,

Nick

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.
September 15, 2016

Hi Nick,

This should be set as a behaviour on the priorities field.

I would say the issue is with the group not being found. Can you please check that jira-developers group is valid in your instance.

I am away from desk at the moment but will look to post an example of how I have this working in my local instance tomorrow.

Thanks

Kristian

abdoon September 15, 2016

That would be appreciated.

Thanks!

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.
September 16, 2016

Hi Nick,

 

I have attached below the example code that I have used to restrict priorities to all users in the jira-software-users group. I notice you are using the jira-developers group as per in the example but in JIRA 7 this does not exist and was replaces with the jira-software-users group.

Below is my script along with some screenshots of how it is configured and works.

Code:

import com.atlassian.jira.component.ComponentAccessor
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 currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (userUtil.getGroupNamesForUser(currentUser.name).contains("jira-software-users")) {

def allowedPriorities = constantsManager.getPriorities().findAll() {        it.id.toInteger() > 2
    }.collectEntries {
        [(it.id): it.name]
    }

    getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
}

Config:

Screen Shot 2016-09-16 at 16.37.58.png

Priorities before script was added:

 Screen Shot 2016-09-16 at 16.34.43.png

Priorities after script was added:

 Screen Shot 2016-09-16 at 16.38.33.png

I hope this helps

 

Kristian

Nick Abdoo September 16, 2016

Thank you so much, Kristian!

It works now. Not sure what was different between our scripts because it is now working for any group I put in there. Really appreciate your help.

-Nick

0 votes
Nick Abdoo September 13, 2016

Thank you, Jamie.

I've replied to Kristian below. He had a fix for the line of code, but it still does not seem to be working properly. Any idea?

0 votes
JamieA
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 12, 2016

The code is deprecated but should still work fine. Slavishly trying to use non-deprecated code is often not worth it for Atlassian apps imho. Does it not work?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events