Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Aug 14, 2022 • edited

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Aug 18, 2022 • edited

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

Hi, @Ram Kumar Aravindakshan _Adaptavist_ 

unfortunately it is not working anymore. And by now I don't get exactly why.

 

I get the following in the log

 

Exception thrown while executing the script. groovy.lang.MissingPropertyException: No such property: it for class: Script2

 

that refers to the line

def issue = it as MutableIssue

 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 24, 2022

Hi @Mario Kubitzki

Aside from this mail handler configuration, do you have any other mail handlers configured? If yes, could you please try to disable them temporarily and re-run the test?

Also, have you upgraded your ScriptRunner plugin lately? If yes, what is the latest release of ScriptRunner you are currently using?

Could you also please try to uninstall your ScriptRunner and re-installing it once again and see if there is any improvement?

Thank you and Kind regards,

Ram

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

unfortunately there is no other mail handler configured. How ever I saved mine and put a new one in. And it works everything just fine, until I implement the iteration through all issues with 

def issue = it as MutableIssue {

}

We have not upgraded our Plugin. It is still in Version 7.2.0. I will install 7.3.0 soon, hopefully this will resolve the problem.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 24, 2022

Hi @Mario Kubitzki

If you have made any modifications to your code from the original example I have provided, please share it here so I can review it.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events