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 approvers based on multiple groups - scriptrunner

Edited

I'm trying to set up an approval process, where the users only have to select the specific systems they need access to and script runner automatically fills approvers based on this selection.

Now I figured the following solution:

  • Create a multi select list custom field
    • Fill this field with active directory group names
  • Create a listener for issue create and issue update events
  • Get the selected group names
  • Get the users from these groups and add it to a list
  • Set these list of names in the approver field

I had partial succes in creating this script, and also created a mess:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.Group

def groupManager = ComponentAccessor.getGroupManager()
def userManager = ComponentAccessor.getUserManager()
def issue = event.issue
def componentManager = ComponentAccessor.issueManager

//CustomField userSelectedGroups = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Selected systems")
//def selectedGroups = issue.getCustomFieldValue(userSelectedGroups) as List

def selectedGroups = ["group1", "group2"]
if(selectedGroups == null)
return;

def users = "" as List
def i = 0;
while ( i < selectedGroups.size()) {
def members = groupManager.getUsersInGroup(selectedGroups[i])
def j = 0;
while ( j < members.size()) {
users.add(members[j])
j++
}
i++
}


CustomField approvers = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Approvers")

approvers.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approvers), users), new DefaultIssueChangeHolder())

So this works, when I add a list of groups here in the script, but when I try to get the selected values from the multi picker field it does not work. I think the problem is getting the multi select list values as a list...

Any help would be appreciated!

Thanks!

1 answer

Eventually I found the solution:

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def userSelectedGroups = customFieldManager.getCustomFieldObjectsByName("Permission to")
def selectedGroups = issue.getCustomFieldValue(userSelectedGroups[0])

def userList = []
selectedGroups.each {
def users = groupManager.getUsersInGroup("$it")
userList.addAll(users)
}

CustomField approvers = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Approvers")
approvers.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approvers), userList), new DefaultIssueChangeHolder())

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

463 views 3 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