package example.CloneTicketAndLink

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.atlassian.crowd.embedded.api.User;
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.IssueFactory;
 
class TaskVersionListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(TaskVersionListener.class)

    @Override
    void workflowEvent(IssueEvent event) {
		log.setLevel(org.apache.log4j.Level.DEBUG)
		def issue = event.getIssue()
		if ( issue.getIssueTypeId() == "XXXXX" ) {  // The Issue Type to catch in the Listener
			def projectName = "YYYYY" //Target project name
 
			def issueMgr = ComponentManager.getInstance().getIssueManager()
			def projectMgr = ComponentManager.getInstance().getProjectManager()
			String currentUser = event.getUser().getName();
			User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
			ComponentManager componentManager=ComponentManager.getInstance()
			CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
			log.debug "IF ISSUE IS TYPE XXXXX CLONE ISSUE TO PROJECT YYYYY WITH SAME FIELDS, ETC"

			def issueFactory = ComponentManager.getInstance().getIssueFactory()
			def newissue = issueFactory.getIssue()
			newissue.setSummary (issue.summary)
			newissue.setProject (projectMgr.getProjectByName(projectName))
			newissue.setIssueTypeId("JJJJ") //The ID of the target Issue Type
			newissue.description = issue.description
			newissue.reporter = issue.getReporter()
			
			//Set the Project Lead of the target project as assignee of the new ticket
			def project = projectMgr.getProjectObjByName(projectName)
			newissue.setAssigneeId(project.getLeadUserKey())
			
			//Copy the fields that you want
			def cf = null;
			//ZZZZZ (list)
			cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'ZZZZZ'}
			newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
			//YYYYYY (date) another way to obtain the custom field
			cf = customFieldManager.getCustomFieldObject("customfield_NNNNN");
			newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
			//XXXXXX (text, numeric, ...)
			cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'XXXXX'}
			newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
			
 
			def params = ["issue":newissue]
			def subTask = issueMgr.createIssue(currentUserObj, params)
			
 
			def linkMgr = ComponentManager.getInstance().getIssueLinkManager()
			linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong("ZZZZZ"),Long.valueOf(1), currentUserObj)  //ZZZZZ is the id of the type of link
		}
	}
}
      

Advertisements
Posted by:.

