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

ScriptRunner - Script

Asaf Yosovich May 8, 2024

Hello

 

I would like to create a script with ScriptRunner, that will send the jira issue direct URL to the assignee email or to a specific email address/


Thanks

2 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Dave Rosenlund _Trundl_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 8, 2024

Hi, @Asaf Yosovich. I found this 'Solved' question from another community member that I think may contain an example you can work with. 

Check out the answer from Ivan Tovbin

Use Scriptrunner to send one email collating all issues assigned to user

Good luck,

-dave

0 votes
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 8, 2024

To construct an URL in a groovy script, it's helpful to fetch the "BaseUrl" property for your instance.

This way you don't have to hard-code the URL and the script will work even if you migrate to a different environment.

 
import com.atlassian.jira.component.ComponentAccessor
def baseUrl = ComponentAccessor.applicationProperties.jiraBaseUrl

Depending on the context where you want this script to execute, I'm assuming you'll already have access to the "issue" object.

So you can construct the URL for the link back to the issue:

def issueUrl = baseUrl + '/browse/' + issue.key

 

To send an email, I generally use the SingleMailQueItem object and MailQueue component.

Something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.mail.Email
import com.atlassian.mail.MailException
import com.atlassian.mail.MailFactory
import com.atlassian.mail.queue.SingleMailQueueItem
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer

MailServerManager mailServerManager = ComponentAccessor.mailServerManager
SMTPMailServer mailServer = mailServerManager.defaultSMTPMailServer

Issue issue //assumes this is already provided by the script context
def baseUrl = ComponentAccessor.applicationProperties.jiraBaseUrl
def issueUrl = baseUrl + '/browse/' + issue.key

if (mailServer && !MailFactory.settings.sendingDisabled) {
Email email = new Email('recipientEmail')

email.from = mailServer.defaultFrom
email.subject = 'you email subject'
email.mimeType = "text/html"
email.body = """your email body including the <a href="$issueUrl">link to the issue</a>"""
try {
SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.mailQueue.addItem(item)
log.info "Success: email has been added to the mail queue"
} catch (MailException e) {
log.error "Failed to send an email due to error: e.message"
}
} else {
log.warn "Cannot send email. Outgoing emails are currently disabled"
}

 

TAGS
AUG Leaders

Atlassian Community Events