@EventListener public void onIssueEvent(IssueEvent issueEvent) { if (!issueEvent.getEventTypeId().equals(EventType.ISSUE_CREATED_ID)) { return; } Issue issue = issueEvent.getIssue(); Collection<ProjectComponent> comps = issue.getComponentObjects(); if (comps == null || comps.size() < 2) { return; } Project project = issue.getProjectObject(); if (project == null || !isApplyPlugin(project.getId())) { return; } List<Issue> newIssues = new ArrayList<Issue>(); ProjectComponent gv = null; Iterator<ProjectComponent> iter = comps.iterator(); while (iter.hasNext()) { gv = iter.next(); MutableIssue nissue = ComponentManager.getInstance().getIssueFactory().getIssue(); // --> summary nissue.setSummary(String.format("[%s] %s", gv.getName(), issue.getSummary())); // --> project if (issue.getProjectObject() != null) { nissue.setProjectId(issue.getProjectObject().getId()); } // --> issue type if (issue.getIssueTypeObject() != null) { nissue.setIssueTypeId(issue.getIssueTypeObject().getId()); } // --> components Collection<ProjectComponent> nComps = new LinkedList<ProjectComponent>(); nComps.add(gv); nissue.setComponentObjects(nComps); // --> assignee String compLead = gv.getLead(); nissue.setAssigneeId(compLead); // --> reporter nissue.setReporter(issueEvent.getUser()); // --> priority nissue.setPriorityObject(issue.getPriorityObject()); // --> description nissue.setDescription(issue.getDescription()); // --> env nissue.setEnvironment(issue.getEnvironment()); // --> due date nissue.setDueDate(issue.getDueDate()); // --> estimate nissue.setEstimate(issue.getEstimate()); // --> labels nissue.setLabels(issue.getLabels()); nissue.setAffectedVersions(issue.getAffectedVersions()); nissue.setWorkflowId(issue.getWorkflowId()); nissue.setParentId(issue.getParentId()); // --> status if (issue.getStatusObject() != null) { nissue.setStatusId(issue.getStatusObject().getId()); } // --> resolution if (issue.getResolutionObject() != null) { nissue.setResolutionId(issue.getResolutionObject().getId()); } nissue.setFixVersions(issue.getFixVersions()); nissue.setResolutionDate(issue.getResolutionDate()); nissue.setTimeSpent(issue.getTimeSpent()); nissue.setVotes(issue.getVotes()); nissue.setCreated(issue.getCreated()); nissue.setSecurityLevelId(issue.getSecurityLevelId()); nissue.setOriginalEstimate(issue.getOriginalEstimate()); List<CustomField> cfs = ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObjects(issue); if (cfs != null) { for (CustomField cf : cfs) { Object cfVal = issue.getCustomFieldValue(cf); if (cfVal != null) { nissue.setCustomFieldValue(cf, cfVal); } } } // --> create issue try { Issue newIssueObj = ComponentManager.getInstance() .getIssueManager() .createIssueObject(issueEvent.getUser(), nissue); newIssues.add(newIssueObj); } catch (CreateException crex) { log.error("IssueClonerByComponents::onIssueEvent - Cannot create dependent issues", crex); } } Collection<Attachment> atts = issue.getAttachments(); if (atts != null) { AttachmentManager am = ComponentManager.getInstance().getAttachmentManager(); for (Attachment att : atts) { File attFile = AttachmentUtils.getAttachmentFile(att); String filename = att.getFilename(); String contentType = att.getMimetype(); for (Issue nissue : newIssues) { File newFile = new File(attFile.getAbsolutePath() + nissue.getKey()); try { FileUtils.copyFile(attFile, newFile); am.createAttachment(newFile, filename, contentType, issueEvent.getUser(), nissue); } catch (Exception ex) { log.error("IssueClonerByComponents::onIssueEvent - Cannot copy attachment", ex); } } } } IssueLinkTypeManager issueLinkTypeManager = ComponentManager.getComponentInstanceOfType(IssueLinkTypeManager.class); Collection<IssueLinkType> types = issueLinkTypeManager.getIssueLinkTypesByName(LINK_TYPE); if (types == null || types.isEmpty()) { return; } IssueLinkType ilt = types.iterator().next(); if (ilt != null) { IssueLinkManager ilm = ComponentManager.getInstance().getIssueLinkManager(); for (Issue nissue : newIssues) { try { ilm.createIssueLink( issue.getId(), nissue.getId(), ilt.getId(), null, issueEvent.getUser()); } catch (CreateException crex) { log.error("IssueClonerByComponents::onIssueEvent - Cannot create link", crex); } } } }
@Override public boolean handleMessage(Message message, MessageHandlerContext context) throws MessagingException { log.debug("CreateIssueHandler.handleMessage"); if (!canHandleMessage(message, context.getMonitor())) { return deleteEmail; } try { // get either the sender of the message, or the default reporter User reporter = getReporter(message, context); // no reporter - so reject the message if (reporter == null) { final String error = getI18nBean().getText("admin.mail.no.default.reporter"); context.getMonitor().warning(error); context.getMonitor().messageRejected(message, error); return false; } final Project project = getProject(message); log.debug("Project = " + project); if (project == null) { final String text = getI18nBean().getText("admin.mail.no.project.configured"); context.getMonitor().warning(text); context.getMonitor().messageRejected(message, text); return false; } // Check that the license is valid before allowing issues to be created // This checks for: evaluation licenses expired, user limit licenses where limit has been // exceeded ErrorCollection errorCollection = new SimpleErrorCollection(); // Note: want English locale here for logging purposes I18nHelper i18nHelper = new I18nBean(Locale.ENGLISH); getIssueCreationHelperBean().validateLicense(errorCollection, i18nHelper); if (errorCollection.hasAnyErrors()) { context .getMonitor() .warning( getI18nBean() .getText( "admin.mail.bad.license", errorCollection.getErrorMessages().toString())); return false; } // If user does not have create permissions, there's no point proceeding. Error out here to // avoid a stack // trace blow up from the WorkflowManager later on. if (!getPermissionManager().hasPermission(Permissions.CREATE_ISSUE, project, reporter, true) && reporter.getDirectoryId() != -1) { final String error = getI18nBean().getText("admin.mail.no.create.permission", reporter.getName()); context.getMonitor().warning(error); context.getMonitor().messageRejected(message, error); return false; } log.debug("Issue Type Key = = " + issueType); if (!hasValidIssueType()) { context.getMonitor().warning(getI18nBean().getText("admin.mail.invalid.issue.type")); return false; } String summary = message.getSubject(); if (!TextUtils.stringSet(summary)) { context.getMonitor().error(getI18nBean().getText("admin.mail.no.subject")); return false; } if (summary.length() > SummarySystemField.MAX_LEN.intValue()) { context.getMonitor().info("Truncating summary field because it is too long: " + summary); summary = summary.substring(0, SummarySystemField.MAX_LEN.intValue() - 3) + "..."; } // JRA-7646 - check if priority/description is hidden - if so, do not set String priority = null; String description = null; if (!getFieldVisibilityManager() .isFieldHiddenInAllSchemes( project.getId(), IssueFieldConstants.PRIORITY, Collections.singletonList(issueType))) { priority = getPriority(message); } if (!getFieldVisibilityManager() .isFieldHiddenInAllSchemes( project.getId(), IssueFieldConstants.DESCRIPTION, Collections.singletonList(issueType))) { description = getDescription(reporter, message); } MutableIssue issueObject = getIssueFactory().getIssue(); issueObject.setProjectObject(project); issueObject.setSummary(summary); issueObject.setDescription(description); issueObject.setIssueTypeId(issueType); issueObject.setReporter(reporter); // if no valid assignee found, attempt to assign to default assignee User assignee = null; if (ccAssignee) { assignee = getFirstValidAssignee(message.getAllRecipients(), project); } if (assignee == null) { assignee = getAssigneeResolver().getDefaultAssignee(issueObject, Collections.EMPTY_MAP); } if (assignee != null) { issueObject.setAssignee(assignee); } issueObject.setPriorityId(priority); // Ensure issue level security is correct setDefaultSecurityLevel(issueObject); /* * + FIXME -- set cf defaults @todo + */ // set default custom field values // CustomFieldValuesHolder cfvh = new CustomFieldValuesHolder(issueType, project.getId()); // fields.put("customFields", CustomFieldUtils.getCustomFieldValues(cfvh.getCustomFields())); Map<String, Object> fields = new HashMap<String, Object>(); fields.put("issue", issueObject); // TODO: How is this supposed to work? There is no issue created yet; ID = null. // wseliga note: Ineed I think that such call does not make sense - it will be always null MutableIssue originalIssue = getIssueManager().getIssueObject(issueObject.getId()); // Give the CustomFields a chance to set their default values JRA-11762 List<CustomField> customFieldObjects = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issueObject); for (CustomField customField : customFieldObjects) { issueObject.setCustomFieldValue(customField, customField.getDefaultValue(issueObject)); } fields.put(WorkflowFunctionUtils.ORIGINAL_ISSUE_KEY, originalIssue); final Issue issue = context.createIssue(reporter, issueObject); if (issue != null) { // Add Cc'ed users as watchers if params set - JRA-9983 if (ccWatcher) { addCcWatchersToIssue(message, issue, reporter, context, context.getMonitor()); } // Record the message id of this e-mail message so we can track replies to this message // and associate them with this issue recordMessageId( MailThreadManager.ISSUE_CREATED_FROM_EMAIL, message, issue.getId(), context); } // TODO: if this throws an error, then the issue is already created, but the email not deleted // - we will keep "handling" this email over and over :( createAttachmentsForMessage(message, issue, context); return true; } catch (Exception e) { context.getMonitor().warning(getI18nBean().getText("admin.mail.unable.to.create.issue"), e); } // something went wrong - don't delete the message return false; }