8 replies on “New JIRA Listener example: Copy ticket & link

      1. Thanks!
        I have compatible according to supported API/code
        Error has vanished
        But, issue clone is not working also link is not appearing is screen

        Regards,
        Sid

        Liked by 1 person

  1. Sure… Following is the updated code
    Note: I am taking Project Name from my custom field “Project(Type-Project Picker)”

    import com.atlassian.jira.event.issue.AbstractIssueEventListener
    import com.atlassian.jira.event.issue.IssueEvent
    import com.atlassian.jira.user.util.UserUtil
    import com.atlassian.jira.ComponentManager
    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.link.IssueLink
    import com.atlassian.jira.util.ImportUtils
    import com.atlassian.crowd.embedded.api.User;
    import com.opensymphony.workflow.WorkflowContext
    import org.apache.log4j.Category
    import com.atlassian.jira.issue.customfields.CustomFieldType
    import com.atlassian.jira.issue.CustomFieldManager
    import com.atlassian.jira.issue.fields.CustomField
    import com.atlassian.jira.issue.IssueManager
    import com.atlassian.jira.project.ProjectManager
    import com.atlassian.jira.project.Project
    import com.atlassian.jira.issue.IssueFactory;

    class TaskVersionListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(TaskVersionListener.class)

    @Override
    void workflowEvent(IssueEvent event) {

    log.setLevel(org.apache.log4j.Level.DEBUG)
    def issue = event.getIssue()

    def cust_field=ComponentAccessor.getCustomFieldManager();
    String sid2= cust_field.getCustomFieldObjectByName(“Project”)

    log.error”————-sid2————“+sid2
    if ( issue.getIssueTypeId() == “10000” ) { // The Issue Type to catch in the Listener
    def projectName = sid2 //Target project name
    log.error”————-Project Name————“+projectName

    def issueMgr = ComponentAccessor.getIssueManager()
    def projectMgr = ComponentAccessor.getProjectManager()
    String currentUser = event.getUser().getName();
    def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
    ComponentManager componentManager=ComponentManager.getInstance()
    CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
    log.debug “IF ISSUE IS TYPE XXXXX CLONE ISSUE TO PROJECT YYYYY WITH SAME FIELDS, ETC”

    def issueFactory = ComponentAccessor.getIssueFactory()
    def newissue = issueFactory.getIssue()
    newissue.setSummary (issue.summary)
    newissue.setProjectObject (projectMgr.getProjectObjByName(projectName))
    newissue.setIssueTypeId(“1000”) //The ID of the target Issue Type
    newissue.description = issue.description
    newissue.reporter = issue.getReporter()

    //Set the Project Lead of the target project as assignee of the new ticket
    def project = projectMgr.getProjectObjByName(projectName)
    newissue.setAssigneeId(project.getLeadUserKey())

    //Copy the fields that you want
    def cf = null;
    //ZZZZZ (list)
    cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == ‘ZZZZZ’}
    newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
    //YYYYYY (date) another way to obtain the custom field
    cf = customFieldManager.getCustomFieldObject(“customfield_NNNNN”);
    newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
    //XXXXXX (text, numeric, …)
    cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == ‘XXXXX’}
    newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))

    def params = [“issue”:newissue]
    def subTask = issueMgr.createIssueObject(currentUserObj, newissue)

    def linkMgr = ComponentAccessor.getIssueLinkManager()
    linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong(“ZZZZZ”),Long.valueOf(1), currentUserObj) //ZZZZZ is the id of the type of link
    }
    }
    }

    Like

  2. Hi,

    I have tested the above code again, and it is creating Clone of a ticket in other projects.

    But “Links” are not coming. Can you help me on that?
    Regards,
    Sid

    Like

      1. Take this code (it’s other similar but for Jira 7)

        Remember: you must create a folder called “example” in your “scripts” folder
        Remember: you must place the file inside the “example” folder with name CloneAndLink.groovy
        Remember: you must change/replace the “ID”‘s for your Jira ID’s. i.e. Type of Issuelink, etc.

        package example.CloneAndLink
        
        import com.atlassian.jira.event.issue.AbstractIssueEventListener
        import com.atlassian.jira.event.issue.IssueEvent
        import com.atlassian.jira.user.util.UserUtil
        import com.atlassian.jira.ComponentManager
        import com.atlassian.jira.component.ComponentAccessor
        import com.atlassian.jira.issue.link.IssueLink
        import com.atlassian.jira.util.ImportUtils
        import com.opensymphony.workflow.WorkflowContext
        import org.apache.log4j.Category
        import com.atlassian.jira.issue.customfields.CustomFieldType
        import com.atlassian.jira.issue.CustomFieldManager
        import com.atlassian.jira.issue.fields.CustomField
        import com.atlassian.jira.issue.IssueManager
        import com.atlassian.jira.project.ProjectManager
        import com.atlassian.jira.project.Project
        import com.atlassian.jira.issue.IssueFactory;
        import com.atlassian.jira.ComponentManager
        import com.atlassian.jira.project.ProjectManager
        import com.atlassian.jira.issue.IssueManager
        import org.ofbiz.core.entity.GenericValue
        import com.atlassian.jira.issue.watchers.WatcherManager
        import java.sql.Timestamp
        
         
        class TaskVersionListener extends AbstractIssueEventListener {
            Category log = Category.getInstance(TaskVersionListener.class)
        
            @Override
            void workflowEvent(IssueEvent event) {
        		log.setLevel(org.apache.log4j.Level.DEBUG)
        		def issue = event.getIssue()
        		if ( issue.getIssueTypeId() == "11003" ) { //IssueType ID to trigger the "clone ticket and link"
        			def projectKey = "XXX"       //Target project KEY
         
        			def issueMgr = ComponentAccessor.getIssueManager()
        			def projectMgr = ComponentAccessor.getProjectManager()
         
                    //We get the user caller or we set a user with permissions to do the clone action
        			//String currentUser = event.getUser().getName();
        			def userManager = ComponentAccessor.getUserManager()
        			def currentUserObj = userManager.getUserByName("admin") //Admin username
        			CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
        			log.debug "CLONE ISSUE XXX"
        
        			def issueFactory = ComponentAccessor.getIssueFactory()
        			def newissue = issueFactory.getIssue()
        			newissue.setSummary (issue.summary)
        			newissue.setProjectObject (projectMgr.getProjectByCurrentKey(projectKey))
        			newissue.setIssueTypeId("66") //Destination IssueType Id
        			newissue.description = issue.description
        			newissue.reporter = issue.getReporter()
        			
        			def project = projectMgr.getProjectByCurrentKey(projectKey)
        			newissue.setAssigneeId(project.getLeadUserKey())
        			
                    //-------------------------------------------
                    def cf = null;
        			//Example field (list)
        			cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Example field'}
        			newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
        			
        			def duedate = issue.dueDate;
        			newissue.setDueDate(duedate);
        			
        			//Example textfield (text)
        			cf = customFieldManager.getCustomFieldObject("customfield_20703");
        			newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
        			//----------------------------------------------
                    
                    
                    def params = ["issue":newissue]
        			
        			def subTask = issueMgr.createIssueObject(currentUserObj, newissue)
        			def linkMgr = ComponentAccessor.getIssueLinkManager()
        			linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong("10000"),Long.valueOf(1), currentUserObj)  //Change 10000 to the ID of the Link type
        			
        			//The watchers must be added once the issue is SAVED (created), we add the projectLead as watcher
        			project =  projectMgr.getProjectByCurrentKey(projectKey)
        			WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
        			watcherManager.startWatching(project.getProjectLead(), newissue)
        		}
        	}
        }
        

        Let me know if it works 😉

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s