Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Add a Comment to the Issue Mentioned in the Mail Subject

Created 1 year ago, Updated 2 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.permission.ProjectPermissions
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageHandlerContext
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.mail.MailUtils
import groovy.transform.Field
import javax.mail.Message

// should dispatch Issue Commented event
final dispatchCommentedEvent = true

// allow comments from non-jira users
final allowCommentsFromUnregisteredUsers = true

// to allow comments from any email sender you must specify an existing user who will add the comments on their behalf
@Field final commentAsUser = 'impersonatingUser'

def subject = message.subject as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (!issue) {
    // existing issue not found within subject, so no issue will be updated
    return
}

def body = MailUtils.getBody(message)
if (!body) {
    // mail has no body content so comment will not be added
    return
}

def commentUser = getCommentUser(message, issue, allowCommentsFromUnregisteredUsers)
if (!commentUser) {
    log.error("Could not add comment to ${issue.key} from email with subject $subject, as no user with permission to comment was found")
    return
}

// get senders email addresses and prepend to mail body so comment has source email address on the first line
def senders = MailUtils.getSenders(message).join(',')
def commentBody = "$senders\n$body"

(messageHandlerContext as MessageHandlerContext).createComment(issue as Issue, commentUser as ApplicationUser, commentBody as String, dispatchCommentedEvent as Boolean)

def getCommentUser(Message message, Issue issue, Boolean allowCommentsFromNonJiraUsers) {
    def userManager = ComponentAccessor.userManager
    def permissionManager = ComponentAccessor.permissionManager
    def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)

    def sender = (messageUserProcessor as MessageUserProcessor).getAuthorFromSender(message)
    def delegatedUser = allowCommentsFromNonJiraUsers ? userManager.getUserByName(commentAsUser as String) : null

    // if sender can add comments return sender user, else return the commentAsUser
    permissionManager.hasPermission(ProjectPermissions.ADD_COMMENTS, issue, sender) ? sender : delegatedUser
}
Having an issue with this script?
Report it here