Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null

Vivo Bandito January 19, 2023

Hello, I'm having some difficulty getting some code to work with a specific issue type of "Escalation" to the specific DBA webhook. I've been able to verify with our same slack webhook (removed for obvious reasons) is working and that other issue types are working. Here is the error message followed by the code.

 

ERROR

2023-01-19 09:13:50,660 WARN [runner.ScriptBindingsManager]: 
Atl Admin: false
Database Admin: true
Network Admin: false
Sys Admin: false
2023-01-19 09:13:50,660 INFO [runner.ScriptBindingsManager]: Issue Key: EDCO-248989 Issue Type is Escalation
2023-01-19 09:13:50,816 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2023-01-19 09:13:50,816 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
groovyx.net.http.HttpResponseException: status code: 400, reason phrase: Bad Request
at groovyx.net.http.RESTClient.defaultFailureHandler(RESTClient.java:263)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.RESTClient.post(RESTClient.java:141)
at groovyx.net.http.RESTClient$post.call(Unknown Source)
at Script319.run(Script319.groovy:122)

 

SCRIPT

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig

log.setLevel(org.apache.log4j.Level.DEBUG)

def issue = event.issue as Issue
def typesToExclude = ["Change","Epic","Project Milestone","Project Deliverable (Sub-task)","Request Test"]

if(typesToExclude.contains(issue.issueType.name)) { log.info "Not a valid issue for notification"; return }

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()

CustomField cfaTeam = customFieldManager.getCustomFieldObject("customfield_14202") // Assigned Team
FieldConfig fcaTeam = cfaTeam.getRelevantConfig(issue)

Option sysa = optionsManager.getOptions(fcaTeam)?.find { it.getValue() == 'Systems Administration' }
Option dba = optionsManager.getOptions(fcaTeam)?.find { it.getValue() == 'Database Administration' }
Option na = optionsManager.getOptions(fcaTeam)?.find { it.getValue() == 'Network Administration' }
Option atl = optionsManager.getOptions(fcaTeam)?.find { it.getValue() == 'Atlassian Administration' }

Option teamOption = issue.getCustomFieldValue(cfaTeam) as Option

def isSysAdmin = sysa == teamOption
def isDba = dba == teamOption
def isNa = na == teamOption
def isAtl = atl == teamOption

def infTeamsList = [isAtl, isDba, isNa, isSysAdmin]


if(!infTeamsList.contains(true)) { log.warn "Not an Infrastructure Team"; return }
log.warn "\nAtl Admin: ${infTeamsList[0] as String}\nDatabase Admin: ${infTeamsList[1] as String}\nNetwork Admin: ${infTeamsList[2] as String}\nSys Admin: ${infTeamsList[3] as String}"

def webhookPath = ''
final webhookPathAtl = '/services/xxxx'
final webhookPathElOne = '/services/xxxxx'
final webhookPathElTwo = '/services/xxxxxx'
final webhookPathElThree = '/services/xxxxxxx'
final webhookPathElLeadership = '/services/xxxxxxxx'
final webhookPathNetworkAdmin = '/services/xxxxxxxxx'
final webhookPathDBA = 'services/xxxxxxxxxx'




if (isAtl) { webhookPath = webhookPathAtl }
else if (isDba) { webhookPath = webhookPathDBA }
else if (isNa) { webhookPath = webhookPathNetworkAdmin }
else if (isSysAdmin)
{
CustomField cfaEl = customFieldManager.getCustomFieldObject("customfield_19303") // Escalation Level
FieldConfig fcaEl = cfaEl.getRelevantConfig(issue)

Option one = optionsManager.getOptions(fcaEl)?.find { it.getValue() == 'Level 1' }
Option two = optionsManager.getOptions(fcaEl)?.find { it.getValue() == 'Level 2' }
Option three = optionsManager.getOptions(fcaEl)?.find { it.getValue() == 'Level 3' }
Option leadership = optionsManager.getOptions(fcaEl)?.find { it.getValue() == 'Leadership' }

Option elOption = issue.getCustomFieldValue(cfaEl) as Option ?: one

def levelOne = one == elOption
def levelTwo = two == elOption
def levelThree = three == elOption
def levelLead = leadership == elOption

if(levelOne) {
log.debug "Sys Admin level 1 webhook path set"
webhookPath = webhookPathElOne
}
else if(levelTwo) {
log.debug "Sys Admin level 2 webhook path set"
webhookPath = webhookPathElTwo
}
else if(levelThree) {
log.debug "Sys Admin level 3 webhook path set"
webhookPath = webhookPathElThree
}
else if(levelLead) {
log.debug "Sys Admin Leadership webhook path set"
webhookPath = webhookPathElLeadership
}
else { log.error "Sys Admin detected, but no EL found. No notification sent."; return }

}
else {
log.error "No Alert Defined"
return
}

final webhookBase = 'https://hooks.slack.com'
def jiraBaseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)

//Issue detail augmentation
String org_summary = issue.summary
String summary = org_summary
String org_description = issue.description ?: ""
String description = org_description.replace("*Description copied from Parent:*","") ?: ""

String textBlock = "${issue.issueType.name}: <$jiraBaseUrl/browse/$issue.key|${summary}>"
log.info "Issue Key: ${issue.key} Issue Type is ${issue.issueType.name}"

def body = [
text: summary,
blocks: [
[
type: 'section',
text: [
type: 'mrkdwn',
text: textBlock
]
]
]
]

def response = new RESTClient(webhookBase).post(
path: webhookPath,
contentType: ContentType.HTML,
body: body,
requestContentType: ContentType.JSON
) as HttpResponseDecorator

assert response.status == 200: "Request failed with status $response.status. $response.entity.content.text"

0 answers

Suggest an answer

Log in or Sign up to answer