Convert issue to subtask via post function

I would like to create a Groovy post function that executes a JQL query to find if an issue already exists with the same summary, and if so the current issue would be converted to a subtask of the issue returned via the query. I have searched the forums here and haven't found the answer, so I figured I'd throw this question out there.


1 answer

1 accepted

It's a very specific requirement so you're unlikely to find something that works for you.

You need to break this question down into pieces. Running a JQL query in a post-function is easy. However converting an issue to a sub-task, moreover the current issue, is not straightforward.

It's the same problem as trying to move an issue programatically. There are other questions on this topic, which don't have good answers.

There is an example for doing a search in a post-function below. Note that "issue" is provided in the script binding, so you don't need to declare it.

Your problem is that Lucene will do a fuzzy match when you query summary, so if you're looking for exact matches you need to do a further compare on the results (you're probably not actually, but the query will match more than you expect..)

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level

Logger log = Logger.getLogger(this.class);

ComponentManager componentManager = ComponentManager.getInstance()

def jqlQueryParser = ComponentManager.getComponentInstanceOfType(JqlQueryParser.class) as JqlQueryParser
def query = jqlQueryParser.parseQuery("summary ~ ${issue.summary}")
def searchService = componentManager.getSearchService()
def results =, query, PagerFilter.getUnlimitedFilter())
def issueManager = ComponentAccessor.getIssueManager()

results.getIssues().each {Issue i ->
    def searchIssue = issueManager.getIssueObject(

    if (searchIssue.summary == issue.summary) {

I figured if anybody could help with this it would be you :)

I suppose the most important part of the question is the JQL query via Groovy script. I've found some answers but haven't been able to get them to work well. Unfortunatly I'm away from my work machine so I don't have the code that I've tried that didn't work.

Care to give an example? Ideally this will be a stored groovy script that will fire in a post function, and maybe the issue I was having was with the imports.

Anyhow, thanks for your help with this and everything else you've done with the community.

heh, thanks!

I updated my answer with an example.

Got a chance to test this out, and of course it works brilliantly.

Thank you again for your help.


Great. It probably needed a little fixing up as I didn't test it, if anyone else uses it you probably will need some quotes in the query, eg

defquery = jqlQueryParser.parseQuery("summary ~ \"${issue.summary}\"")

Good point. I actually tested with a statically defined query instead of checking issue.summary, but for my purposes that may be what I need to solve for anyhow.

What about the reverse? Changing a sub-task to a regular issue?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 13, 2019 in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


236 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you