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

ScriptRunner custom event listener

Gleb Kartashov July 23, 2019

Hi, this might be a bit techical.

The task is;

When one of two specific issue fields is changed:

  1. Update 2 other custom fields 
  2. Execute a HTTP request

 

The problem seems to be that originalissue global variable is unaccesible in the Listener. Is there a way to create a field changed listener instead of issue updated lisntener

The second thing is, some imports also do not seem accesible, though same code works perfectly in workflow post function set field value with Groovy.

Here's my code (please don't mind the issue global variable, I know that for Listeners it is event.getissue)

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;

import java.util.ArrayList;
import java.util.stream.Collectors;


public Object GetInsightObject(int key){
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().
findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
return objectFacade.loadObjectBean(key);
}


public Object GetInsightObject(String key) {
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().
findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
return objectFacade.loadObjectBean(key);
}


def GetInsightValue (int InsightObjectId, int InsightAttributeId) {
def insightObjectAttributeValueBeans = GetInsightObject(InsightObjectId, InsightAttributeId);
if (insightObjectAttributeValueBeans == null){
return null;
}
else{
def insightValues = insightObjectAttributeValueBeans[0];
if (insightValues == null){
}
else {
def insightValue = insightValues.getValue();
return insightValue;
}
}
}







def GetInsightObject (int InsightObjectId, int InsightAttributeId){
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);

def obj = objectFacade.loadObjectBean(InsightObjectId);
if (obj == null){
return false;
}
else{
}
attribType = objectTypeAttributeFacade.loadObjectTypeAttributeBean(InsightAttributeId);
if (attribType == null){
return false;
}
else{
}
def attrib = objectFacade.loadObjectAttributeBean(InsightObjectId, InsightAttributeId);
if (attrib == null){
return false;
}
else{
}
def attribValues = attrib.getObjectAttributeValueBeans();
if (attribValues == null){
return null;
}
else {
return attribValues;
}
}


if( (issue.get("customfield_10226") != originalIssue.get("customfield_10226")) || (issue.get("priority") != originalIssue.get("priority"))){ //service or priority field updated
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");

/* The reference to the object facade which you can use in your code */
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
def InsightObjectKey = issue.get("customfield_10226")
InsightObjectKey = InsightObjectKey[0].toString()
def tempkey = (InsightObjectKey=~/\(TSS-.*\)/)[0]
tempkey = tempkey.drop(1) // get object id
tempkey = tempkey[0..-2]
//log.info(tempkey)
def objectID = objectFacade.loadObjectBean(tempkey).getId()

def react = 191
def react_critical = 193
def resolution = 192
def resolution_critical = 194
def priorityValue = issue.getPriorityObject.getName() //check priority value
if(priorityValue == 'P1' || priorityValue == 'P2'){ //
issue.setFieldValue("customfield_11200", GetInsightValue(objectID,react_critical)) //set new crit react time
issue.setFieldValue("customfield_11201", GetInsightValue(objectID,resolution_critical)) //set new crit resolution time
}
else if(priorityValue == 'P3' || priorityValue == 'P4'){ // check priority value
issue.setFieldValue("customfield_11200", GetInsightValue(objectID,react)) //set new react time
issue.setFieldValue("customfield_11201", GetInsightValue(objectID,resolution)) //set new resolution time
}

//RESET SLA
def SLA_id = issue.get("customfield_11100")[0].toString()[0]
//TODO http request here
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Yury Lubanets August 13, 2019

Hi @Gleb Kartashov 

You can check if the changes with a field were and get custom fields' values (original & new) from history using ChangeHistoryManager class.

def fieldHistory = ComponentAccessor.changeHistoryManager
.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans
.find { it.field == 'fieldName' }

If 'fieldHistory' is not null so the field was changed and you can get values.

def oldValue = fieldHistory.getFromString()

def newValue = fieldHistory.getToString()

To set a field's value in Listener you should use IssueService class.

def issueInputParameters = ComponentAccessor.issueService.newIssueInputParameters().with {

addCustomFieldValue(Long customFieldId, String... values)

}

def updateValidationResult = ComponentAccessor.issueService.validateUpdate(ApplicationUser user, Long issueId, issueInputParameters)

def issueResult = ComponentAccessor.issueService.update(ApplicationUser user, createValidationResult)
TAGS
AUG Leaders

Atlassian Community Events