Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
heh, thanks!
I updated my answer with an example.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Got a chance to test this out, and of course it works brilliantly.
Thank you again for your help.
MJ
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
def
query = jqlQueryParser.parseQuery(
"summary ~ \"${issue.summary}\""
)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What about the reverse? Changing a sub-task to a regular issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.