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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,556,570
Community Members
 
Community Events
184
Community Groups

the variable issue is undeclared

We have installed the Adaptavist ScriptRunner for JIRA plugin. We would like to implement Behaviours Initialisers; however we find that code that we think should work results in static type checking errors. For example, this Behaviour serverside script has issues as it indicates:

"The variable [issue] is undeclared"

Our code is:

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.project.Project
import groovy.json.*
import com.atlassian.jira.issue.index.IssueIndexManager

/* Set debugging level */
log.setLevel(org.apache.log4j.Level.DEBUG)

MutableIssue myIssue = (MutableIssue) issue
def MutableIssue issueObject = (MutableIssue) issue
def MutableIssue parentIssue = issueObject.getParentObject() as MutableIssue

/* Initialize global objects */
def customFieldManager = ComponentAccessor.getCustomFieldManager() //ComponentManager.getInstance().getCustomFieldManager()
def issueIndexManager = ComponentAccessor.getIssueIndexManager()

/* Define a table of issue type to Payer fields in that issue type */
def issueTypeToPayerFieldMap = [:]
issueTypeToPayerFieldMap["Miscellaneous Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Operational Inquiry"] = "Payer(s)"
issueTypeToPayerFieldMap["Parser Test File Request"] = "Payer(s) for File Stream ID"
issueTypeToPayerFieldMap["Payment Inquiry"] = "Payer"
issueTypeToPayerFieldMap["Policy Inquiry"] = "Payer(s)"
issueTypeToPayerFieldMap["Policy Research Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Project Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Report Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Test File Request"] = "Payer(s) for File Stream ID"
issueTypeToPayerFieldMap["Test Scenario Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Raw CIT Payment Inquiry"] = "CIT_PAYER"
def payerFieldName = issueTypeToPayerFieldMap[issue.issueTypeObject.name]

/* Collect the Client Team Assignment URL for this environment from the Project Description field */
def CTA_SERVICE_HOST = ""
def parsedDescription
def projectDescription = issue.getProjectObject().getDescription()
log.debug("projectDescription:" + projectDescription)
if (projectDescription) {
parsedDescription = new JsonSlurper().parseText(projectDescription)
}
log.debug("parsedDescription:" + parsedDescription)
if (parsedDescription?.CTA_SERVICE_HOST) {
CTA_SERVICE_HOST = parsedDescription.CTA_SERVICE_HOST
}
log.debug("CTA_SERVICE_HOST:" + CTA_SERVICE_HOST)

/* Retrieve list of user-selected Payers */
def payerFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName(payerFieldName.toString()))
log.debug("payerFieldValue:" + payerFieldValue)
log.debug("payerFieldValue.getClass():" + payerFieldValue.getClass())

/* Turn it into a comma-delimited list for inclusion in the service call */
def commaDelimitedPayerList = ''
//Raw CIT Payment Inquiry payer value is a string, not a list, handle it differently
if (payerFieldValue.getClass() == String) {
commaDelimitedPayerList = '{"payerKeys":["' +payerFieldValue+'"]'+'}'
} else {
commaDelimitedPayerList ='{"payerKeys":' + new groovy.json.JsonBuilder(payerFieldValue).toString()+'}'
}
log.debug("commaDelimitedPayerList:" + commaDelimitedPayerList)

/* Make call to client team assignment service */
def serviceURLString = CTA_SERVICE_HOST + "/teamAssignmentService/rest/teamAssignments/getTeamAssignmentsForPayers"
log.debug("serviceURLString:" + serviceURLString)
def url = new URL(serviceURLString)
def connection = url.openConnection()
connection.setRequestMethod("POST")
connection.doOutput = true
connection.setRequestProperty("Content-Type","application/json")
def writer = new OutputStreamWriter(connection.outputStream)
writer.write(commaDelimitedPayerList)
writer.flush()
writer.close()
connection.connect()
def rawServiceResponse = connection.content.toString()


