Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,925
Community Members
 
Community Events
165
Community Groups

Add a Label when creating issue via ScriptRunner mail handler?

Hello! I'm trying to use ScriptRunner's mail handler to create issues via mail, and so far I've got everything working for most system fields & all custom fields. However, I can't seem to nail it down for Labels, and searching around I think the problem is because I'm not properly setting it up for a new issue vs an existing one. Does anyone have any insight on this?

Script & errors below, my notes inline.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.service.services.file.FileService
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.jira.issue.label.LabelManager // am I importing the wrong thing here?
import com.atlassian.mail.MailUtils
import org.apache.commons.io.FileUtils

def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)

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

if (issue) {
return
}

ApplicationUser user = userManager.getUserByName("admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user

def project = projectManager.getProjectObjByKey("TEST")
def issueObject = issueFactory.getIssue()

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

LabelManager.addLabel(user.getDirectoryUser(),issue.id,labels('test_label'),false,false) // this line has the errors.

issue = 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)
}

Errors for line 46:

[Static type checking] - Cannot find matching method Script103#labels(java.lang.String). Please check if the declared type is correct and if the method exists.
Possible solutions: equals(java.lang.Object) @line 46, column 56

[Static type checking] - Cannot find matching method com.atlassian.jira.issue.label.LabelManager#addLabel(com.atlassian.crowd. // it trails offscreen here
java.lang.Long, java.lang.Object, boolean, boolean). Please check if the declared type is correct and if the method exists.

 

1 answer

1 accepted

Hi @Connor Kelly ,

The problem is that you trying to use the class without instancing it.

Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.service.services.file.FileService
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

UserManager userManager = ComponentAccessor.getComponent(UserManager)
ProjectManager projectManager = ComponentAccessor.getProjectManager()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
MessageUserProcessor messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)

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

if (issue) {
return
}

ApplicationUser user = userManager.getUserByName("admin")
ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user

Project project = projectManager.getProjectObjByKey("TEST")
MutableIssue issueObject = issueFactory.getIssue()

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

issue = messageHandlerContext.createIssue(user, issueObject)

labelManager.addLabel(user, issue.getId(), "Label", false)

MailUtils.Attachment[] attachments = MailUtils.getAttachments(message)

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

 

Perfect, thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

285 views 0 6
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you