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)
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
Note that this is with JMCF 1.7. In JMCF 2, it would be one line of code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
issue.portfolioChildIssues?.sum { it.get("Story Points", 0) }
This should actually work for both.
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, I'm also trying to sum the story points and show the total on Epics via scripted fields. Did you get this working??
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.