-
Notifications
You must be signed in to change notification settings - Fork 10
/
sum-components-versions-in-epics-for-project.groovy
117 lines (96 loc) · 4.39 KB
/
sum-components-versions-in-epics-for-project.groovy
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.query.Query
import com.atlassian.jira.web.bean.PagerFilter;
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.dvhb.epicscomponentsversionssum")
log.setLevel(Level.DEBUG)
// Utility functions
def epicForIssue = { Issue issue ->
def epicLinkID = 'customfield_10007' //Component/s field id
CustomField epicLinkField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(epicLinkID)
MutableIssue epic = issue.getCustomFieldValue(epicLinkField) as MutableIssue
return epic
}
def issuesForEpic = { Issue epic ->
ComponentAccessor.getIssueLinkManager().getOutwardLinks(epic.id)
.findAll { it.issueLinkType.name == "Epic-Story Link" } // filter
.collect { it.getDestinationObject() } // map
}
def componentsForIssues = { Collection<Issue> issues ->
issues
.collect { it.components }
.flatten()
.unique(false) as Collection<ProjectComponent>
}
def versionsForIssues = { Collection<Issue> issues ->
issues
.collect { it.fixVersions }
.flatten()
.unique(false) as Collection<Version>
}
def updateEpicAndReindex = { MutableIssue epic, Collection<ProjectComponent> components, Collection<Version> versions ->
UserManager userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
epic.setComponent(components)
epic.setFixVersions(versions)
issueManager.updateIssue(userManager.getUserByName("username"), epic, EventDispatchOption.ISSUE_UPDATED, false) //JIRA username that should run this script
issueIndexingService.reIndex(epic)
}
def setComponentsAndVersionsOfIssuesToEpic = { MutableIssue epic ->
if (epic == null) {
log.debug("Epic not found. Terminating.")
return
}
log.debug("Epic " + epic.getKey() + " found. Analyzing.")
Collection<Issue> issues = issuesForEpic(epic)
Collection<ProjectComponent> components = componentsForIssues(issues)
Collection<Version> versions = versionsForIssues(issues)
Boolean needsUpdate = (epic.components.sort() != components.sort()) || (epic.fixVersions.sort() != versions.sort())
if (needsUpdate) {
log.debug("Updating Epic " + epic.getKey())
updateEpicAndReindex(epic, components, versions)
}
else {
log.debug("Epic " + epic.getKey() + ". No update necessary")
}
}
def projectForKey = { String key ->
ProjectManager projectManager = ComponentAccessor.getProjectManager()
Project project = projectManager.getProjectObjByKey(key)
return project
}
def epicsForProject = { Project project ->
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Query query = JqlQueryBuilder.newBuilder().where().project(project.key).buildQuery()
IssueManager issueManager = ComponentAccessor.getIssueManager()
SearchResults searchResult = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
Collection<Issue> issues = searchResult.issues
return issues
.findAll { it.getIssueType().getName() == "Epic" }
.collect { issueManager.getIssueObject(it.id) }
}
// Logic
Project project = projectForKey("PROJECTKEY") // Project key
Collection<MutableIssue> epics = epicsForProject(project)
epics.collect(setComponentsAndVersionsOfIssuesToEpic)
// ---
// Debug
epics.collect { log.debug(it) }