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.
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)
}
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.