Script Pre Processing Task to Prevent Mail from a Static Jira System Address from Adding Comments to Existing Issues

 

Introduction

If Jira users that are external to your Jira instance, e.g., customers, can add comments to your issues, then one solution is to create a Pre Processing Task script. The script identifies the sender of the issue by their email address and will enclose the issue key with curly brackets , for example, {DAYS-10}.

Note: By implementing the following instructions, external users who send emails with subjects that match your issue key will, instead, create new issues in your local Jira rather than adding comments to your existing issues.

Script Pre Processing Task

A Script is one of 4 methods you can use to perform a pre-processing task, and to perform a pre-processing task you must enable Auditing and you must select the pre-processing task you wish to use. For more information, follow: .

 Instructions

Step by step guide:

  1. Go to Profiles > Configure > Email > Pre-processing.

  2. Scroll down until you find “Subject IssueKey (comment) Regexps”, add the Regex below and scroll down and click save.

\s([A-Z][A-Z_0-9]+-[0-9]+)
  1. Next, Go to Profiles > Configure > Summary. Then click “Email” and, under pre-processing, click the pencil beside “Script Task” to open the Pre Processing Task Properties pop up (see below).

 

 

  1. In the “Matching Headers” field, add: “from,subject” (make sure not to include the quotation marks).

  2. Copy and add the following script in the “Header Scipt” field:

    /Step 1: Define the variable "subject" in the script from the header/ var subject = headerBeans.get('subject').getOriginalVal(); /Step 2: validate the script has the information required, e.g, subject and fromAddress, to change the issue key/ if(subject != null && fromAddress != null && fromAddress.getAddress() != null){ print("Found information required to change the issue key"); /Step 3: identifies whether the source from the specified domain (in this case test.com)/ /*NOTE: make sure to change "test.com" to the name of the domain you wish to block, e.g., gmail.com */ if(fromAddress.getAddress().indexOf("test.com") != -1){ print("address is from blocked domain"); /Step 4: find the issue key and enclose with curly brackets: "{issue key in here}"/ var pattern = "\\b[A-Z][A-Z_0-9]+-[0-9]+\\b"; /script does not like the \b \b/ var targetPattern = Java.type("java.util.regex.Pattern").compile(pattern); var matcher = targetPattern.matcher(subject); if(matcher.find()) { var baseMatch = matcher.group(); var replacement = "{" + baseMatch + "}"; var replaced = subject.replace(baseMatch, replacement); headerBeans.get('subject').setUpdatedVal(replaced); } } } else { print("can not find the information required to change the issue key"); }
  3. Now click submit in the bottom right corner of the “Pre Processing Task Properties” pop up.

Testing

Testing requires Test Cases. So if you are not familiar with test cases, then check out the documentation: .

The rest of the testing section will highlight the impact of the pre-processing script by comparing a test case from the domain “test.com”, which is the domain that the script is ‘blocking’, and a test case that is not from the ‘blocked’ domain.

Note: Above is the two test cases I will use to test the script and, importantly, the addresses listed by “From” are different: <andy@test.com> is the ‘blocked’ domain and <andy@localhost> is not 'blocked'.

Results

Comment is added when from address does not contain “test.com”.

This test case demonstrates what happens when the from address is not from the domain specified in the script, therefore a comment on an issue is made. Notice the issue key in the subject is not enclosed by curly brackets.

 

Comment is not added when from address contains “test.com”, and a new issue is created instead.

This test case demonstrates what happens when the from address is from the domain specified in the script, therefore a comment on is not made and a new issue is created instead. Notice the issue key in the subject is enclosed by curly brackets.