Forums

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

Scriptrunner Mail Handler help

Justin Buchanan
January 20, 2021

Hi folks,

I'm attempting to setup a ScriptRunner Mail Handler that does the below:

1) Creates an issue

2) Adds attachments from the email to the issue

3) Adds users to 2 different custom fields

4) Adds a comment to the issue

5) Transitions the issue to a new status (not necessary but would be nice)

I'm new to Scriptrunner and Groovy but cobbled together something that half works from examples on the internet. Parts 3 and 4 work in the Console but not when added to the Mail Handler script and issues are created using the Mail Handler. I couldn't figure out transitions at all. Attachments also do not work. Here is my script. Apologies if there is anything redundant in there. Is anyone able to point me in the right direction here? Any help is appreciated.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager

def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)

def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("jira-admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = projectManager.getProjectObjByKey("TP")

def issueObject = issueFactory.getIssue()
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setDescription(MailUtils.getBody(message))
issueObject.setIssueTypeId(project.issueTypes.find { it.name == "Task" }.id)
issueObject.setReporter(reporter)

messageHandlerContext.createIssue(user, issueObject)

def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)
}

def purWatchers = userManager.getUserByName("jdoe")
def purCustom = customFieldManager.getCustomFieldObjectByName("Purchasing Watcher Users")

def accWatcher1 = userManager.getUserByName("jdoe")
def accWatcher2 = userManager.getUserByName("jsmith")
def accCustom = customFieldManager.getCustomFieldObjectByName("Accounting Watchers")

issue.setCustomFieldValue(purCustom, [purWatchers])
issue.setCustomFieldValue(accCustom, [accWatcher1,accWatcher2])

String userName = "jira-admin"
def userComment = userManager.getUserByName(userName)
CommentManager commentManager = ComponentAccessor.getCommentManager()
commentManager.create(issueObject,userComment,"[~jmith] can you approve this?", true)

issueManager.updateIssue(user, issueObject, EventDispatchOption.DO_NOT_DISPATCH, false)

 

3 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Pete Singleton
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 Champions.
November 2, 2019

I managed to get this working (using JMCF add-on) as follows:

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

double totalSP = 0
customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points");
enableCache = {-> false}

issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
if (issueLink.issueLinkType.name == "Epic-Story Link" ) {
int SP = (int)(issueLink.destinationObject.getCustomFieldValue(customField) ?: 0)
log.debug("Story Points : "+ SP);
log.debug("Issue :" + issueLink.getDestinationObject().getKey() );
totalSP = SP + totalSP;
}}

return totalSP
David Fischer
Community Champion
November 2, 2019

Note that this is with JMCF 1.7.  In JMCF 2, it would be one line of code. 

Sara Shoemaker
December 26, 2019

Could you tell me what that line of code would be?

David Fischer
Community Champion
December 27, 2019

Sure:

issue.stories?.sum { it.get("Story Points", 0) }

Be aware, though, that the "Story Points" field might be called differently on your instance (such as "Story points" - lowercase "p"). The safest is to use the custom field ID instead of name, which is accessible from the "Issue Fileds" help tab of the formula editor.

Chris Melville
Contributor
December 10, 2021

What would the equivalent JMCF code be for summing up the story points in an Initiative that contains Epics with story points?

Ideally, I would have one formula that works for either Initiatives or Epics, so I don't have to create a new context for the two issue types.

David Fischer
Community Champion
December 13, 2021
issue.portfolioChildIssues?.sum { it.get("Story Points", 0) }

This should actually work for both.

David

Like Chris Melville likes this
Chris Melville
Contributor
December 14, 2021

Thank you for the response.  It does work for Initiatives and other Portfolio hierarchy issue types, but does not work for Epics, even though they are defined in the hierarchy.  I can solve for both by simply combining the two.

0 votes
Danil Chernyshev
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 1, 2019
Not sure, but maybe something wrong with typeID or feldID. Could you try this:




//Scripted Fields
//Field name : StoryPointsSum
//Field Description : Sum of Story Points in Epic
//Default Configuration Scheme for Count of Open Stories
//Template : Number Field

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
def componentManager = ComponentManager.getInstance()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
double totalSP = 0




customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points");

if (issue.getIssueType().getName() != "Epic") {
return null
}

issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->


if (issueLink.issueLinkType.name == "Epic-Story Link" ) {
totalSP += (double)(issueLink.destinationObject.getCustomFieldValue(customField) ?: 0)
}
}

return totalSP 
0 votes
Pete Singleton
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 Champions.
July 17, 2019

Hi, I'm also trying to sum the story points and show the total on Epics via scripted fields.  Did you get this working??

TAGS
AUG Leaders

Atlassian Community Events