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

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

Setting issue security level based on custom field value using a scriptrunner listener

Edited

Hi Community

I'm trying to set issue security level based on the cUntitled.pngustom field when issue created and when custom field is updated. Here is my code and am not sure where to go from here. Any help is appreciated.

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel

def issueManager = ComponentAccessor.getIssueManager()
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'User County'}

if (issue.getCustomFieldValue(cf).toString().equals('County A'))
{
issue.setSecurityLevelId('10601')
}
else if (issue.getCustomFieldValue(cf).toString().equals('COunty B'))
{
issue.setSecurityLevelId('10602')
}
else// if county is empty then
{
issue.setSecurityLevelId('10603')
}

2 answers

2 accepted

2 votes
Answer accepted

Hi

For starters, you can remove the static type checking errors (which are not always show- stopper, but in the case they are) by using com.atlassian.jira.issue.MutableIssue instead of com.atlassian.jira.issue.Issue and removing the single quotes around your security level Id.

But then, this only modifies an instance of an issue object in memory.

You need to persist that object into the database and into the index.

You can do that with

//additional import
import com.atlassian.jira.issue.index.IssueIndexingService
//additional service
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)

def sendEmailWithChange = false
def user= ComponentAccessor.jiraAuthenticationContext.loggedInUser //or use userManager to get a user that has permission to edit the issue

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,sendEmailWithChange )
indexingService.reIndex(issue)

Here taking your original script and adding my suggestions:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'User County'}

if (issue.getCustomFieldValue(cf).toString().equals('County A'))
{
issue.setSecurityLevelId(10601)
}
else if (issue.getCustomFieldValue(cf).toString().equals('COunty B'))
{
issue.setSecurityLevelId(10602)
}
else// if county is empty then
{
issue.setSecurityLevelId(10603)
}

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
indexingService.reIndex(issue)

This should clear all the static type checking issues

Thank you for taking the time for clearing the errors. 

1 vote
Answer accepted
Ravi Sagar Community Leader Jan 27, 2021

Hi @Sysad 

You are almost there. Try this code to update the security level.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def issue = ComponentAccessor.getIssueManager().getIssueObject("SD-1261")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
issue.setSecurityLevelId(10001)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()

issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,true)

I hope this code will give an idea of what you can add in your script.

Make sure security scheme is applied to the project.

Ravi

Hi @Ravi Sagar

 

This what I produced and looks like it is working! while creating issues and editing USER COUNTY filed on the created issue. Here is the code.

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService

def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'User County'}

if (issue.getCustomFieldValue(cf).toString().equals('ABCD'))
{
issue.setSecurityLevelId(10600)
}
else if (issue.getCustomFieldValue(cf).toString().equals('EFGH'))
{
issue.setSecurityLevelId(10602)
}
else if (issue.getCustomFieldValue(cf).toString().equals('IJKL'))
{
issue.setSecurityLevelId(10603)
}
else// if county is empty then
{
issue.setSecurityLevelId(10601)
}

def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def sendEmailWithChange = false
def user= ComponentAccessor.jiraAuthenticationContext.loggedInUser //or use userManager to get a user that has permission to edit the issue
def issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,sendEmailWithChange )
indexingService.reIndex(issue)

 

But I do see few errors and for static type as suggested by @Peter-Dave Sheehan  to use MutableIssue, I get unable to resolve class Issuse at def issue =event.issue as Issue

 

Untitled.png

Untitled.png

 

Ravi Sagar Community Leader Jan 27, 2021

Hi @Sysad 

You can ignore these static type checking errors because when you run this code as a post function or as a listener because then you will have issue, a binding available during the execution.

For testing you can always test your code in the console with an example issue like this.

def issue = ComponentAccessor.getIssueManager().getIssueObject("ISD-2")

.. and when pasting the script in the listener or post function comment this line.

Ravi

Thank you for the insight

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Trello

Taco Tuesday: Your favorite Trello blog takeaway

Hello friends! From the community that brought you Welcome Wednesday, Throwback Thursday and Friday Fun, welcome to Taco Tuesday, a weekly discussion about all things Trello. The best part? One Tac...

67 views 4 3
Join discussion

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