This “Groovy” trick for Jira allows us to create Jira issues automatically and repeatedly on demand!

For the example, we will edit a Workflow and we will add a new Transition of type Recursive ( from All to Itself). And we name the Transition as “Clone issue and Linked Issues“.

unnamed

(NOTE: Remember after this to publish the Workflow, and turn in Edit mode Again, because this is a well-know bug of Jira… since version 6!! Atlassian team, please… solve this problem!)

Now we can add a Script PostFunction in Groovy (We can use Groovy Amigo or Adaptavist ScriptRunner)

unnamed-1

We can add a Groovy code like this:

unnamed-2

Source code:

import com.atlassian.jira.issue.IssueFactory;
import org.apache.log4j.Category
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink

Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "START Cloning Postfunction"

IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
def linkMgr = ComponentAccessor.getIssueLinkManager()
MutableIssue clonedIssue = issueFactory.cloneIssue(issue);
IssueManager issueManager = ComponentAccessor.getIssueManager();
Issue clonedIssueObject = issueManager.createIssueObject(issue.getReporter(), clonedIssue);
log.debug "Issue " + clonedIssueObject?.getKey() + " created"

List allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
 for (Iterator outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
 IssueLink issueLink = (IssueLink) outIterator.next();
 def linkedIssue = issueLink.getDestinationObject()
 String type = linkedIssue.getIssueType().getName();
 log.debug(linkedIssue.getKey()) 
 //Cloning the tickets    
 MutableIssue clonedLinked = issueFactory.cloneIssue(linkedIssue);    
 Issue clonedLinkedObject = issueManager.createIssueObject(issue.getReporter(), clonedLinked);
 log.debug "Issue " + clonedLinkedObject.getKey() + " created"    
 linkMgr.createIssueLink (clonedLinkedObject.id, clonedIssueObject.id, Long.parseLong("10003"),Long.valueOf(1), issue.getReporter())  //ZZZZZ is the id of the type of link
}


 

Enjoy it! Very useful code for medium-large Organizations! We can easily clone tickets and clone the linked tickets and link again (the new cloned tickets to the new parent cloned ticket)

By MrAddon

Posted by:.

2 replies on “Recurring Tasks in Jira using a Groovy PostFunction

  1. so it will clone an issue all right. not sure I get the ‘recurring’ piece. how often does it recur? what’s in the trigger? manually click a transition?

    Like

    1. Imagine, the issue you want to “clone” each month contains X issues linked and you want to clone them too each month. You can launch this code using a “PostFunction” or using a “Service” to do it programatically (doing a little modification in the code)

      Best regards

      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 )

Facebook photo

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

Connecting to %s