/jira-automation-plugin/src/main/java/com/atlassian/plugin/automation/jira/action/CommentIssueAction.java

https://bitbucket.org/atlassianlabs/automation · Java · 207 lines · 185 code · 18 blank · 4 comment · 13 complexity · 821a9db56704faaf159c3f740c35e604 MD5 · raw file

  1. package com.atlassian.plugin.automation.jira.action;
  2. import com.atlassian.fugue.Either;
  3. import com.atlassian.jira.bc.issue.IssueService;
  4. import com.atlassian.jira.bc.issue.comment.CommentService;
  5. import com.atlassian.jira.issue.Issue;
  6. import com.atlassian.jira.user.ApplicationUser;
  7. import com.atlassian.jira.user.util.UserManager;
  8. import com.atlassian.jira.util.SimpleErrorCollection;
  9. import com.atlassian.plugin.automation.core.Action;
  10. import com.atlassian.plugin.automation.core.action.ActionConfiguration;
  11. import com.atlassian.plugin.automation.core.auditlog.AuditString;
  12. import com.atlassian.plugin.automation.core.auditlog.DefaultAuditString;
  13. import com.atlassian.plugin.automation.jira.util.ErrorCollectionUtil;
  14. import com.atlassian.plugin.automation.jira.util.SecurityLevelContextProvider;
  15. import com.atlassian.plugin.automation.util.ErrorCollection;
  16. import com.atlassian.plugin.automation.util.ParameterUtil;
  17. import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
  18. import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
  19. import com.atlassian.sal.api.message.I18nResolver;
  20. import com.atlassian.soy.renderer.SoyException;
  21. import com.atlassian.soy.renderer.SoyTemplateRenderer;
  22. import com.atlassian.templaterenderer.RenderingException;
  23. import com.atlassian.templaterenderer.TemplateRenderer;
  24. import com.google.common.collect.Maps;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.apache.log4j.Logger;
  27. import javax.inject.Inject;
  28. import java.util.List;
  29. import java.util.Map;
  30. import static com.atlassian.plugin.automation.jira.util.Constants.CONFIG_COMPLETE_KEY;
  31. import static com.atlassian.plugin.automation.util.ParameterUtil.singleValue;
  32. /**
  33. * Implements comment on issue
  34. */
  35. @Scanned
  36. public class CommentIssueAction implements Action<Issue>
  37. {
  38. public static final String COMMENT_KEY = "jiraComment";
  39. public static final String COMMENT_NOTIFICATION_KEY = "jiraCommentNotification";
  40. public static final String COMMENT_SECURITY_KEY = "jiraCommentSecurityLevel";
  41. private static final Logger log = Logger.getLogger(CommentIssueAction.class);
  42. private static final int MAX_COMMENT_LENGTH = 60;
  43. private final CommentService commentService;
  44. private final UserManager userManager;
  45. private final IssueService issueService;
  46. private final SoyTemplateRenderer soyTemplateRenderer;
  47. private final TemplateRenderer templateRenderer;
  48. private final SecurityLevelContextProvider securityLevelContextProvider;
  49. private String comment;
  50. private Either<String, Long> securityLevel;
  51. private boolean sendNotification;
  52. @Inject
  53. public CommentIssueAction(
  54. @ComponentImport final CommentService commentService,
  55. @ComponentImport final UserManager userManager,
  56. @ComponentImport final IssueService issueService,
  57. @ComponentImport final SoyTemplateRenderer soyTemplateRenderer,
  58. @ComponentImport final TemplateRenderer templateRenderer,
  59. final SecurityLevelContextProvider securityLevelContextProvider)
  60. {
  61. this.commentService = commentService;
  62. this.userManager = userManager;
  63. this.issueService = issueService;
  64. this.soyTemplateRenderer = soyTemplateRenderer;
  65. this.templateRenderer = templateRenderer;
  66. this.securityLevelContextProvider = securityLevelContextProvider;
  67. }
  68. @Override
  69. public void init(ActionConfiguration config)
  70. {
  71. comment = singleValue(config, COMMENT_KEY);
  72. securityLevel = securityLevelContextProvider.extractValue(singleValue(config, COMMENT_SECURITY_KEY));
  73. sendNotification = Boolean.parseBoolean(singleValue(config, (COMMENT_NOTIFICATION_KEY)));
  74. }
  75. @Override
  76. public void execute(String actor, Iterable<Issue> items, ErrorCollection errorCollection)
  77. {
  78. log.debug("Processing issues");
  79. final ApplicationUser user = userManager.getUserByName(actor);
  80. for (Issue issue : items)
  81. {
  82. log.debug("Processing the issue: " + issue.getKey());
  83. final Issue mutableIssue = issueService.getIssue(user, issue.getId()).getIssue();
  84. com.atlassian.jira.util.ErrorCollection jiraErrorCollection = new SimpleErrorCollection();
  85. // Render the fragment
  86. final Map<String, Object> context = Maps.newHashMap();
  87. context.put("issue", issue);
  88. context.put("reporter", issue.getReporter());
  89. context.put("project", issue.getProjectObject());
  90. try
  91. {
  92. final String renderedComment = templateRenderer.renderFragment(comment, context);
  93. if (securityLevel != null)
  94. {
  95. if (securityLevel.isLeft())
  96. {
  97. commentService.create(user, mutableIssue, renderedComment, securityLevel.left().get(), null,
  98. sendNotification, jiraErrorCollection);
  99. }
  100. else if (securityLevel.isRight())
  101. {
  102. commentService.create(user, mutableIssue, renderedComment, null, securityLevel.right().get(),
  103. sendNotification, jiraErrorCollection);
  104. }
  105. }
  106. else
  107. {
  108. commentService.create(user, mutableIssue, renderedComment, null, null, sendNotification, jiraErrorCollection);
  109. }
  110. }
  111. catch (RenderingException e)
  112. {
  113. log.error("Unable to render the comment", e);
  114. errorCollection.addErrorMessage(String.format("Unable to add comment on issue '%s'", issue.getKey()));
  115. }
  116. if (jiraErrorCollection.hasAnyErrors())
  117. {
  118. log.error(String.format("Unable to add comment on issue '%s' using actor '%s': %s", issue.getKey(), actor, jiraErrorCollection));
  119. errorCollection.addErrorCollection(ErrorCollectionUtil.transform(jiraErrorCollection));
  120. }
  121. }
  122. }
  123. @Override
  124. public AuditString getAuditLog()
  125. {
  126. return new DefaultAuditString(String.format("Comment issue - Adding comment '%s'",
  127. StringUtils.abbreviateMiddle(comment, "...", MAX_COMMENT_LENGTH)));
  128. }
  129. @Override
  130. public String getConfigurationTemplate(ActionConfiguration config, final String actor)
  131. {
  132. try
  133. {
  134. final Map<String, Object> context = Maps.newHashMap();
  135. String currentSecurityLevel = null;
  136. ParameterUtil.transformParams(context, config);
  137. if (config != null)
  138. {
  139. currentSecurityLevel = singleValue(config, COMMENT_SECURITY_KEY);
  140. }
  141. final Map<String, Object> securityLevelContext = securityLevelContextProvider.getContext(userManager.getUser(actor), null, currentSecurityLevel);
  142. context.put("securityLevel", securityLevelContext);
  143. return soyTemplateRenderer.render(CONFIG_COMPLETE_KEY, "Atlassian.Templates.Automation.JIRA.commentAction", context);
  144. }
  145. catch (SoyException e)
  146. {
  147. log.error("Error rendering template", e);
  148. return "Unable to render configuration form. Consult your server logs or administrator.";
  149. }
  150. }
  151. @Override
  152. public String getViewTemplate(final ActionConfiguration config, final String actor)
  153. {
  154. try
  155. {
  156. final Map<String, Object> context = Maps.newHashMap();
  157. String currentSecurityLevel = null;
  158. ParameterUtil.transformParams(context, config);
  159. if (config != null)
  160. {
  161. currentSecurityLevel = singleValue(config, COMMENT_SECURITY_KEY);
  162. }
  163. final Map<String, Object> securityLevelContext = securityLevelContextProvider.getContext(userManager.getUserByName(actor), null, currentSecurityLevel);
  164. context.put("securityLevel", securityLevelContext);
  165. return soyTemplateRenderer.render(CONFIG_COMPLETE_KEY, "Atlassian.Templates.Automation.JIRA.commentActionView", context);
  166. }
  167. catch (SoyException e)
  168. {
  169. log.error("Error rendering template", e);
  170. return "Unable to render configuration form. Consult your server logs or administrator.";
  171. }
  172. }
  173. @Override
  174. public ErrorCollection validateAddConfiguration(I18nResolver i18n, Map<String, List<String>> params, final String actor)
  175. {
  176. final ErrorCollection errorCollection = new ErrorCollection();
  177. if (StringUtils.isBlank(singleValue(params, COMMENT_KEY)))
  178. {
  179. errorCollection.addError(COMMENT_KEY, i18n.getText("automation.jira.comment.invalid"));
  180. }
  181. final String securityKey = singleValue(params, COMMENT_SECURITY_KEY);
  182. if (StringUtils.isNotBlank(securityKey))
  183. {
  184. errorCollection.addErrorCollection(securityLevelContextProvider.validate(i18n,
  185. userManager.getUser(actor), null, securityKey));
  186. }
  187. return errorCollection;
  188. }
  189. }