Hello everyone,
I spent a lot of time trying to figure out how to work with the Advanced Roadmaps Team field, so there is some useful information for someone who is struggling the same as I was.
Methods for Teams: Overview (Portfolio 2 / Common Public API 9.12.0 API)
REST API (where you also can see the data structure for better understanding how to use methods): Advanced Roadmap Teams REST API
How to retrieve id & name of all teams:
import
com.onresolve.scriptrunner.runner.customisers.WithPlugin
import
com.onresolve.scriptrunner.runner.customisers.PluginModule
import
com.atlassian.rm.teams.api.team.GeneralTeamService
@WithPlugin
(
"com.atlassian.teams"
)
@PluginModule
GeneralTeamService teamService
def teams = teamService.getAllShareableTeams()
if
(!teams) {
log.warn(
"No teams found in Advanced Roadmaps"
)
return
}
teams.each { teamId, team ->
def title = team.getDescription()?.getTitle() ?:
"No title found"
log.warn(
"${teamId}: ${title}"
)
}
import
com.onresolve.scriptrunner.runner.customisers.WithPlugin
import
com.onresolve.scriptrunner.runner.customisers.PluginModule
import
com.atlassian.jira.component.ComponentAccessor
import
com.atlassian.jira.issue.MutableIssue
import
com.atlassian.rm.teams.api.team.GeneralTeamService
import
com.atlassian.jira.user.ApplicationUser
import
com.atlassian.jira.issue.fields.CustomField
import
com.atlassian.jira.event.type.EventDispatchOption
@WithPlugin
(
"com.atlassian.teams"
)
@PluginModule
GeneralTeamService teamService
// List of target team IDs
def targetTeamIds = [
256
,
257
,
258
,
259
,
260
,
261
,
262
]
// Map to store users by team ID (userKey of users)
def teamUsersMap = [:]
// Get UserManager for retrieving ApplicationUser
def userManager = ComponentAccessor.getUserManager()
// Get all teams
def teams = teamService.getAllShareableTeams()
if
(!teams) {
log.warn(
"No teams found in Advanced Roadmaps"
)
return
}
teams.each { teamId, team ->
// Log to check the data type of team ID
log.warn(
"Team ID type: ${teamId.getClass().name}, Team ID value: ${teamId}"
)
// Convert teamId to the correct type for comparison
def teamIdString = teamId.toString()
// Check if teamId is in the list of targetTeamIds
if
(teamIdString in targetTeamIds.collect { it.toString() }) {
def title = team.getDescription()?.getTitle() ?:
"No title found"
log.warn(
"Team ID: ${teamId}, Team Name: ${title}"
)
// Map to store userKey of users in the current team
def usersInTeam = []
// Get team resources (persons associated with the team)
def resources = team.getResources()
resources.each { resource ->
def person = resource.getPerson()
// Get person ID (if available)
def personId = person?.getId() ?:
"No person ID"
// Get jiraUser via getDescription()
def personDescription = person?.getDescription()
def jiraUserString = personDescription?.getJiraUser()?.orNull()
// Convert jiraUserString (userKey) to ApplicationUser
def jiraUser = userManager.getUserByKey(jiraUserString)
if
(jiraUser) {
def userKey = jiraUser.getKey()
// Get userKey of the user
usersInTeam << userKey
log.warn(
"Resource ID: ${resource.getId()}, Person ID: ${personId}, Jira User Key: ${userKey}"
)
}
else
{
log.warn(
"No ApplicationUser found for ${jiraUserString}"
)
}
}
// Add the list of users to the map with the team ID as the key
teamUsersMap[teamId] = usersInTeam
}
}
// Log the users in teams map
log.warn(
"Users in teams: ${teamUsersMap}"
)
// Get the issue key
def issueKey =
"TEST-1111"
// Replace with the real issue key
// Get the issue by key
def issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueObject(issueKey)
if
(!issue) {
log.warn(
"Issue ${issueKey} not found"
)
return
}
// Get the assignee of the issue
def assignee = issue.getAssignee()
if
(!assignee) {
log.warn(
"No assignee found for issue ${issueKey}"
)
return
}
// Get the userKey of the assignee
def assigneeUserKey = assignee.getKey()
// Check if the assignee is in any of the teams
def isAssigneeInTeam =
false
def targetTeamId =
null
teamUsersMap.each { teamId, users ->
if
(assigneeUserKey in users) {
log.warn(
"Assignee ${assigneeUserKey} is in team ${teamId}"
)
isAssigneeInTeam =
true
targetTeamId = teamId
// Store the team ID
}
}
def targetTeam = teams[targetTeamId.find { it in teams.keySet() }]
// Get the team by ID from targetTeamIds
if
(isAssigneeInTeam) {
// Get the custom field Team (customfield_11111)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField teamField = customFieldManager.getCustomFieldObject(
"customfield_11111"
)
if
(teamField) {
// Update the Team field with the team ID
issue.setCustomFieldValue(teamField, targetTeam)
// Use EventDispatchOption to update the issue
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED,
false
)
log.warn(
"Team field (customfield_11111) updated with team ID: ${targetTeam}"
)
}
else
{
log.warn(
"Team custom field (customfield_11111) not found"
)
}
}
else
{
log.warn(
"Assignee ${assigneeUserKey} is not in any of the target teams"
)
}
Perfect!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.