Hi Community
I'm trying to set issue security level based on the custom 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')
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi just wants to know how to check whats the ID for security level we define?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your project, under issue security, select "Edit Issue Security" under the "Actions" menu.
Then, hover over the actions for the security level for which you want to know the ID.
In the URL, you will see something like: &levelId=10230&schemeId=10330
The level id is 10230.
If you want to get it using scriptrunner console:
import com.atlassian.jira.component.ComponentAccessor
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def project = ComponentAccessor.projectManager.getProjectByCurrentKey('SERP')
ComponentAccessor.issueSecurityLevelManager.getUsersSecurityLevels(project, currentUser).collect{"$it.id=$it.name"}.join('<br>')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Peter that helps
How can I achieve this requirement, I am using this code, but not sure where to put listener or Behavior.
And not sure if this is even correct, but I want to set security level as per component.
And if they change component it should set new security level
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
"Behaviour" code is always inherently different from most other code in Scriptrunner (listener, post function etc) because it is designed to interact with the screen DOM elements.
Whereas all other Scriptrunner script are designed to run at the server level on java objects.
So "getFieldById" is a behaviour method to get a field from the currently visible screen.
While "issue.setSecurityLevelId" is a java method to change an issue object.
You can't really mix the two (well, there are ways, but it's not recommended).
So you have to decide, do you want to modify the issue security level on the screen on behalf of the user as soon as they change the component (and perhaps lock the field from further changes). Or do you want to let the user pick whatever then react to the change in component using a listener or a workflow script?
These are two very different scenarios with different pros and cons.
One of which is that I'm not sure if Scriptrunner behaviours are capable of interacting with the issue security level drop-down in the create/edit screen. They might be, I just haven't tried.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Peter,
Look like I was able to make it work with listener if I set security level as per issue type selection, but do you know how to read component via listener.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
issue.components*.name
will give you a list of component names associated with the issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.