Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,005
Community Members
 
Community Events
168
Community Groups

How to update an Issue by finding it with the summary

Hi,

"Problem Description":

We get Maintenance Mails from our Service Provider. Quite many. All Mails start with a Ticket Number from our Service Provide "CS1234567". We get a Mail for starting maintenance and when there are problems or when it is resolved.

Right now my Mail Handler creates everytime a new Ticket with the eMail Subject as Summary. eg: 

Internal Ticket (issue key) DC-87 

Summary "CS1561207 - DE.FRA6 - Communication - Maintenance"

So now I get a new email with the same CS# and I want to search for the Issues in the project with that number.

I got the "function" for creating a substring out of that email subject and I saved it as a variable

def subjectMatch = "CS\\d*"

def subject = message.subject as String

def substringCS = (subject =~ subjectMatch)
but I do not know how to search now for that string in the summaries.
I tried the solution from the adaptavist library which should compary the subject of a new email with the current issues. However I think it searches for the ID "DC-87"
def issue = ServiceUtils.findIssueObjectInString(subject)

if (issue) {

// issue already exists so do nothing

return

}

But the value of "issue" is always 'null'

As I saw in the Jira API documenation this one is depreciated by now, however it is still an example in the library. 

Unfortunately it is not possible for me to use the Jira Ticket Number for searching, since these emails come directly from the maintenance system of our supplier. 

Does anybody know how I can use the string "subjectstringCS[0]" for a search in the summary of the issues?

1 answer

1 accepted

1 vote
Answer accepted

Hi @Mario Kubitzki

For your requirement, you could try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.mail.MailUtils

def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def commentManager = ComponentAccessor.commentManager

def subject = message.subject as String
def messageBody = MailUtils.getBody(message)

def project = projectManager.getProjectByCurrentKey('MOCK')
def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))
def subjectKey = subject.substring(0, subject.indexOf('-')).trim() // To extract the key from the email's subject

issues.each {
def issue = it as MutableIssue
if (issue.summary.contains(subjectKey)) {
commentManager.create(issue, loggedInUser, messageBody, false) // adding a new comment with message body
}
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the mail handler configuration:-

mail_handler_config.png

What this mail handler does is that it iterates through all the issues in the project and checks the issue summary, which contains the key specified email subject. 

If the match is found, it will copy the body of the email and add it to the comment.

Below is the sample email body that I am testing with:-

Subject: CS12345 - Subject Updated
From: Sample User 1 <sample@mail.com>
To: Sample User 2 <sample2@mail.com>
Content-Type: multipart/alternative; boundary="0000000000007080b105895f1bb3"

--0000000000007080b105895f1bb3
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hello World

--0000000000007080b105895f1bb3--

If you observe the email template above, the subject contains the key CS12345.

Below are a few test screens:-

1) Below is a screenshot of an existing issue. The issue's summary contains the same value as the subject displayed in the email above.

image1.png

2) When the Mail Handler is triggered, it checks through the project for the existing issues. If it can find an issue which has a summary that contains the value CS12345, it will update the issue and add a comment to it as shown below:-

image2.png

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

Hi Ram,

thank you very much. 

That helped me so much! 

issues.each {
def issue = it as MutableIssue
if (issue.summary.contains(subjectKey)) {
commentManager.create(issue, loggedInUser, messageBody, false) // adding a new comment with message body
}

Especially the "issue.summary.contains()" was perfect! Now I can also check with "issue.description.contains()" for additional keys. Perfect!

 

Now I just have to find out, how I can also make the transition to "Closed" and the Resolution to "Resolved" if the ticket is resolved.

The beginning is the

if (issue.description.contains("Resolved") {
//some Transition stuff.

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

again thank you for your help!

I found already a solution to change the status and set the resolution (I have to set the resolution through Jira Automation, since

issue.setResolution(resolutionManager.getResolutionByName("Done"))

is not working. I don't know why, but it isn't. But it works with the automation.

 

I have a question about your solution. 

def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

With that I get all Issues in my Project. Is possible that I only get all the issues with status "Open" of a certain issue type?

In my case the Project is called "DC", the issue Type is "DC-Maintenance"

I don't know how the script will perform when it goes through all issues of the project since there will be a few thousands tickets after a year.

 

Thanks

Hi @Mario Kubitzki

If you want your resolution update to take effect, i.e. 

issue.setResolution(resolutionManager.getResolutionByName("Done"))

will not work.

You will need to use the Workflow Manager and Issue Manager to validate and update the transition, as shown in this example available in the Adaptavist Library.

Otherwise, your change to the issue will not take effect.

Also, if you only want to check for open issues, you will need to include an additional if condition to do the filter, i.e.

if(issue.status.name == 'Open') {
.....
}

So if you want the issues to be filtered and transitioned accordingly, your will need to update your code to something like:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.mail.MailUtils

def projectManager = ComponentAccessor.projectManager
def issueService = ComponentAccessor.issueService
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def commentManager = ComponentAccessor.commentManager
def workflowManager = ComponentAccessor.workflowManager

def issueInputParameters = issueService.newIssueInputParameters()

final actionName = 'Done'

def subject = message.subject as String
def messageBody = MailUtils.getBody(message)

def project = projectManager.getProjectByCurrentKey('MOCK')
def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))
def subjectKey = subject.substring(0, subject.indexOf('-')).trim() // To extract the key from the email's subject

issues.each {
def issue = it as MutableIssue

if(issue.status.name == 'In Progress') { // update the status name accordginly

if (issue.summary.contains(subjectKey)) {
commentManager.create(issue, loggedInUser, messageBody, false) // adding a new comment with message body
}

if (issue.description.contains('Resolved') {

def workflowActionId = workflowManager.getWorkflow(issue).allActions.findByName(actionName)?.id

def transitionValidationResult = issueService.validateTransition(loggedInUser, issue.id, workflowActionId, issueInputParameters)
assert transitionValidationResult.valid: transitionValidationResult.errorCollection

def transitionResult = issueService.transition(loggedInUser, transitionValidationResult)
assert transitionResult.valid: transitionResult.errorCollection

issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

I have tested this in my environment, and it can filter the issues and transition them accordingly.

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events