Skip to content

Commit b98ae1a

Browse files
authored
Create scriptrunner-create-issues-based-on-multiselected-cf.groovy
1 parent 4460ce8 commit b98ae1a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import com.atlassian.jira.bc.project.component.ProjectComponent
2+
import com.atlassian.jira.issue.watchers.WatcherManager
3+
import org.apache.log4j.Level
4+
import org.apache.log4j.Logger
5+
import org.apache.commons.collections.Closure
6+
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
7+
import com.atlassian.jira.project.Project
8+
import com.atlassian.jira.project.ProjectManager
9+
import com.atlassian.jira.issue.link.IssueLink
10+
import com.atlassian.jira.issue.link.IssueLinkManager
11+
import com.atlassian.jira.issue.customfields.manager.OptionsManager
12+
import com.atlassian.jira.issue.Issue
13+
import com.atlassian.jira.event.type.EventDispatchOption
14+
import com.atlassian.jira.issue.IssueManager
15+
import com.atlassian.jira.issue.fields.CustomField
16+
import com.atlassian.jira.issue.customfields.option.Option
17+
import com.atlassian.jira.issue.CustomFieldManager
18+
import com.atlassian.jira.issue.MutableIssue
19+
import com.atlassian.jira.bc.issue.IssueService
20+
import com.atlassian.jira.component.ComponentAccessor
21+
import com.atlassian.jira.issue.IssueInputParameters
22+
import com.atlassian.jira.user.ApplicationUser
23+
24+
Logger logger = Logger.getLogger("sacos.s5k.evalcreate")
25+
logger.setLevel(Level.ALL)
26+
27+
IssueService issueService = ComponentAccessor.getIssueService()
28+
IssueManager issueManager = ComponentAccessor.getIssueManager()
29+
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
30+
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
31+
ProjectManager projectManager = ComponentAccessor.getProjectManager()
32+
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
33+
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
34+
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
35+
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
36+
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
37+
38+
def issue = issueManager.getIssueObject('SACOS-255')
39+
40+
Project project = projectManager.getProjectByCurrentKey("SACEVAL")
41+
String evalIssueTypeId = 10201
42+
Long requestToEvalLinkTypeId = 10000 // Blocks
43+
Long requestCfEvalComponents = 12200 // "Eval components"
44+
45+
if(issue.getIssueType().getName() != "Request"){
46+
logger.info("Issue type is not executed on type Request: " + issue.getIssueType().getName())
47+
return;
48+
}
49+
50+
CustomField cf_targetEvals = customFieldManager.getCustomFieldObject(requestCfEvalComponents)
51+
Collection<String> targetEvals = (Collection<String>)issue.getCustomFieldValue(cf_targetEvals)
52+
targetEvals = targetEvals.collect { it -> it.toString() }
53+
54+
// two convenient closures
55+
def isProject(String key, IssueLink it) {
56+
it.destinationObject.getProjectObject().getKey() == key
57+
}
58+
59+
def isEvaluationTypeBlocksLink(IssueLink it, String evalIssueTypeId, Long requestToEvalLinkTypeId, Logger logger){
60+
if ( it.getDestinationObject().getIssueType().getId() == evalIssueTypeId && it.getIssueLinkType().getId() == requestToEvalLinkTypeId ){
61+
logger.info(it.getDestinationObject().getIssueType().getName() + ": " + it.getIssueLinkType().getName()+ ": Ok" )
62+
return true
63+
} else {
64+
logger.info(it.getDestinationObject().getIssueType().getName() + ": " + it.getIssueLinkType().getName()+ ": Skip" )
65+
return false
66+
}
67+
}
68+
69+
logger.info(issue.key)
70+
List<String> alreadyCreated = issueLinkManager.getOutwardLinks(issue.id).each { isEvaluationTypeBlocksLink(it, evalIssueTypeId, requestToEvalLinkTypeId, logger) }.collect { IssueLink it ->
71+
it.destinationObject?.getComponents().collect {
72+
ProjectComponent pc -> pc.getName()
73+
}
74+
}.flatten() as List<String>
75+
76+
logger.info("Target Evals: " + targetEvals.toSet())
77+
logger.info("Already Evals: " + alreadyCreated.toSet())
78+
Set<String> relevantEvals = targetEvals.toSet().minus(alreadyCreated.toSet())
79+
logger.info("Missing Evals: " + relevantEvals)
80+
81+
relevantEvals.each { String evalComponent ->
82+
IssueService.IssueResult createResult;
83+
84+
ProjectComponent componentName = projectComponentManager.findByComponentName(project.getId(), evalComponent)
85+
String summary = "[" + evalComponent + "] " + issue.getSummary()
86+
87+
issueInputParameters
88+
.setProjectId(project.getId())
89+
.setSummary(summary)
90+
.setReporterId(issue.getReporterId())
91+
.setDescription("Intentionally left empty, see Parent for details")
92+
.setPriorityId(issue.getPriority().getId())
93+
.setIssueTypeId(evalIssueTypeId)
94+
.setComponentIds(componentName.getId())
95+
96+
IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters)
97+
98+
if (createValidationResult.isValid())
99+
{
100+
createResult = issueService.create(user, createValidationResult)
101+
102+
if (!createResult.isValid())
103+
{
104+
log.error("Error while creating the issue.")
105+
} else {
106+
MutableIssue newIssue = createResult.getIssue() as MutableIssue
107+
// child/parent of link
108+
logger.info("NewIssue: " + newIssue.getKey() + ": " + newIssue.getAssignee() + ": "+ newIssue.getSummary())
109+
issueLinkManager.createIssueLink(issue.id, newIssue.id, requestToEvalLinkTypeId, 0, user)
110+
watcherManager.stopWatching(user, newIssue);
111+
112+
}
113+
}
114+
else {
115+
log.error(createValidationResult.errorCollection);
116+
}
117+
}

0 commit comments

Comments
 (0)