How to share issue by email using ScriptRunner?

Yanduganov Andrey January 10, 2018

I know JIRA has API method for share issue by email. And I have custom script in post-function. I want to send this email from my script. Built-in script Send Custom Email doesn't fit for me because I need send email just like if I pressed Share button on Issue screen. API doesn't fit too.

I dont know other ways for it.

1 answer

1 accepted

2 votes
Answer accepted
Steven F Behnke
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.
January 11, 2018

What's wrong with the Send Custom Email? It's not clear why it's not sufficient.

Code example

First of all, the Share feature is provided by a plugin, so we need to use the @WithPlugin and @PluginModule annotations instead of just importing the classes.

Second, we must construct a ShareBean, which represents most of the input for the operation. Then, we need to pass the bean, the Issue, and the Sender (user) to ShareService.validateShareIssue() method.

Finally, this returns a Validation Result. The result is either Valid or has Errors. We check the state and either log an error, or proceed and share the issue.


import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.plugins.share.ShareService
import com.atlassian.jira.plugins.share.ShareBean

import java.util.Collections

log.setLevel(org.apache.log4j.Level.INFO)

@WithPlugin("com.atlassian.jira.jira-share-plugin")
@PluginModule
ShareService shareService

def remoteUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def user = ComponentAccessor.userManager.getUserByKey("admin")
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("PROJ-1")

def userkeys = Collections.singleton(user.key) as Set<String>
def emails = Collections.emptySet() as Set<String>
def message = "Hello World!" as String

def shareBean = new ShareBean(
userkeys,
emails,
message,
null // jql
)

def result = shareService.validateShareIssue(remoteUser, shareBean, issue)

if(!result.isValid()) {
log.error "Failed to validate Share Issue parameters:\n${result.errorCollection}"
return null
}

log.info "${remoteUser.displayName} is sharing ${issue.key} ${issue.summary} with ${user.displayName}."
shareService.shareIssue(result)

 

Yanduganov Andrey January 11, 2018

Because I need exactely same email as if I pressed Share buttom. Email must be autogenerated by JIRA and must have appropriate styles. Just like in the picture:

Screenshot_1.png

Steven F Behnke
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.
January 11, 2018

Then just construct your own template that looks like it. You can copy it from the emails you get.

Yanduganov Andrey January 11, 2018

So.. There is no way to do it with standard JIRA tools?

Steven F Behnke
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.
January 11, 2018

Very few people have tried to do what you're doing.

Yanduganov Andrey January 11, 2018

Your code example works fine, thakn you!

Steven F Behnke
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.
January 11, 2018

Oh excellent! :) I hadn't ever seen those classes or API before, so I wasn't really sure... I did a single test and didn't see it work... I guess I'll have to look closer! 

Glad it worked though, and thanks for the update!

Gray Watson July 11, 2023

<Removed>

Suggest an answer

Log in or Sign up to answer