/* Write the service response to the CLIENT_TEAM_SERVICE_RESPONSE field */
def responseField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "CLIENT_TEAM_SERVICE_RESPONSE"}
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
responseField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(responseField), rawServiceResponse), changeHolder)

/* Parse the JSON string into a groovy Map data structure for manipulation */
def parsedResponseMap = new JsonSlurper().parseText(rawServiceResponse.toString())
log.debug("Parsed JSON Response:" + parsedResponseMap)

/* stash missing JIRA users away for display/resolution */
def List missingUserList = []

if (issue.issueTypeObject.name == "Payment Inquiry" || issue.issueTypeObject.name == "Raw CIT Payment Inquiry") {
/* Role: Client Policy Manager */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CPM", "CPM(s)"))
/* Role: Client Service Manager */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CSM", "CSM(s)"))
/* Role: Client Service Medical Doctor */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CSMD", "Client MD(s)"))
/* Role: Policy Inquiry Analyst */
// missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "Policy Inquiry Analyst", "PIA(s)"))
/* Role: Policy Inquiry Analyst */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CSD", "CSD(s)"))
/* Role: Test Analyst Group */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "Test Analyst Group", "TA(s)"))
/* Role: FBA */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "FBA", "FBA(s)"))
}

/* Policy Research Request - NOTHING NEEDED */
/* Project Request - NOTHING NEEDED */
/* Misellaneous Request - NOTHING NEEDED */

/* save missing user list to JIRA */
def missingUsersField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Users Not In JIRA"}
IssueChangeHolder missingUsersChangeHolder = new DefaultIssueChangeHolder()
missingUsersField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(missingUsersField), missingUserList.join(",")), missingUsersChangeHolder)

/* Store and Re-index to save changes to DB and in-memory storage */
issue.store()
issueIndexManager.reIndex(issue)

/*****
function to set values of user custom fields in jira issue.
Inputs:
JSONString
JSON Key
JSON Value
Field Name
*****/
def updateMultiUserField(serviceResponseMap, key, value, fieldName) {
def userUtil = ComponentAccessor.getUserUtil()
def localCustomFieldManager = ComponentManager.getInstance().getCustomFieldManager()

/* iterate thru the list of nodes mathing our query */
def matchingNodes = serviceResponseMap.findAll{it.get(key) == value}
def List<ApplicationUser> matchingUsers = []
def List<String> missingUsers = []
matchingNodes.userId.each {
log.debug(key + ":" + value + " Node Found: " + it)
currentUser = userUtil.getUserByKey(it.toLowerCase())
if (currentUser != null) {
matchingUsers.add(currentUser)
} else {
log.debug(key + ":" + value + " AD User NOT Found in JIRA: " + it)
missingUsers.add(it)
}
}
log.debug(key + ":" + value + " Users List: " + matchingUsers)

def fieldToUpdate = localCustomFieldManager.getCustomFieldObjects(issue).find {it.name == fieldName}
IssueChangeHolder localFieldChangeHolder = new DefaultIssueChangeHolder()
if (fieldToUpdate.hasValue(issue)) {
log.debug(key + ":" + value + " has an existing value. Updating value.")
fieldToUpdate.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldToUpdate), matchingUsers), localFieldChangeHolder)
} else {
if (matchingUsers) {
log.debug(key + ":" + value + " has NO value. Creating value.")
fieldToUpdate.createValue(issue, matchingUsers)
}
}

log.debug(key + ":" + value + " issue.getCustomFieldValue(fieldToUpdate): " + issue.getCustomFieldValue(fieldToUpdate))

/* return a list of users who were not found in JIRA */
return missingUsers
}

/* Re-set debugging level */
log.setLevel(org.apache.log4j.Level.WARN)

1 answer

1 vote
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.
Feb 01, 2018

Behaviours do not have the issue variable. Instead you should use underlyingIssue, Kindly read the follwoing article:

https://scriptrunner.adaptavist.com/4.3.1/jira/behaviours-overview.html

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events