|
| 1 | +import com.atlassian.jira.issue.watchers.WatcherManager |
| 2 | +import org.apache.log4j.Level |
| 3 | +import org.apache.log4j.Logger |
| 4 | +import org.apache.commons.collections.Closure |
| 5 | +import com.atlassian.jira.issue.customfields.option.LazyLoadedOption |
| 6 | +import com.atlassian.jira.project.Project |
| 7 | +import com.atlassian.jira.project.ProjectManager |
| 8 | +import com.atlassian.jira.issue.link.IssueLink |
| 9 | +import com.atlassian.jira.issue.link.IssueLinkManager |
| 10 | +import com.atlassian.jira.issue.customfields.manager.OptionsManager |
| 11 | +import com.atlassian.jira.issue.Issue |
| 12 | +import com.atlassian.jira.event.type.EventDispatchOption |
| 13 | +import com.atlassian.jira.issue.IssueManager |
| 14 | +import com.atlassian.jira.issue.fields.CustomField |
| 15 | +import com.atlassian.jira.issue.customfields.option.Option |
| 16 | +import com.atlassian.jira.issue.CustomFieldManager |
| 17 | +import com.atlassian.jira.issue.MutableIssue |
| 18 | +import com.atlassian.jira.bc.issue.IssueService |
| 19 | +import com.atlassian.jira.component.ComponentAccessor |
| 20 | +import com.atlassian.jira.issue.IssueInputParameters |
| 21 | +import com.atlassian.jira.user.ApplicationUser |
| 22 | +import com.atlassian.jira.bc.project.component.ProjectComponent |
| 23 | + |
| 24 | + |
| 25 | +Logger logger = Logger.getLogger("sacos.s5k.createEpics") |
| 26 | +logger.setLevel(Level.ALL) |
| 27 | + |
| 28 | +IssueService issueService = ComponentAccessor.getIssueService() |
| 29 | +IssueManager issueManager = ComponentAccessor.getIssueManager() |
| 30 | +IssueInputParameters issueInputParameters = issueService.newIssueInputParameters(); |
| 31 | +CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() |
| 32 | +ProjectManager projectManager = ComponentAccessor.getProjectManager() |
| 33 | +WatcherManager watcherManager = ComponentAccessor.getWatcherManager() |
| 34 | +IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager() |
| 35 | +OptionsManager optionsManager = ComponentAccessor.getOptionsManager() |
| 36 | +def projectComponentManager = ComponentAccessor.getProjectComponentManager() |
| 37 | + |
| 38 | +@com.onresolve.scriptrunner.parameters.annotation.ShortTextInput(description = "Enter Jira to execute the script on - leave empty for non-debug modes", label = "Jira Request Issue") |
| 39 | +String jiraDebugIssue |
| 40 | + |
| 41 | +logger.info("jiraDebugIssue=" + jiraDebugIssue) |
| 42 | + |
| 43 | +//Issue issue |
| 44 | +if ( jiraDebugIssue != null ){ |
| 45 | + issue = issueManager.getIssueObject(jiraDebugIssue) // Add an issue for testing |
| 46 | + logger.setLevel(Level.ALL) // ALL, WARN |
| 47 | +} |
| 48 | +ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() |
| 49 | + |
| 50 | +if(issue.getIssueType().getName() != "Request"){ |
| 51 | + return; |
| 52 | +} |
| 53 | + |
| 54 | +CustomField cf_targetProducts = customFieldManager.getCustomFieldObject(12121) // "Target products" |
| 55 | +Collection<String> targetProducts = (Collection<String>)issue.getCustomFieldValue(cf_targetProducts) |
| 56 | +targetProducts = targetProducts.collect { it -> it.toString() } |
| 57 | +logger.info("Target products: " + targetProducts ) |
| 58 | + |
| 59 | +Collection<String> target = targetProducts.collect { it -> it.toString() } |
| 60 | + |
| 61 | +// two convenient closures |
| 62 | +def isProject(String key, IssueLink it) { |
| 63 | + it.destinationObject.getProjectObject().getKey() == key |
| 64 | +} |
| 65 | + |
| 66 | +def isChildParentLink(IssueLink it){ |
| 67 | + it.getIssueLinkType().getName() == "Hierarchy" |
| 68 | +} |
| 69 | + |
| 70 | +// already done links |
| 71 | +List<String> alreadyDone = issueLinkManager.getOutwardLinks(issue.id).each { isChildParentLink(it) }.collect { IssueLink it -> |
| 72 | + it.destinationObject.getProjectObject().getKey() |
| 73 | +}.toList() |
| 74 | +Set<String> relevantProjects = targetProducts.toSet().minus(alreadyDone.toSet()) |
| 75 | + |
| 76 | +CustomField cf_ewh = customFieldManager.getCustomFieldObject(12154) // Expected Work Hours |
| 77 | +CustomField cf_ec = customFieldManager.getCustomFieldObject(12112) // Expected Cost |
| 78 | + |
| 79 | +// Expected Work Hours |
| 80 | +logger.warn(cf_ewh) |
| 81 | +def fieldConfig_ewh = cf_ewh.getRelevantConfig(issue) |
| 82 | +def option_ewh = optionsManager.getOptions(fieldConfig_ewh).getOptionForValue((String)issue.getCustomFieldValue(cf_ewh), null) |
| 83 | + |
| 84 | +List<Option> options_ewh = optionsManager.findByOptionValue((String)issue.getCustomFieldValue(cf_ewh)); |
| 85 | +CustomField customField_ewh = option_ewh.getRelatedCustomField().getCustomField(); |
| 86 | + |
| 87 | +// Expected Cost |
| 88 | +def fieldConfig_ec = cf_ec.getRelevantConfig(issue) |
| 89 | +def option_ec = optionsManager.getOptions(fieldConfig_ec).getOptionForValue((String)issue.getCustomFieldValue(cf_ec), null) |
| 90 | + |
| 91 | +List<Option> options_ec = optionsManager.findByOptionValue((String)issue.getCustomFieldValue(cf_ec)); |
| 92 | +CustomField customField_ec = option_ec.getRelatedCustomField().getCustomField(); |
| 93 | + |
| 94 | +logger.info(issue.key) |
| 95 | +Collection<String> requestComponents = issue.getComponents().collect { ProjectComponent component -> |
| 96 | + logger.info("Source project component names: ${component.getName()}" ) |
| 97 | + component.getName() |
| 98 | +} |
| 99 | + |
| 100 | +Collection<Project> projects = relevantProjects.collect { String it -> projectManager.getProjectByCurrentKey(it) } |
| 101 | + |
| 102 | +projects.each { Project project -> |
| 103 | + logger.info("Target project name-ID: ${project.getName()} (${project.getKey()}) internal ID: ${project.getId()}") |
| 104 | + Long[] thisProjectCompIds = requestComponents.collect { String compString -> |
| 105 | + projectComponentManager.findByComponentName(project.getId(),compString).getId() |
| 106 | + } as Long[] |
| 107 | + logger.info("Target project: ComponentID " + thisProjectCompIds.collect{ Long it -> it}) |
| 108 | + |
| 109 | + |
| 110 | + IssueService.IssueResult createResult; |
| 111 | + |
| 112 | + def exists = issueLinkManager.getOutwardLinks(issue.id).find { IssueLink it -> |
| 113 | + isProject(project.getKey(), it) && isChildParentLink(it) |
| 114 | + } != null |
| 115 | + |
| 116 | + if(!exists){ |
| 117 | + log.info("creating") |
| 118 | + issueInputParameters |
| 119 | + .setProjectId(project.getId()) |
| 120 | + .setSummary("[" + project.getKey() + "] " + issue.getSummary()) |
| 121 | + .setReporterId(issue.getReporter().username) |
| 122 | + .setIssueTypeId(issue.getIssueTypeId()) |
| 123 | + .setDescription("Intentionally left empty, see Parent for details") |
| 124 | + .setComponentIds(thisProjectCompIds) |
| 125 | + .addCustomFieldValue(12129, "-") // Accounting |
| 126 | + .addCustomFieldValue(10002, "[" + project.getKey() + "] " + issue.getSummary() ) // Epic Name |
| 127 | + .setPriorityId(issue.getPriority().getId()) |
| 128 | + .setIssueTypeId("10000") // Epic |
| 129 | + .addCustomFieldValue(customField_ewh.getId(), "" + option_ewh.getOptionId()) |
| 130 | + .addCustomFieldValue(customField_ec.getId(), "" + option_ec.getOptionId()) |
| 131 | + |
| 132 | + IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters) |
| 133 | + |
| 134 | + if (createValidationResult.isValid()) |
| 135 | + { |
| 136 | + createResult = issueService.create(user, createValidationResult) |
| 137 | + |
| 138 | + if (!createResult.isValid()) |
| 139 | + { |
| 140 | + log.error("Error while creating the issue.") |
| 141 | + } else { |
| 142 | + MutableIssue newIssue = createResult.getIssue() as MutableIssue |
| 143 | + // child/parent of link |
| 144 | + issueLinkManager.createIssueLink(issue.id, newIssue.id, 10900, 0, user) // Hierarchy |
| 145 | + watcherManager.stopWatching(user, newIssue); |
| 146 | + } |
| 147 | + } |
| 148 | + else { |
| 149 | + log.error(createValidationResult.errorCollection); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments