Forums

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

GroovyService not executing - incorrect code perhaps?

Tyler Brown-Jones June 3, 2018

 

Hi

 

Still new to Groovy - i am wondering if my code is incorrect as my groovy script does not seem to want to execute, i did have it as a script listener but for company purposes it needs to be changed to a service. The only thing i did when i changed it from listener to service was add the JQL search part, looking through it - it all seems to be fine (to the best of my knowledge anyhow). My code is below - (things are named test as to protect company name etc)


import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager;
import java.util.logging.Logger
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug("start of code")


def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11401)
def cFieldValue = issue.getCustomFieldValue(estField)
log.debug(cFieldValue)

jqlSearch = 'updated ≥ -4w'; // Create the JQL query to perform
theUser = "jira_bot"; // Specify the user that should execute the query if not logged in


SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
UserUtil userUtil = ComponentAccessor.getUserUtil();
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// If the user isn't specified, then set it to the default script running user
if (!user) {
user = userUtil.getUserObject(theUser);
}
// Set the default list of issues to null just to be safe
List<Issue> issues = null;

// Perform the search as a user and return the result so it can be validated.
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);

Issue issue = event.issue

if(parseResult.isValid()){
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
issue.each(){
if ( cFieldValue == 0.0) {
def subject = "${issue.key} has no Estimate"
def body = "https://test.atlassian.net/issue/${issue.key} \n This issue does not have an estimate"
def emailAddr = "test@test.com"
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

if (mailServer) {
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setBody(body.toString());
mailServer.send(email);
log.debug("Mail sent")
}
else {
log.debug("Please make sure that a valid mailServer is configured")
}
}
}
}
else{
log.error("Invalid JQL: " + jqlSearch);
}


 Also this is a screenshot of my service i am executing-

chrome_2018-06-03_23-22-30.pngchrome_2018-06-03_23-23-21.png

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 3, 2018

Hello,

Did you provide com.onresolve.jira.groovy.GroovyService class from the Service. Are there no logs from your service in the atlassian-jira.log?

Tyler Brown-Jones June 4, 2018

Hi Alexey, thanks for the response.

Could you ellaborate on this a bit more for me please 

Where are these logs stored as I tried looking for them.

Many thanks

Tyler

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 4, 2018
Tyler Brown-Jones June 4, 2018

Your second link, I did all of this.(you can see in the screenshots I provided)

 

I will look at the logs now and see what was outputted if it was.

 

Thanks again.

Tyler Brown-Jones June 4, 2018

@Alexey Matveev there is nothing within my logs related to this issue.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 4, 2018

It is strange. It should work. Make a simpler script with log.error("my script output"). Add it as it is explained in https://scriptrunner.adaptavist.com/4.3.0/jira/services.html and have a look at the logs.

Tyler Brown-Jones June 5, 2018

@Alexey Matveev do you think my file path could be incorrect within my service i set up? please check the screenshot

Tyler Brown-Jones June 5, 2018

@Alexey Matveev

I finally managed to find a log input for this

 IHNE.run(IHNE.groovy:17) is the following: "
def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11401)"

2018-06-04 23:05:00,010 Caesium-1-2 INFO anonymous no estimate - send email [c.o.jira.groovy.GroovyService] groovyService.run
2018-06-04 23:05:00,127 Caesium-1-2 DEBUG anonymous no estimate - send email [c.onresolve.jira.groovy] start of code
2018-06-04 23:05:00,233 Caesium-1-2 ERROR anonymous no estimate - send email [c.o.jira.groovy.GroovyService] Script service failed: D:\Atlassian\scripts\IHNE.groovy
groovy.lang.MissingPropertyException: No such property: issue for class: IHNE
at IHNE.run(IHNE.groovy:17)
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2018

Try to change the line to:

def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11401L)
Monique vdB
Community Manager
Community Managers are Atlassian Team members who specifically run and moderate Atlassian communities. Feel free to say hello!
June 7, 2018

@Tyler Brown-Jones I saw you tried to bump the thread, did you see Alexey's suggestion above?

Suggest an answer

Log in or Sign up to answer