$customHeader
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »


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.
(info) Since JEMH 3.3.21 we lock down access to arbitrary classes in the script context, they cannot be accessed using historic methods:

var pageFilter = PagerFilter.static.getUnlimitedFilter();
var jqlQueryBuilder = JqlQueryBuilder.static.newBuilder();

but need to be gained through fully specified class names:

var pageFilter = Java.type("com.atlassian.jira.web.bean.PagerFilter")
var jqlQueryBuilder = Java.type("com.atlassian.jira.jql.builder.JqlQueryBuilder")

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

//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:

//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");
      
    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).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);
}

Things to change when using this script:

  • The user object has to be modified to ensure the extraction of the corresponding user who'll be executing this query is successful. The user object is obtained via userManager which in this script gets the admin user (username). Therefore, you need to ensure you are getting the user which exists in your Jira instance. 

  • When building the query object, ensure you are using the relevant custom field type to carry this query out. The custom field type for this query is a Text Field (Single Line). If you have a different custom field type, you'll need to update your query accordingly. 

  • Don't forget to update the field name when building the query object. In this example, we are using the field name but you can also use the field id (customfield_10400). If you have custom fields with duplicate names, you should use field id instead of the name to ensure the JQL query is valid

Refer to following classes for more information:

  • No labels