Extract a custom Field value from a related closed issue to add on new issue

Scenario

You have Threat Match Condition set to Open Issue Only and this is resulting in JEMH creating a new issue instead of commenting on the related Issue but you would like to extract a field from the related issue to then be added to the new issue.

Implementation

This script will find and gather a Custom Field value from the related issue and will then add the value from the Custom Field to the new issue that is being created.

Note: The method 'getReferencedMessageIds()' was added in JEMH 3.4.11, 4.0.17, 4.1.8, and above. If using an earlier version you will need to extract the header using 'message.getHeader("References")' which returns a String array.

if (relatedIssue === null) //Checks if the email is commenting on a issue. { var allReferences = jemhUtils.getReferencedMessageIds(); //Gathers all of the References from the email if(allReferences !== null && !allReferences.isEmpty()){ //Checks if the returned reference headers are null or empty var referencesString = allReferences.toString(); //Converts all reference Headers to a single string var pattern = Java.type("java.util.regex.Pattern").compile("JIRA\.([0-9]+)\."); //Compiles a Regexp Pattern to extract only the IssueId from the Reference-Id var matcher = pattern.matcher(referencesString); if(matcher.find()){ //Runs if a match is found var group = matcher.group(1); //Gathers the first group Regexp match. In this case the Issue ID var idAsLong = textUtils.parseLong(group); //Converts the Issue ID String into a number of type 'Long' if(issueManager.getIssue(idAsLong) !== null){ //Checks if an issue with the ID present in the reference header is valid var matchedIssue = issueManager.getIssueObject(idAsLong); //Retrieves the issue using the Issue ID var fieldObject = customFieldManager.getCustomFieldObjectByName("example"); //Gets the Custom Field Object for the relevant Custom Field, "example" needs to be changed if(fieldObject !== null){ //Checks if the Custom Field is valid var fieldValue = matchedIssue.getCustomFieldValue(fieldObject); //Retrieves the value of the Custom Field present on the Matched Issue if (fieldValue !== null){ // Checks if there is a value set, If there is then add this value to new issue print("value was found on the related issue: "+ matchedIssue); resultMap.put("example", fieldValue); //Sets value on the new issue, "example" needs to be changed to the field name } } } } } }