Hello everyone,
I spent a lot of time trying to figure out how to work with the Team field of the Advanced Roadmap plugin, so I share here some useful info.
Methods for the Team: https://docs.atlassian.com/portfolio-for-jira-server/javadoc/9.12.0/overview-summary.html
REST API (and also a useful scheme for understanding how to use methods): https://docs.atlassian.com/portfolio-for-jira-server/REST/9.15.0/teams/#/definitions/TeamDTO
Script for finding team id and name:
importcom.onresolve.scriptrunner.runner.customisers.WithPluginimportcom.onresolve.scriptrunner.runner.customisers.PluginModuleimportcom.atlassian.rm.teams.api.team.GeneralTeamService
@WithPlugin("com.atlassian.teams")@PluginModuleGeneralTeamService teamServicedef 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}")}
importcom.onresolve.scriptrunner.runner.customisers.WithPluginimportcom.onresolve.scriptrunner.runner.customisers.PluginModuleimportcom.atlassian.jira.component.ComponentAccessorimportcom.atlassian.jira.issue.MutableIssueimportcom.atlassian.rm.teams.api.team.GeneralTeamServiceimportcom.atlassian.jira.user.ApplicationUserimportcom.atlassian.jira.issue.fields.CustomFieldimportcom.atlassian.jira.event.type.EventDispatchOption
@WithPlugin("com.atlassian.teams")@PluginModuleGeneralTeamService teamService
// List of target team IDsdef targetTeamIds = [256,257,258,259,260,261,262]
// Map to store users by team ID (userKey of users)def teamUsersMap = [:]
// Get UserManager for retrieving ApplicationUserdef userManager = ComponentAccessor.getUserManager()
// Get all teamsdef 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 IDlog.warn("Team ID type: ${teamId.getClass().name}, Team ID value: ${teamId}")
// Convert teamId to the correct type for comparisondef teamIdString = teamId.toString()
// Check if teamId is in the list of targetTeamIdsif(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 teamdef 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 ApplicationUserdef jiraUser = userManager.getUserByKey(jiraUserString)
if(jiraUser) {def userKey = jiraUser.getKey()// Get userKey of the userusersInTeam << userKeylog.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 keyteamUsersMap[teamId] = usersInTeam}}
// Log the users in teams maplog.warn("Users in teams: ${teamUsersMap}")
// Get the issue keydef issueKey ="TEST-1111"// Replace with the real issue key
// Get the issue by keydef issueManager = ComponentAccessor.getIssueManager()MutableIssue issue = issueManager.getIssueObject(issueKey)
if(!issue) {log.warn("Issue ${issueKey} not found")return}
// Get the assignee of the issuedef assignee = issue.getAssignee()
if(!assignee) {log.warn("No assignee found for issue ${issueKey}")return}
// Get the userKey of the assigneedef assigneeUserKey = assignee.getKey()
// Check if the assignee is in any of the teamsdef isAssigneeInTeam =falsedef targetTeamId =nullteamUsersMap.each { teamId, users ->if(assigneeUserKey in users) {log.warn("Assignee ${assigneeUserKey} is in team ${teamId}")isAssigneeInTeam =truetargetTeamId = 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 IDissue.setCustomFieldValue(teamField, targetTeam)// Use EventDispatchOption to update the issueissueManager.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")}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.