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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,466,493
Community Members
 
Community Events
176
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

2 votes
Answer 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

Atlassian Community Events