Need to set the security level on the create transition using scriptrunner

Mohamed El Taweel April 4, 2018

In project with one workflow "ProjectX" with many issue types ("IssueType1" , "IssueType2", "IssueType3")

userA can create issues for all types. He is assigned to two roles (Role1 and Role2 and Role3)
userB can create issues for ("IssueType3") only
 
Need to create post function with ScriptRunner on the create transition to set the security level as follow:

If Role of user = "Role1" and issueType = 'Issue Type1"
   then set the securityLevel to "Level1"
   
If Role of user = "Role2" and issueType = 'Issue Type2"
   then set the securityLevel to "Level2"
   
If Role of user = "Role3" and issueType = 'Issue Type3"
   then set the securityLevel to "Level3"

 

Thanks

1 answer

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
April 5, 2018

Hi Mohamed,

Try the code below. Also make sure you place this post function AFTER ALL OTHER post function in your 'Create' transition.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.user.ApplicationUser

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueSecurityLevelManager issueSecMgr = ComponentAccessor.getIssueSecurityLevelManager()
ProjectRoleManager roleMgr = ComponentAccessor.getComponent(ProjectRoleManager)

List<ProjectRole> roleList = new ArrayList<ProjectRole>()
ProjectRole role1 = roleMgr.getProjectRole("Role 1 name")
ProjectRole role2 = roleMgr.getProjectRole("Role 2 name")
ProjectRole role3 = roleMgr.getProjectRole("Role 3 name")
roleList.addAll(role1, role2, role3)

Map<String, String> issueSecMap = new HashMap<String, String>()
issueSecMap.put("issue type name 1", "issue security level name 1")
issueSecMap.put("issue type name 2", "issue security level name 2")
issueSecMap.put("issue type name 3", "issue security level name 3")
Long secLvlId = issueSecMgr.getIssueSecurityLevelsByName(issueSecMap.get(issue.getIssueType().getName()))[0].getId()

for (int i; i < roleList.size(); i++){
if (roleMgr.isUserInProjectRole(currentUser, roleList[i], (Project)issue.getProjectObject())){
issue.setSecurityLevelId(secLvlId)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
break
}
}

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events