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.

MJ

1 answer

1 accepted

This widget could not be displayed.

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);
log.setLevel(Level.DEBUG)

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 = searchService.search(componentManager.getJiraAuthenticationContext().getUser(), query, PagerFilter.getUnlimitedFilter())
def issueManager = ComponentAccessor.getIssueManager()

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

    if (searchIssue.summary == issue.summary) {
        // DO SOMETHING WITH THE RETURNED ISSUE HERE
    }
}

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.

MJ

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 Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

533 views 3 19
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