Only Comment on issues that has a summary hash that is the same as the email subject

Summary

This script uses a JQL query to locate all issues that has a summary hash that is the same as the email subject and if any is found then will comment on the first matched issue. This script will also only comment on thread matched issues that has a summary hash that is the same as the subject. If there is no matching issue then a new issue is created.

The below script uses a custom field that stores the hash of the summary when searching for matching issues. If the Custom Field with the same value is not any issues then a new issue will be created.

Implementation

In order for this to function correctly you will need to be using a version newer than JEMH 3.4.11, 4.0.17, 4.1.8 as these versions contain a change that allows the Issue Type Script Directive to be applied correctly.

This script uses a JQL query to check if the related issue or any existing issues have the same subject as the email. If not then it will create a new issue.

Due to issues highlighted on the following page,https://thepluginpeople.atlassian.net/wiki/spaces/JEMH/pages/3974856709/Using+Java+17#Classnotfound-exception-when-using-%E2%80%9Cjava.type%E2%80%9D. It means that if you are using Java 17 with Nashorn installed within jira-install/lib then you will need to use JEMH 4.1.33+ and then use the following instead of lines 7 and 8:

  • var filter = pagerFilter; var builder = jqlQueryBuilder;
var emailSubject = jemhUtils.hash(subject); //Gathers the email subject var foundIssue = ""; //get the user to run the JQL query as var user = userManager.getUserByName("admin"); var filter = Java.type("com.atlassian.jira.web.bean.PagerFilter").getUnlimitedFilter(); var builder = Java.type("com.atlassian.jira.jql.builder.JqlQueryBuilder").newBuilder(); //construct the JQL query to search for issues with the Summary that is the same as the email subject var fullQuery = builder.where().field("Summary_hash").like(emailSubject).buildQuery(); //get list of issues (could be empty) var fullResults = searchService.search(user, fullQuery, filter).getResults(); if (relatedIssue !== null) //Checks whether the related issue is found within the list of these results. If so a comment will be added { if (fullResults.size() > "0") //Checks if the query returned any values, if so then it checks if the related issue is found in query { for(i =0; i < fullResults.size(); i++) //Checks if related issue is found in query results, if not then new issue will create { if(relatedIssue.getKey() == fullResults[i].getKey()) { foundIssue= relatedIssue.getKey(); } else //related issue not found in query result so a comment will be added to the first found issue in the query result { if (fullResults.size()>0) //related issue not found in query, will create new issue { var issue = fullResults[0]; var foundIssue = issue.getKey(); } } } } else //related issue not found in query, will create a new issue using following params { print("no results from JQL query"); resultMap.put("issueKey", ""); resultMap.put("project", "Service Project"); //sets the project that the issue should be created within resultMap.put("issueType", "task"); //sets issuetype that the issue should be } } else //if there is no related issue then it JEMH will comment on the first value found in the JQL query results { if (fullResults.size()>0) { var issue = fullResults[0]; var foundIssue = issue.getKey(); print("Found issue using JQL "+fullQuery+" : "+foundIssue); } else { print("No issues match using JQL: "+fullQuery + " running partial Query"); } } if(foundIssue !== "") //Sets the Issue that it should comment on based on the query result. { print("commenting on: " + foundIssue); resultMap.put("issueKey", foundIssue); } else { print("no issue found, creating a new issue"); resultMap.put("Summary_hash", emailSubject); }