Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

292 views 5 14
Read article

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