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,360,113
Community Members
 
Community Events
168
Community Groups

groovy.lang.MissingMethodException: No signature of method: addMessage()

I have a scriptrunner automation script that adds an email address(extracted from ticket description) to a customField. This script will be triggered(through project automation) when creating a new ticket. I have used the the utility functions available for writing information and error messages to the Automation for Jira audit log- addMessage(String message) and addError(String errorMessage). Here is the link to documentation: 

https://scriptrunner.adaptavist.com/latest/jira/plugins/automation-for-jira.html

I have a built-in test runner script to test the above scriptrunner automation script. I have followed the documentation from this link : https://scriptrunner.adaptavist.com/latest/jira/testing.html

When I run the built-in test runner script, I get the below exception :

groovy.lang.MissingMethodException: 

No signature of method: automationScriptName.addMessage() is applicable for argument types: (String) values: [XXXX].

If I comment the addMessage(String message) line on the automation script, the groovy.lang.MissingMethodException does not occur and my test will pass. Commenting those line is the temporary workaround that I have.

Wondering why the utility functions - addMessage(String message) and addError(String errorMessage) not supported when the script is called from the test runner framework ?

Any insight on this issue is greatly appreciated.

 

Thanks,

Anita Kalidoss

 

 

 

 

1 answer

1 accepted

0 votes
Answer accepted

Hi @Anita Kalidoss

Could you please share a screenshot of your Automation configuration?

I suspect (I may be wrong) that you are invoking the method as:-

automationScriptName.addMessage()

Could you instead try to invoke like:-

addMessage("<Add Your Message Here>")

It would be best if you could share your code so I can better understand the problem.

Some examples are provided in the latest ScriptRunner Documentation.

Thank you and Kind regards,

Ram

Hi Ram,

I'm not invoking the method as automationScriptName.addMessage(). Please see the attached screenshot for automation configuration, automation-script code and the unit test code.

Automation Configuration.png


Automation Script code: saveSenderEmailToIssue.groovy

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def emailPattern = /[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})/
def description = "${issue.description}"
def lastLine = description.substring(description.indexOf("[Created via e-mail received from:")).split(" ")
def email = lastLine[lastLine.size() - 1]
email = email.substring(0, email.length() - 1)

if(email.endsWith(">"))
email = email.substring(1, email.length() - 1)

addMessage("Email address found: " + email)
CustomField senderCf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sender");
def changeHolder = new DefaultIssueChangeHolder()
senderCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(senderCf), email), changeHolder)

if(!email.matches(emailPattern))
{
   addError("Email address might be invalid or missing: " + email)

}


Test Runner Built-In unit test code: saveSenderEmailToIssueIT.groovy

package com.test.scripts.it

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.onresolve.scriptrunner.canned.common.admin.SrSpecification
import spock.lang.Shared
import org.slf4j.LoggerFactory

//TODO add some description
class SaveSenderEmailToIssueIT extends SrSpecification {

@Shared
IssueService issueService = ComponentAccessor.getComponent(IssueService)

@Shared
IssueManager issueManager = ComponentAccessor.issueManager

@Shared
ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

@Shared
UserManager userManager = ComponentAccessor.getUserManager()

@Shared
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)

@Shared
def scriptRunner = ScriptRunnerImpl.getScriptRunner()


def "Save Email address to Sender Custom Field"() {
setup:
def logger = LoggerFactory.getLogger(this.class)
logger.debug("Inside Setup")
long newProjectId = 10201
def issueTypeID = "10001"
ApplicationUser reporter = userManager.getUserByName("external_user")


and:
logger.debug("Inside Setup and")
def issueInputParams = issueService.newIssueInputParameters()
issueInputParams.with {
projectId = newProjectId
priorityId = "3"
description = "test desc updated" + "\r\n[Created via e-mail received from: testuser@example.com]"
summary = "Test Updated saveSenderEmailAddressToIssue.groovy"
issueTypeId = issueTypeID
reporterId = reporter.name

}

when:
def createValidationResult = issueService.validateCreate(currentUser, issueInputParams);

then:
!createValidationResult.errorCollection.hasAnyErrors()

when:
IssueService.IssueResult createResult = issueService.create(currentUser, createValidationResult)
def createdIssueKey = createResult.getIssue().key
MutableIssue newIssue = createResult.getIssue()

then:
issueManager.isExistingIssueKey(createdIssueKey) == true

when:

//Use this line to test calling the automation script from the unit test
scriptRunner.runFileAsScript(new File("automation-scripts/saveSenderEmailAddressToIssue.groovy"), [issue: newIssue])
CustomField senderCf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11300")
def senderEmailAddress = senderCf.getValue(newIssue)


then:
senderEmailAddress == "testuser@example.com"

}
}


Error displayed in Test runner Built-in script when saveSenderEmailToIssueIT  is run:

Run 1
Failures 1
Error Save Email address to Sender Custom Field(com.test.scripts.it.SaveSenderEmailToIssueIT)
groovy.lang.MissingMethodException:
No signature of method: saveSenderEmailAddressToIssue.addMessage() is applicable for argument types: (String) values: [Email address found: testuser@example.com]

Hi @Anita Kalidoss

I have tried to run a basic test using Jira's Automation, and I don't seem to be encountering any problems.

Below is the code that I have tested with:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

import java.util.regex.Pattern

def description = issue.description
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def sampleText = customFieldManager.getCustomFieldObjectsByName('Sample Text Field').first()

def pattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}")
def matcher = pattern.matcher(description)

if (matcher.find()) {
issue.setCustomFieldValue(sampleText, matcher.group())
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of my automation configuration:- automation_config_1.png

In my environment, I am not adding any *Then* options.

Below are a few screenshots for your reference:-

test1.pngtest2.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Like Vikrant Yadav likes this

Hi Ram,

I noticed couple of things:

1) You didn't use the two utility functions available for writing information and error messages to the Automation for Jira audit log - addMessage(String message) and addError(String errorMessage) in your code

2) You are testing your code through project automation. MissingMethodException on addMessage(string) won't show up when testing through project automation. I'm testing the code through Scriptrunner Built-In Scripts "Test Runner" option. The MissingMethodException on addMessage(string) shows only when testing the code through "Test Runner" option. I have written a unit test code "saveSenderEmailToIssueIT.groovy"(code attached in my previous response). Below are the screen shots of running the unit test through Test Runner. 

We have a unit test plug-in created(followed steps in this link:https://scriptrunner.adaptavist.com/latest/jira/testing.html) to include the unit tests for project automation. This plug-in will allow/support our testing effort to be automated.

TestRunner_Screen1.png

TestRunner_Screen2.png

 

Thanks,

Anita

Hi Ram,

I opened a support case with Adaptavist Scriptrunner on this. - https://productsupport.adaptavist.com/servicedesk/customer/portal/2/SRJSUP-26749

They confirmed that "there is no public API from A4J that allow us to inject the execution context to execute the script by ourselves".

 

Thanks for your assistance!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

184 views 1 6
Read article

Atlassian Community Events