Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set a Reporter based on an email in Description using regular expression

Martin Benavides July 7, 2022

Appreciating any help/guidance:

 

In a Jira Service Management project (Data Center), we have a third party app that creates tickets in this project via email. The Jira ticket Reporter is setting a generical email from this app and then, we need to change it manually to the right user(email included in the Jira ticket Description when created). 

The right output would be to set the right Reporter automatically when the ticket is created(instead of the generical app email).

The idea is to use Scriptrunner, with a Post function in the Create transition to set the right user with this kind of script:

import com.atlassian.jira.component.ComponentAccessor

// the username of the reporter

final String userName = "user_name" def user = ComponentAccessor.userManager.getUserByName(userName) issue.setReporter(user)

But first, I need to get the email from the Description field, using a regular expression. I use reggex101 to figure it out:

https://regex101.com/r/7dYFI3/1

 

"[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}"i

This regular expression is getting the email, and now I don´t know how to assign it to the username variable.

Since I don´t have the expertise to develop this script with scriptrunner, would be very appreciate it if you guys could help me to achieve this :)

Any other workaround is welcome. Cheers

 

2 answers

0 votes
Deepak Rai
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 22, 2024
0 votes
Craig Nodwell
Community Champion
July 8, 2022

Hi @Martin Benavides set the reporterUser to the value from your regex
def reporterUser= "Value of Regex"
then set up your issueObject 

def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == defaultIssueType }.id)
issueObject.setReporter(reporterUser)
try {
(messageHandlerContext as MessageHandlerContext).createIssue(user, issueObject)
} catch (CreateException e) {
log.error('Error creating issue: ', e)
}

Martin Benavides July 8, 2022

Thank you for your help @Craig Nodwell and apologize for my lack of experience (working on it).

I just set the value of the regex but scriptrunner is not accepting it:

 

def reporterUser= "[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}"
def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == defaultIssueType }.id)
issueObject.setReporter(reporterUser)
try {
(messageHandlerContext as MessageHandlerContext).createIssue(user, issueObject)
} catch (CreateException e) {
log.error('Error creating issue: ', e)
}
The script could not be compiled:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script2557.groovy: 1: unexpected char: '\' @ line 1, column 27.
   def reporterUser= "[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}"
(I´m trying to set it in the Create transition, as a Post function).
Craig Nodwell
Community Champion
July 10, 2022

I thought you were further into your script.
First off you need to get that email out of the Description Field.
Then you need to do a replaceAll on the string with your regex.
I'll have a further look for you tomorrow.

Martin Benavides July 11, 2022

I wish I had it :(
The Regex provided above, will take the email out of a paragraph, but yeah, I don´t know how to develop the script to use this regex, take the email out of the description and then set it in the reporter. 

Thank you for your help, I really appreciate it.

Like Craig Nodwell likes this
Craig Nodwell
Community Champion
July 11, 2022

I'll need a few hours maybe even tomorrow I have work to do this afternoon standing up a new delivery pipeline tool stack.
Here's some links if you want to get started on your own.
Scriptrunner Library 
Scriptrunner Tutorials

Like Martin Benavides likes this
Martin Benavides July 11, 2022

Awesome! yes I will start from there

craig.nodwell
Contributor
July 11, 2022

@Martin Benavides here's another one specifically about email handlers.
Mail Handler
Create From Email

Sorry for the account switch I'm at work now.

Martin Benavides July 11, 2022

This option is very helpful, actually this Service Management project is running with a Mail Server that creates the tickets in the project. 

The issue here is that, in this use case, we are receiving Feedback from a third party app called Appzi. Once this feedback is sent to the email set in the Mail Server, the ticket is created and the Reporter is a generic email "info@appzi.io" . 

That is the reason I would like to develop a script to set the right Reporter automatically which is contained in the feedback description (which is the Description in the ticket) as an email (which would be the right Reporter, every time might be a different email). The regex provided is able to get this email from a paragraph, but from there, that is the challenge :).

I will have a look on those documents to see if somehow I could achieve that, instead of a custom script, but I think this last one is the only option.

Craig Nodwell
Community Champion
July 11, 2022

So here's one for you sorry still busy but based on your information about Service Management.
Email Scriptrunner Jira Service Management'

This is only an example however, like you don't need to create the attachement.
What you need to focus on is getting the email body parsing it and setting the reporter to that value instead of grabbing the sender information.

Martin Benavides July 22, 2022

Hi @Craig Nodwell 

Sorry again to bother you. I haven´t find how to develop this script yet. 

I tried in Cloud with automation and it works:

1..jpg

This automation takes the email from description, sets it in a custom field called Email Address, then it creates a new customer if needed and then it sets the Reporter with a smart value {{customer.accountId}}

 

So I would like to replicate this process in Jira Data Center with Scriptrunner but honestly I dont know if the same approach is needed. 
This is what I have so far but I think it doesn´t make much sense. Could you please give me a north if this is the way or something else different is needed?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueInputParametersImpl

def description = issue.description
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueManager = ComponentAccessor.issueManager
def reporterUser = "[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}"i
def issueObject = issueFactory.issue

issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == defaultIssueType }.id)
issueObject.setReporter(reporterUser)

{

if (description.toLowerCase().contains(reporterUser)) {
    issue.setReporter(issueType(reporterUser)
    issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

    issueObject.setProjectObject(project)
    issueObject.setSummary(subject)
    issueObject.setDescription(MailUtils.getBody(message))
    issueObject.setIssueTypeId(project.issueTypes.find { it.name == defaultIssueType }.id)
    issueObject.setReporter(reporterUser)
}
try {
(messageHandlerContext as MessageHandlerContext).createIssue(user, issueObject)
} catch (CreateException e) {
log.error('Error creating issue: ', e)
}
}
Thanks again!
Like Craig Nodwell likes this
Craig Nodwell
Community Champion
July 25, 2022

Will review this for you in a little bit.  I seen another post very similar, from this afternoon.

Suggest an answer

Log in or Sign up to answer