Add Service Management Ticket Request Participants and Reporter as Watchers on Linked Issues

automateissueuser

Overview

Batch update issues returned by JQL to include all linked Service Management issues customers as watchers. Use this script to update a collection of non-Service Management issues to include all linked Service Management requests reporters and request participants as watchers. The non-Service Management issues must be inward linked to the Service Management issue using the specified link type.

Example

I have a support management project and a public software development project. Many support requests have links to software project issues. I want to make sure each user watching or contributing to a service management request is added as a watcher on the linked software issue so they can follow issue progress.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 import com.atlassian.jira.bc.issue.search.SearchService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.link.IssueLinkTypeManager import com.atlassian.jira.issue.search.SearchResults import com.atlassian.jira.web.bean.PagerFilter import com.atlassian.jira.issue.Issue import com.atlassian.jira.user.ApplicationUser import groovy.transform.Field final projectKey = 'TEST' final weeks = '1' final issueLinkTypeName = 'Cause' final PAGE_SIZE = 100 // limit JQL search to only retrieve 100 issues at a time def searchService = ComponentAccessor.getComponentOfType(SearchService) def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager) def projectManager = ComponentAccessor.projectManager @Field Set issuesUpdated = [] @Field def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser def project = projectManager.getProjectByCurrentKey(projectKey) assert project: "Could not find project with key $projectKey" assert weeks: 'You must specify how many weeks back to search for issues to update' def issueLinkType = issueLinkTypeManager.issueLinkTypes.findByName(issueLinkTypeName) assert issueLinkType: "Could not find link type with name $issueLinkTypeName" def jqlSearch = "project = ${project.key} and issueFunction in hasLinkType('${issueLinkType.name}') AND created >= endOfDay(-${weeks}w) AND status != Done AND level is EMPTY" def parseResult = searchService.parseQuery(loggedInUser as ApplicationUser, jqlSearch) if (!parseResult.valid) { log.error('Invalid query') return } def query = parseResult.query def pagerFilter = PagerFilter.newPageAlignedFilter(0, PAGE_SIZE) def searchResults = null while (!searchResults || (searchResults as SearchResults).total > pagerFilter.start) { searchResults = searchService.search(loggedInUser as ApplicationUser, query, pagerFilter) searchResults.issues.each { addWatchersFromLinkedIssues(it as Issue, issueLinkType.name) } pagerFilter.start += PAGE_SIZE } ['updatedIssues': issuesUpdated] void addWatchersFromLinkedIssues(Issue issue, String linkTypeName) { def requestParticipantsFieldName = 'Request participants' def watcherManager = ComponentAccessor.watcherManager def customFieldManager = ComponentAccessor.customFieldManager def requestParticipantsCF = customFieldManager.getCustomFieldObjectsByName(requestParticipantsFieldName).first() def linkedIssues = findInwardIssuesByLinkType(issue, linkTypeName) linkedIssues.each { def participants = it.getCustomFieldValue(requestParticipantsCF) as List def potentialWatchersToAdd = [] as Set<ApplicationUser> potentialWatchersToAdd.add(it.reporter) if (participants) { potentialWatchersToAdd.addAll(participants) } def currentWatcherCount = issue.watches def resultingIssue = null potentialWatchersToAdd.each { candidateWatcher -> resultingIssue = watcherManager.startWatching(candidateWatcher, issue.refresh()) } if ((resultingIssue as Issue)?.watches > currentWatcherCount) { issuesUpdated << resultingIssue } } } List<Issue> findInwardIssuesByLinkType(Issue issue, String linkType) { def serviceDeskProjectTypeKey = 'service_desk' def issueLinkManager = ComponentAccessor.issueLinkManager def issues = issueLinkManager.getLinkCollection(issue, loggedInUser as ApplicationUser, true).getInwardIssues(linkType) issues.findAll { it.projectObject.projectTypeKey.key == serviceDeskProjectTypeKey } }