Groovy Script as a Service

Hi, 

I'm having a few issues getting a Groovy script to execute as a timed job in the Services panel. The script seems to be sound, and will quite happily execute using the script console both as an inline script and as a file from the file system. 

I've set the service up as:

Class: com.onresolve.jira.groovy.GroovyService

Name: TestScript

with a schedule of every 15 minutes. 

When setting the path, I've tried it as an absolute path from the root, and as a relative path in the scripts folder, both of which work in the script console. The script has a .groovy extension

The logs don't report any errors in finding the files, running the script etc. but at the same time, the issue that is meant to be created by the script isn't created in JIRA. 

Is there anything I should be doing to my script in order to make it work differently under the service job? I'm assuming that it's finding the script given the lack of complaining that the file is missing in the logs, but that's just an assumption. 

Any help would be greatly appreciated :)

Dave

2 answers

Hey dave!

Would you mind sharing your service script with me so that I might take a look and see if there are any issues?

Additionally, it may help to see a screenshot or two of your service configuration in JIRA.

Thanks!                                                                                                     Aidan

Hi Aidan, 

Please find the details below

Dave

Hi Aidan,

Thanks for getting back to me, I'm sure I'm missing something stupid but can't see the wood for the trees. The config for the service is:

Details.png

The overview is:

Service.png

 and the code is:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.project.Project;

def createIssue(String createdBy, String projKey, String issueSummary, String reportedBy, String issueType = "12345") {
ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey(createdBy);
IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters inputParameters = issueService.newIssueInputParameters();
Project curPrj = ComponentAccessor.getProjectManager().getProjectByCurrentKey(projKey)
if(curPrj == null)
return "no project"

inputParameters.setProjectId(curPrj.getId())
.setIssueTypeId(issueType)
.setSummary(issueSummary)
.setReporterId(reportedBy)
.setAssigneeId(curPrj.getLeadUserKey())

IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, inputParameters);

if(createValidationResult.isValid()){

IssueService.IssueResult createResult = issueService.create(user, createValidationResult);
if(!createResult.isValid())
return "Error in Issue Creation"
}
else{
return "Not creating invalid issue"
}
}

createIssue("testuser1", "TST", "This is a another test issue", "testuser2");

 Hoefully you can spot something I'm missing. 

Thanks again

Dave

Hey again Dave!

Sorry for my super belated reply :(

Looking at your script and config, it's hard to get a read on the problem. But I've talked with a colleague and we have a couple of ideas:

  1. It looks like you are missing a package declaration at the top of your script. Check to make sure that this is intentional. If the script is within a package that isn't specified, this could be a reason for the script to fail, as it will not run.
  2. As stated in this community question ===> https://community.atlassian.com/t5/Answers-Developer-Questions/Unable-to-run-a-groovy-script-as-a-service-using-script-runner/qaq-p/503808 <=== you need to set the "logged in user." In the context of a Service, the logged in user is anonymous. The problem here may be that, since JIRA authenticates the use of API against the logged in user, the anonymitiy of said user makes it so that statements like:
    ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey(createdBy);
    aren't working properly. To fix this, look to the community question that I linked above.
  3. You may be getting errors and you don't know it. In your script, you are using return statements instead of logging statements for your errors, so you may be getting one but they aren't showing up in the logs. Instead of the return statements, do:
    log.error("Error in Issue Creation")

Once you've checked these things and have made the appropriate changes, look try running the service again and checking your logs.

Let me know what you find!

Cheers,                                                                                                           Aidan

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

130 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you