Hi community,
I am not an expert in scripting but am really eager to learn more and to get things done by using groovy and Script Runner. Today I kindly ask you for your support since I get stuck. I have tried a lot and I have read a lot of articles in Script Runner documentation + library as well as around here. Most of the articles generally lead into the right direction but I can't put all these things together in order to achieve the following:
We're using Jira (Server) 7.13.11 and Service Desk 3.6.11 plus Script Runner for Jira 6.2.1. To get this task done I was going to set up a script to be used as a script listener via Script Runner only on the "Issue Created" event.
Please find below the current work on my script:
(parts that are commented out were part of my testing)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import groovy.transform.Field
import org.apache.log4j.Logger
import org.apache.log4j.Level
@Field Logger log = Logger.getLogger("Partner/Organisation setter:")
log.setLevel(Level.DEBUG)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserManager userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
MutableIssue updatedIssue = (MutableIssue) event.getIssue()
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("")
//def issueReporter
//def partner = customFieldManager.getCustomFieldObject("customfield_10111")
//def organisation = customFieldManager.getCustomFieldObject("customfield_10103")
def partner = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10111");
//config.partner = issue.getCustomFieldValue(partner);
def organisation = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10103");
//config.organisation = issue.getCustomFieldValue(organisation);
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//CustomField partner = customFieldManager.getCustomFieldObjectByName("partner")
//CustomField organisation = customFieldManager.getCustomFieldObjectByName("organisation")
// String organisationValue
// def partner = updatedIssue.getCustomFieldValue(partner)
log.info("Inspecting: " + updatedIssue.key)
log.info("Partner: " + partner)
// ### end of initial part ###
// ### start of test part ###
if(partner == "Test Partner"){
log.info("Updating Partner: Test Partner")
def issueReporter = userManager.getUserByKey('testpartner@domain.de')
issue.setReporter(issueReporter)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
organisation.updateValue(null, updatedIssue, new ModifiedValue(updatedIssue.getCustomFieldValue(organisation), "Test Partner"),new DefaultIssueChangeHolder())
//updatedIssue.setCustomFieldValue(organisation)
issueManager.updateIssue(event.getUser(), (MutableIssue)updatedIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
And this is the current outcome in log:
2020-08-14 10:34:49,868 INFO [Partner/Organisation setter:]: Inspecting: CS-96814 2020-08-14 10:34:49,868 INFO [Partner/Organisation setter:]: Partner: Partner 2020-08-14 10:34:49,872 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2020-08-14 10:34:49,872 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null java.lang.IllegalArgumentException: Can only compare Field objects. at com.atlassian.jira.issue.fields.ImmutableCustomField.compareTo(ImmutableCustomField.java:1841) at Script642.run(Script642.groovy:58)
Many thanks in advance for for help!
Best regards,
Johannes
This is how it looks in the end. During development we went into some other trouble but in the end we managed to get it working as a workflow post function with script runner):
import com.atlassian.fugue.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import groovy.transform.Field
import org.apache.log4j.Logger
import org.apache.log4j.Level
@Field Logger log = Logger.getLogger("Partner/Organisation setter:")
log.setLevel(Level.DEBUG)
@WithPlugin("com.atlassian.servicedesk")
@PluginModule ServiceDeskManager serviceDeskManager
@PluginModule OrganizationService organizationService
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserManager userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
def partner = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_id")
def organisation = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_id")
def partnerValue = partner.getValue(issue)
def organisationValue = organisation.getValue(issue)
//Logging
log.info("Partner: " + partnerValue.getClass())
log.info("Organisation: " + organisationValue.getClass())
def organisationList
def organization
switch (partnerValue.toString()) {
case 'Partner A':
organization = organizationService.getById(user, 1).right()?.get()
issue.setReporterId('partner.A')
break
case 'Partner B':
organization = organizationService.getById(user, 2).right()?.get()
issue.setReporterId('partner.B')
break
case 'Partner C':
organization = organizationService.getById(user, 3).right()?.get()
issue.setReporterId('partner.C')
break
// etc.
}
def organizationList = [organization]
issue.setCustomFieldValue(organisation, organizationList)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
It looks like there is an issue on line 58. Which part of the code is line 58?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
if(partner == "Test Partner"){
I was quite sure that from all lies this one would be fine. But I can be wrong. :-)
Line 58 makes sense btw., now I see it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think you may want to check if the value of the partner customfield is equal to "Test Partner"
def issue = event.issue
def partner = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10111")
def partnerValue = partner.getValue(issue)
if(partnerValue.equals("Test Partner")
{
...
}
I usually like to test these before I post them, but I'm unable to test at the moment.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.