How to test for duplicate summaries when creating issues

rootUncategorized

3333-minToday I had the request to add a bit of business logic to a project configuration.  The customer has a JIRA project which contains 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 summary is not possible, so it has to loop over the results.

 

The resulting code is

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 the create transition (which is hidden behind the first status)
  3. Add a scripted validator
  4. Choose 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?img-1

Check out exalate https://www.exalate.com – Exalate is an advanced issue synchronization solution which supports cross-organization issue tracking.
Its flexibility comes from the built in groovy scripting capability allowing to implement almost any type of integration.