/src/main/resources/com/onresolve/jira/groovy/canned/workflow/postfunctions/CloneIssue.groovy
Groovy | 120 lines | 95 code | 23 blank | 2 comment | 6 complexity | d3b04be0e999ebb506e1adbcb603e925 MD5 | raw file
- package com.onresolve.jira.groovy.canned.workflow.postfunctions
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.config.ConstantsManager
- import com.atlassian.jira.issue.Issue
- import com.atlassian.jira.issue.MutableIssue
- import com.atlassian.jira.issue.link.IssueLinkManager
- import com.atlassian.jira.issue.link.IssueLinkType
- import com.atlassian.jira.issue.link.IssueLinkTypeManager
- import com.atlassian.jira.util.ErrorCollection
- import com.atlassian.jira.util.SimpleErrorCollection
- import com.onresolve.jira.groovy.canned.CannedScript
- import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
- import com.onresolve.jira.groovy.canned.utils.ConditionUtils
-
- class CloneIssue extends CopyIssueWithAttachments implements CannedScript{
-
- String getName() {
- return "Clones an issue and links."
- }
-
- public String getHelpUrl() {
- "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Clonesanissueandlinks"
- }
-
- String getDescription() {
- return """Clones this issue to another issue, optioninally in another project, and optionally a different issue type.
- """
- }
-
- List getCategories() {
- ["Function", "Listener"]
- }
-
- List getParameters(Map params) {
- [
- ConditionUtils.getConditionParameter(),
- [
- Name:FIELD_TARGET_PROJECT,
- Label:"Target Project",
- Type: "list",
- Description:"Target project. Leave blank for the same project as the source issue.",
- Values: CannedScriptUtils.getProjectOptions(true),
- ],
- [
- Name:FIELD_TARGET_ISSUE_TYPE,
- Label:"Target Issue Type",
- Type: "list",
- Description:"""Target issue type. Leave blank for the same issue type as the source issue.
- <br>NOTE: This issue type must be valid for the target project""",
- Values: CannedScriptUtils.getAllIssueTypes(true),
- ],
- getOverridesParam(),
- [
- Name:FIELD_LINK_TYPE,
- Label:'Issue Link Type',
- Type: "list",
- Description:"What link type to use to create a link to the cloned record.",
- Values: CannedScriptUtils.getAllLinkTypes(true)
- ],
- ]
- }
-
- public ErrorCollection doValidate(Map params, boolean forPreview) {
- SimpleErrorCollection errorCollection = new SimpleErrorCollection()
- if (!params[FIELD_LINK_TYPE]) {
- errorCollection.addError(FIELD_LINK_TYPE, "You must provide a link type.")
- }
- // todo: validation for issue type if set
- return errorCollection
- }
-
- Map doScript(Map params) {
- MutableIssue issue = params['issue'] as MutableIssue
-
- Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
- if (! doIt) {
- return [:]
- }
-
- params = super.doScript (params)
-
- Issue newIssue = params['newIssue'] as Issue
-
-
- String linkTypeId = params[FIELD_LINK_TYPE] as String
-
- // get the current list of outwards depends on links to get the sequence number
- IssueLinkManager linkMgr = ComponentManager.getInstance().getIssueLinkManager()
-
- if (linkTypeId && linkMgr.isLinkingEnabled()) {
- IssueLinkTypeManager issueLinkTypeManager = (IssueLinkTypeManager) ComponentManager.getComponentInstanceOfType(IssueLinkTypeManager.class)
-
- IssueLinkType linkType = issueLinkTypeManager.getIssueLinkType(linkTypeId as Long)
-
- if (linkType) {
- linkMgr.createIssueLink (issue.id, newIssue.genericValue.id, linkType.id, 0, getUser(params))
- }
- else {
- log.warn ("No link type $linkTypeId found")
- }
- }
-
- params
- }
-
-
- String getDescription(Map params, boolean forPreview) {
- ConstantsManager constantsManager = ComponentManager.getInstance().getConstantsManager()
-
- StringBuffer sb = new StringBuffer()
- sb << getName() + "<br>Issue will be cloned to project <b>" + (params[FIELD_TARGET_PROJECT] ?: "same as parent") + "</b>"
- 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>"
- sb.toString()
- }
-
- public Boolean isFinalParamsPage(Map params) {
- true
- }
- }