Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Excerpt

This example shows what can be done with a bit more time to develop a script. This example includes use of regular expressions to extract values from the email, and searching with a JQL query to find an existing Jira issue. It results in a behaviour similar to the Regexp Field Processor.

Implementation
Status
colourGreen
titleSINCE 3.4.11, 4.0.17, 4.1.8

Following the release of JEMH 3.4.11, 4.0.17, and 4.1.8 the utils class jemhThreadUtils has been added to the Scripting context and adds some methods that help reduce complexity of Issue Association via Script. The following example script can be used in place of the legacy implementations.

Code Block
// For more detail and background information regarding this Script please see the following Documentation: https://thepluginpeople.atlassian.net/l/cp/CZKAm2Vw
// Create Regexp pattern to extract external id
var pattern = Java.type("java.util.regex.Pattern").compile("Case\\s#:\\s([0-9]+)");
var matcher = pattern.matcher(body);
var foundExternalId = "";

// Set the value of the Custom Field which will hold the External ID value on the Issue
var externalIDField = "customfield_10122";
 
// If able to extract issue key, set variable
if (matcher.find()) {
    var group = matcher.group(1);
    if (group) {
        foundExternalId = group;
    }
}
 
var issueWithExternalId = "";
if (foundExternalId) {
    print("external ID extracted: "+foundExternalId);
    
    // Use Thread Utils to find a list of Issues which hold the 'foundExternalId' in the 'externalIDField'. This method will 
    // apply Thread Match conditions defined in the Profile (only match open issues, matches JQL, etc.). 
    // Additionally, the returned list will be ordered by Creation Date (Ascending) therefore the Issue at index 0 would be the oldest valid Issue
    var results = jemhThreadUtils.getFilteredAssociatedIssuesFromExternalId(externalIDField, foundExternalId);
      
    // Get first issue if exists and add via issueKey directive (Could be empty)
    if (results.size()>0) {
        var issue = results[0];
        issueWithExternalId = issue.getKey();
        print("Found issue using External ID match:"+issueWithExternalId);
    } else {
        print("No issues match found using External ID match");
    }
}
 
if (!issueWithExternalId.isEmpty()) {
        resultMap.put("issueKey",issueWithExternalId)
} else if (!foundExternalId.isEmpty()) {
        resultMap.put(externalIDField, foundExternalId);
}

Legacy Implementation

(info) Since JEMH 3.3.21 we lock down access to arbitrary classes in the script context, they cannot be accessed using historic methods:

Code Block
var pageFilterfilter = PagerFilter.static.getUnlimitedFilter();
var jqlQueryBuilderbuilder = JqlQueryBuilder.static.newBuilder();

but need to be gained through fully specified class names:

Code Block
var pageFilterfilter = Java.type("com.atlassian.jira.web.bean.PagerFilter").getUnlimitedFilter();
var jqlQueryBuilderbuilder = Java.type("com.atlassian.jira.jql.builder.JqlQueryBuilder").newBuilder();

These specific classes were not initially added, are available since JEMH 3.3.38

Info

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 20 and 21:

  • Code Block
    var filter = pagerFilter;
    var builder = jqlQueryBuilder;
Code Block
//create regexp pattern to extract external id
var pattern = Java.type("java.util.regex.Pattern").compile("Case\\s#:\\s([0-9]+)");
var matcher = pattern.matcher(body);
var foundExternalId = "";
 
//if able to extract issue key, set variable
if (matcher.find()) {
    var group = matcher.group(1);
    if (group) {
        foundExternalId = group;
    }
}
 
var issueWithExternalId = "";
if (foundExternalId) {
    print("external ID extracted: "+foundExternalId);
    //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
    var query = builder.where().field("External ID").like(foundExternalId).buildQuery();
      
    //get list of issues (could be empty)
    var results = searchService.search(user, query, filter).getIssues();
      
    //get first issue if exists and add via issueKey directive
    if (results.size()>0) {
        var issue = results[0];
        issueWithExternalId = issue.getKey();
        print("Found issue using JQL "+query+" : "+issueWithExternalId);
    } else {
        print("No issues match using JQL: "+query);
    }
}
 
if (!issueWithExternalId.isEmpty()) {
        resultMap.put("issueKey",issueWithExternalId)
} else if (!foundExternalId.isEmpty()) {
        resultMap.put("External ID",foundExternalId);
}

Jira 8.0.0

Do note that getIssues() method is no longer supported on Jira 8.x.x and will trigger a ScriptException. The corresponding method has been renamed to getResults() and it can be used as follows:

Code Block
//create regexp pattern to extract external id
var pattern = Java.type("java.util.regex.Pattern").compile("Case\\s#:\\s([0-9]+)");
var matcher = pattern.matcher(body);
var foundExternalId = "";
 
//if able to extract issue key, set variable
if (matcher.find()) {
    var group = matcher.group(1);
    if (group) {
        foundExternalId = group;
    }
}
 
var issueWithExternalId = "";
if (foundExternalId) {
    print("external ID extracted: "+foundExternalId);
    //get the user to run the JQL query as
    var user = userManager.getUserByKey("admin");
      
    //note the
use of "static" on the below 2 lines,
see the additional information to the right
    var filter = PagerFilter.staticJava.type("com.atlassian.jira.web.bean.PagerFilter").getUnlimitedFilter();
    var builder = JqlQueryBuilder.staticJava.type("com.atlassian.jira.jql.builder.JqlQueryBuilder").newBuilder();
      
    //construct the JQL query
    var query = builder.where().field("External ID").like(foundExternalId).buildQuery();
      
    //get list of issues (could be empty)
    var results = searchService.search(user, query, filter).getResults();
      
    //get first issue if exists and add via issueKey directive
    if (results.size()>0) {
        var issue = results[0];
        issueWithExternalId = issue.getKey();
        print("Found issue using JQL "+query+" : "+issueWithExternalId);
    } else {
        print("No issues match using JQL: "+query);
    }
}
 
if (!issueWithExternalId.isEmpty()) {
        resultMap.put("issueKey",issueWithExternalId)
} else if (!foundExternalId.isEmpty()) {
        resultMap.put("External ID",foundExternalId);
}

...