Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,552,866
Community Members
 
Community Events
184
Community Groups

Using ScriptRunner to restrict priority

I am trying to use ScriptRunner to restrict a couple of Priority choices for a particular project, however the documentation is deprecated, and I'm on mac which means I don't get code-completion, so I need some help (a link to up to date docs or just the code I'm missing?)

This is the code I use (from the example given)

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()

def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (!userUtil.getGroupNamesForUser(currentUser.name).contains("boardgames")) {

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

getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
}

Result: Nothing.  All the priorities are showing.

1 answer

0 votes
Alejandro Suárez
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.
Feb 19, 2020

Without testing sound good to me. Where did you put this code in?

It should go to the initialiser, in other hand your code only filters the priorities with id > 2, and in my experience the priority ids its something like 5 digit number.

Priorities have a sequence and an ID number, at least in the database (and I don't know anywhere else they are shown.)
Ours sequences run from 1 to 11, and

our IDs run from 1 to 11

Yes, it is the initialiser

I amended the code to this:

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()

def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (!userUtil.getGroupNamesForUser(currentUser.name).contains("boardgames")) {

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

getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
}

and it does nothing at all.

Alejandro Suárez
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.
Feb 20, 2020

hmmm, I guess the problem could be with the group condition? can you remove it and test?

Or maybe if you get the priorities with the priority manager:

def priorityManager = ComponentAccessor.getComponent(PriorityManager)

I cant tell any more issues with your code.

Alejandro Suárez
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.
Feb 21, 2020

One more question, do you have JavaScript enabled in your web browser? Behaviours is client sided, it inyects javaScript, and if you dont have this enabled you wont see any changes. 

Yes, javascript enabled.

I changed the role to "jira-administrator"

Is there an updated version of the script somewhere?

The statements

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser() 
def allowedPriorities = constantsManager.getPriorityObjects().findAll {

have yellow warnings that they are deprecated (but the code suggested in (!) is not recognized)

And the statement

getFieldById(PRIORITY).setFieldOptions(allowedPriorities) 

has a red x in the script console (but not in the initialiser)

 

BTW our JIRA is 7.2.4 and
our ScriptRunner is 5.4.28 - issue? Should be OK right, according to the version history https://marketplace.atlassian.com/apps/6820/scriptrunner-for-jira/version-history

I am having the same issue. once the code is applied then users in our training group can still see all priorities. here is what i have in the initialiser

 

import com.atlassian.jira.issue.IssueFieldConstants
import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def priorityField = getFieldById(IssueFieldConstants.PRIORITY)
def constantsManager = ComponentAccessor.getConstantsManager()
def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (!userUtil.getGroupNamesForUser(currentUser.name).contains("Training")) {

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

getFieldById("Priority").setFieldOptions(allowedPriorities)
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events