Pull users from 'Team' in Jira to populate 'Approvers'
Asked by Brittney Hinesly:

Hi,

I want to populate the users from a 'Team' that I had created in the Teams feature in Jira into the approvers list when a specific request type comes through from a customer on JSM.

Looking for the best route to take on this.

We do have Scriptrunner available to use although I'm not finding a way to pull the data from the 'Teams' feature then populate the approvers with those users in the Team.

Any suggestions are much appreciated :)

Another thought was to pull data into a field from Azure AD groups so I don't have to keep up on ensuring the right people are populated in the Team. Not too sure how to go about that with what we have. We don't want any additional apps at the moment.


Answers:
author: Chris Melville
body: It's something I struggled with for a long time until a kind soul pointed me in the right direction. Here is a listener that autoassigns a portfolio "Team" based on the assignee. It works on our 9.4.4 DC instance.

You should be able to modify this easily to do what you need.   

Apologies for the formatting, It's impossible to paste code into this 'code block' macro using chrome and even then the indentation is wiped out. Maybe somebody can point me to a solution, I must not be the only one with this problem.

```
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.rm.teams.api.team.GeneralTeamService
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
def reIndexIssue(issue) {
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService

final Logger log = Logger.getLogger("com.joby.melville")
log.setLevel(Level.INFO)
final projList = ["XXX", "YYY"] // projects in scope for prefix editing
final serviceAccount = "autobot"
def MutableIssue issue = event.issue

if (issue.issueType.isSubTask() || issue.assignee == null)
return
final user = ComponentAccessor.userManager.getUserByName(serviceAccount)
final assignee = [ issue.assignee.username, issue.assignee.key ] // deep team can have either value
final Object cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('Team')
assert cf: "Custom Field 'Team' not found"
final teamMap = [ // ID : Summary Prefix (if used)
7 : "XYZ: ",
447 : "PQR: "
]
def team = teamMap.find { t ->
if (!teamService.exists(Long.valueOf(t.key))) {
log.error("Bad Team Entry: ${t.key}")
return
}
def deepTeam = teamService.getDeepTeam(Long.valueOf(t.key))
deepTeam.get().getResources().find { it.person.description.jiraUser.get() in assignee }
}
if (!team) {
log.warn("Assignee not found in any Team ${assignee}")
return
}
log.info("Found ${assignee} in Team: ${team.key}")
def changeList = []
if (issue.getCustomFieldValue(cf) as String != team.key as String) {
issue.setCustomFieldValue(cf, teamService.getTeam(Long.valueOf(team.key),false).get())
changeList.add(team.key)
}
// Add prefix if a) prefix is defined, b) issue is in scope, and c) Summary doesn't already start with another valid prefix
if (team.value && issue.projectObject.getKey() in projList && !teamMap.find { it.value?.trim() && issue.summary.startsWith(it.value) } ) {
log.info("Updating prefix to ${team.value}")
issue.summary = team.value + issue.summary
changeList.add(team.value)
}
if (changeList) {
log.info("Updated ${issue.key} with ${changeList}")
ComponentAccessor.issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
reIndexIssue(issue)
}

```


Tags: cloud

jira-cloud

jira-service-management-cloud

scriptrunner