Today 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
- Access your workflow
- Access the create transition (which is hidden behind the first status)
- Add a scripted validator
- Choose simple scripted validator
- Paste the code above
- Add an error message such ‘No bro – duplicates are not allowed – check the summary as it exists already’
- Set the field to summary
Are you still using multiple issue trackers?
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.