Testing for Duplicate Summaries when Creating JIRA Issues with ScriptRunner

I once had the request to add a bit of business logic to a project configuration.  The customer had a JIRA project which contained parts (a bit like a CMDB) and he wanted to avoid duplicate entries.

I decided to write a simple scripted validator (using the Scriptrunner).  The code had to take into account that an exact search on the summary is not possible, so it has to loop over the results.

The resulting code was:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
 
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class)
ApplicationUser adminUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
  
#
# Search the current project for any issue which have a similar summary
#
ParseResult parseResult = searchService.parseQuery(adminUser,
                                                   "project = '" + issue.projectObject.key + "' " +
                                                    "and summary ~ '" + issue.summary +"'")
 
SearchResults queryResult
 
#
# Execute the query - taking into account that things can fail
#
if (parseResult.isValid()) {
    try
    {
        queryResult = searchService.search(adminUser,parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
        if (queryResult == null) {
            log.debug("search - results is null - which should not happen");
            return false
        }
    }
    catch (SearchException e)
    {
        log.error("Error running search", e)
    }
} else {
    log.debug ("Some problem trying to search for duplicates ")
    return false
}
 
 
#
# Test the resulting array of issues if there is an issue with an identical summary
#
return ! (queryResult.issues.find { it.summary == issue.summary })

Install the script:

  1. Access your workflow
  2. Access to create transition (which is hidden behind the first status)
  3. Add a scripted validator
  4. Choose a simple scripted validator
  5. Paste the code above
  6. Add an error message such ‘No bro – duplicates are not allowed – check the summary as it exists already’
  7. Set the field to summary

 

Are you still using multiple-issue trackers?

Check out exalate – It’s an advanced issue synchronization solution that supports cross-organization issue tracking.
Its flexibility comes from the built-in groovy scripting capability, allowing it to implement almost any type of integration.

We hope this use case, followed by the solution, helped you. If you need more information, get in touch!

Outline

Subscribe to our newsletter to receive monday.com insights & events

    Related Articles