How to share issue by email using ScriptRunner?

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

1 vote

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)

 

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 Behnke Community Champion Jan 11, 2018

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

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

Steven Behnke Community Champion Jan 11, 2018

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

Your code example works fine, thakn you!

Steven Behnke Community Champion Jan 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!

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

795 views 2 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you