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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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.

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.

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
Community showcase
Published in Marketplace Apps & Integrations

Learn how to request and manage app requests in the Marketplace

G’day everyone! Super exciting news coming from the Marketplace. We have now fully rolled out the ability for end-users to submit app requests to admins directly from within the product! No longer ...

62 views 1 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you