PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/resources/com/onresolve/jira/groovy/canned/workflow/postfunctions/UpdateBlockedIssues.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 124 lines | 99 code | 24 blank | 1 comment | 10 complexity | 0c8a690b295f2c04d283bcc457dca5f2 MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.workflow.postfunctions
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.issue.Issue
  4. import com.atlassian.jira.util.ErrorCollection
  5. import com.onresolve.jira.groovy.canned.CannedScript
  6. import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
  7. import org.apache.log4j.Category
  8. import com.atlassian.jira.issue.link.IssueLinkManager
  9. import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
  10. import com.atlassian.jira.issue.link.IssueLink
  11. import com.atlassian.jira.issue.MutableIssue
  12. import com.atlassian.jira.issue.comments.CommentManager
  13. import com.atlassian.jira.util.SimpleErrorCollection
  14. import com.atlassian.crowd.embedded.api.User
  15. class UpdateBlockedIssues implements CannedScript{
  16. ComponentManager componentManager = ComponentManager.getInstance()
  17. Category log = Category.getInstance(UpdateBlockedIssues.class)
  18. def projectManager = componentManager.getProjectManager()
  19. public static final String FIELD_LINK_TYPE = 'FIELD_LINK_TYPE'
  20. public static final String FIELD_COMMENT = 'FIELD_COMMENT'
  21. String getName() {
  22. "Adds a comment to all blocked issues when this issue is transitioned."
  23. }
  24. public String getHelpUrl() {
  25. "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Updateblockingissues"
  26. }
  27. String getDescription() {
  28. "Useful for alerting participants of other issues that a blocker is resolved, etc." + """<br>
  29. This function should be put on the Resolve transition (or similar).
  30. """
  31. }
  32. List getCategories() {
  33. ["Function"]
  34. }
  35. List getParameters(Map params) {
  36. [
  37. // possibly require addition comment and additional-code fields here
  38. [
  39. Name:FIELD_LINK_TYPE,
  40. Label:'Issue Link Type',
  41. Type: "list",
  42. Description:"What link types to look for... e.g. <b><i>blocks</i> will add a comment on all issues that this one blocks</b>",
  43. Values: CannedScriptUtils.getAllLinkTypesWithDirections(true)
  44. ],
  45. [
  46. Name:FIELD_COMMENT,
  47. Label: 'Comment',
  48. Description: 'Comment to add to linked issues. Click an example below.',
  49. Type:'mediumtext',
  50. Examples:[
  51. "Blocking issue ABC-123 resolved with resolution Fixed.":
  52. "Blocking issue \$issue resolved with resolution <% out << issue.resolution?.name %>."
  53. ],
  54. ]
  55. ]
  56. }
  57. public ErrorCollection doValidate(Map params, boolean forPreview) {
  58. SimpleErrorCollection errorCollection = new SimpleErrorCollection()
  59. if (! params[FIELD_COMMENT]) {
  60. errorCollection.addError(FIELD_COMMENT, "You must provide a comment. Editing this function and clicking the example link will copy an example.")
  61. }
  62. if (! params[FIELD_LINK_TYPE]) {
  63. errorCollection.addError(FIELD_LINK_TYPE, "You must provide a link type.")
  64. }
  65. errorCollection
  66. }
  67. Map doScript(Map params) {
  68. Issue issue = params['issue'] as Issue
  69. String linkTypeStr = params[FIELD_LINK_TYPE] as String
  70. String commentTemplate = params[FIELD_COMMENT] as String
  71. String linkTypeId = ""
  72. String linkDirection = ""
  73. (linkTypeId, linkDirection) = linkTypeStr.split(/ /) as List
  74. IssueLinkManager linkMgr = ComponentManager.getInstance().getIssueLinkManager()
  75. CommentManager commentManager = componentManager.getCommentManager()
  76. User currentUser = WorkflowUtils.getUser(params)
  77. List links = []
  78. if (linkDirection == CannedScriptUtils.OUTWARD_FIELD_NAME) {
  79. links = linkMgr.getOutwardLinks(issue.id)
  80. }
  81. else if (linkDirection == CannedScriptUtils.INWARD_FIELD_NAME) {
  82. links = linkMgr.getInwardLinks(issue.id)
  83. }
  84. links.each {IssueLink link ->
  85. if (link.getLinkTypeId() == linkTypeId as Long) {
  86. MutableIssue destIssue = linkDirection == CannedScriptUtils.OUTWARD_FIELD_NAME ? link.getDestinationObject() : link.getSourceObject()
  87. Writable template = WorkflowUtils.mergeTemplate(params, commentTemplate)
  88. log.info ("Create comment on ${destIssue.key}")
  89. commentManager.create(destIssue, currentUser.getName(), template.toString(), true)
  90. }
  91. }
  92. [:]
  93. }
  94. String getDescription(Map params, boolean forPreview) {
  95. getName()
  96. }
  97. public Boolean isFinalParamsPage(Map params) {
  98. true
  99. }
  100. }