Forums

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

Add a Label when creating issue via ScriptRunner mail handler?

Connor Kelly October 1, 2019

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

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Alejandro Suárez García
Atlassian Partner
October 3, 2019 edited

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

 

Connor Kelly October 4, 2019

Perfect, thank you!

TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events