Restrict Priorities by Group

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

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

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

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

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

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

That would be appreciated.

Thanks!

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

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 vote

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?

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?

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

77 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you