PageRenderTime 87ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 120 lines | 95 code | 23 blank | 2 comment | 6 complexity | d3b04be0e999ebb506e1adbcb603e925 MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.workflow.postfunctions
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.config.ConstantsManager
  4. import com.atlassian.jira.issue.Issue
  5. import com.atlassian.jira.issue.MutableIssue
  6. import com.atlassian.jira.issue.link.IssueLinkManager
  7. import com.atlassian.jira.issue.link.IssueLinkType
  8. import com.atlassian.jira.issue.link.IssueLinkTypeManager
  9. import com.atlassian.jira.util.ErrorCollection
  10. import com.atlassian.jira.util.SimpleErrorCollection
  11. import com.onresolve.jira.groovy.canned.CannedScript
  12. import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
  13. import com.onresolve.jira.groovy.canned.utils.ConditionUtils
  14. class CloneIssue extends CopyIssueWithAttachments implements CannedScript{
  15. String getName() {
  16. return "Clones an issue and links."
  17. }
  18. public String getHelpUrl() {
  19. "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Clonesanissueandlinks"
  20. }
  21. String getDescription() {
  22. return """Clones this issue to another issue, optioninally in another project, and optionally a different issue type.
  23. """
  24. }
  25. List getCategories() {
  26. ["Function", "Listener"]
  27. }
  28. List getParameters(Map params) {
  29. [
  30. ConditionUtils.getConditionParameter(),
  31. [
  32. Name:FIELD_TARGET_PROJECT,
  33. Label:"Target Project",
  34. Type: "list",
  35. Description:"Target project. Leave blank for the same project as the source issue.",
  36. Values: CannedScriptUtils.getProjectOptions(true),
  37. ],
  38. [
  39. Name:FIELD_TARGET_ISSUE_TYPE,
  40. Label:"Target Issue Type",
  41. Type: "list",
  42. Description:"""Target issue type. Leave blank for the same issue type as the source issue.
  43. <br>NOTE: This issue type must be valid for the target project""",
  44. Values: CannedScriptUtils.getAllIssueTypes(true),
  45. ],
  46. getOverridesParam(),
  47. [
  48. Name:FIELD_LINK_TYPE,
  49. Label:'Issue Link Type',
  50. Type: "list",
  51. Description:"What link type to use to create a link to the cloned record.",
  52. Values: CannedScriptUtils.getAllLinkTypes(true)
  53. ],
  54. ]
  55. }
  56. public ErrorCollection doValidate(Map params, boolean forPreview) {
  57. SimpleErrorCollection errorCollection = new SimpleErrorCollection()
  58. if (!params[FIELD_LINK_TYPE]) {
  59. errorCollection.addError(FIELD_LINK_TYPE, "You must provide a link type.")
  60. }
  61. // todo: validation for issue type if set
  62. return errorCollection
  63. }
  64. Map doScript(Map params) {
  65. MutableIssue issue = params['issue'] as MutableIssue
  66. Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
  67. if (! doIt) {
  68. return [:]
  69. }
  70. params = super.doScript (params)
  71. Issue newIssue = params['newIssue'] as Issue
  72. String linkTypeId = params[FIELD_LINK_TYPE] as String
  73. // get the current list of outwards depends on links to get the sequence number
  74. IssueLinkManager linkMgr = ComponentManager.getInstance().getIssueLinkManager()
  75. if (linkTypeId && linkMgr.isLinkingEnabled()) {
  76. IssueLinkTypeManager issueLinkTypeManager = (IssueLinkTypeManager) ComponentManager.getComponentInstanceOfType(IssueLinkTypeManager.class)
  77. IssueLinkType linkType = issueLinkTypeManager.getIssueLinkType(linkTypeId as Long)
  78. if (linkType) {
  79. linkMgr.createIssueLink (issue.id, newIssue.genericValue.id, linkType.id, 0, getUser(params))
  80. }
  81. else {
  82. log.warn ("No link type $linkTypeId found")
  83. }
  84. }
  85. params
  86. }
  87. String getDescription(Map params, boolean forPreview) {
  88. ConstantsManager constantsManager = ComponentManager.getInstance().getConstantsManager()
  89. StringBuffer sb = new StringBuffer()
  90. sb << getName() + "<br>Issue will be cloned to project <b>" + (params[FIELD_TARGET_PROJECT] ?: "same as parent") + "</b>"
  91. sb << "<br>With issue type: <b>" + (params[FIELD_TARGET_ISSUE_TYPE] ? constantsManager.getIssueTypeObject(params[FIELD_TARGET_ISSUE_TYPE] as String)?.name : "same as parent") + "</b>"
  92. sb.toString()
  93. }
  94. public Boolean isFinalParamsPage(Map params) {
  95. true
  96. }
  97. }