Convert issue to subtask via post function

Matt Johnson November 6, 2012

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

0 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2012

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
    }
}

Matt Johnson November 6, 2012

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.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2012

heh, thanks!

I updated my answer with an example.

Matt Johnson November 7, 2012

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

Thank you again for your help.

MJ

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 7, 2012

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}\"")

Matt Johnson November 7, 2012

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.

Justin Leader
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 5, 2014

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events