Usually ...
HttpResponseException: status code: 400, reason phrase: Bad Request
...means that something is wrong with your request. Either something missing in the headers or in the body and the remote endpoint is rejecting it.
I would add a log above the RESTClient call to output your body as Json then try the exact same request in a tool like postman to see if the API endpoint might return some more detailed information about what the error is.
log.info (new JsonBuilder(body).toPrettyString())
You might want to add some handlers to the rest client.
E.g.
RESTClient client = new RESTClient(webhookBase)
client.handler.success = {HttpResponseDecorator response, responsePayload ->
//either return the full resposne as httpResponseDecorator or convert it somehow e.g.
return [success:true, status: response?.status, content: responsePayload
}
client.handler.failure = {HttpResponseDecorator response ->
log.error "FailureHandler - Error making $method request to $client.uri"
log.error "FailureHandler - Submitted Payload: $payload"
def content = "$response.entity.content".toString()
log.error "FailureHandler - Response ContentType: $response.contentType"
log.error "FailureHandler - Response: $content"
//return something, either the response, or some error message or transformed the decorator into a simple map, e.g.
return [success:false, status: response?.status, content: content]
}
client.post(method, contentType) {
path: webhookPath,
contentType: ContentType.HTML,
body: body,
requestContentType: ContentType.JSON
} //this will return whatever is returned by the appropriate handler
Hey @PD Sheehan ,
Not too sure how I would implement this into the code. Do you have any suggestions or resources as to how?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try it like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import groovy.json.JsonBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
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.customFieldManager
CustomField teamCf = customFieldManager.getCustomFieldObject("customfield_14202") // Assigned Team
Option selectedTeamOption = issue.getCustomFieldValue(teamCf) as Option
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'
def infrastructureTeamMap = [
'System Administrations' : [
uselevel: true,
levelMap: [
'Level 1' : webhookPathElOne,
'Level 2' : webhookPathElTwo,
'Level 3' : webhookPathElThree,
'Leadership': webhookPathElLeadership,
]
],
'Database Administration' : webhookPathDBA,
'Network Administration' : webhookPathNetworkAdmin,
'Atlassian Administration': webhookPathAtl
]
def isInfraTeam = infrastructureTeamMap.keySet().any { it == selectedTeamOption.value }
if (!isInfraTeam) {
log.warn "Not an Infrastructure Team"; return
}
if (infrastructureTeamMap[selectedTeamOption.value]?.levelMap) {
CustomField escalationLevelCf = customFieldManager.getCustomFieldObject("customfield_19303") // Escalation Level
Option selectedEscLvl = escalationLevelCf.value as Option
webhookPath = infrastructureTeamMap[selectedTeamOption.value]?.levelMap[selectedEscLvl.value]
if (!webhookPath) {
log.error "$selectedTeamOption.value detected which expects an Escalation Level, but none was found. No notification sent."
return
} else {
log.debug "webhookPath set to $webhookPath because Escalation Level is $selectedEscLvl.value"
}
} else {
webhookPath = infrastructureTeamMap[selectedTeamOption.value]
if (!webhookPath) {
log.error "Assigned Team '$selectedTeamOption.value' which doesn't have a webhook path defined. No notification sent."
return
} else {
log.debug "webhookPath set to $webhookPath because Assigned Team is $selectedTeamOption.value"
}
}
final webhookBase = 'https://hooks.slack.com'
def jiraBaseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
//Issue detail augmentation
String org_summary = issue.summary
String org_description = issue.description ?: ""
String description = org_description.replace("*Description copied from Parent:*", "") ?: ""
String textBlock = "$issue.issueType.name: <$jiraBaseUrl/browse/$issue.key|$issue.summary>"
log.info "Issue Key: $issue.key Issue Type is $issue.issueType.name"
def body = [
text : issue.summary,
blocks: [[type: 'section', text: [type: 'mrkdwn', text: textBlock]]]
]
log.info "Making a POST Request to $webhookBase\$webhookPath \n with Payload: \n${new JsonBuilder(body).toPrettyString()}"
RESTClient client = new RESTClient(webhookBase)
client.handler.success = { HttpResponseDecorator response, responsePayload ->
log.debug "success post detected with response: $responsePayload"
return [success: true, status: response?.status, content: responsePayload]
}
client.handler.failure = { HttpResponseDecorator response ->
log.error "FailureHandler - Error making POST request to $client.uri"
log.error "FailureHandler - Submitted Payload: $body"
def content = "$response.entity.content".toString()
log.error "FailureHandler - Response ContentType: $response.contentType"
log.error "FailureHandler - Response: $content"
return [success: false, status: response?.status, content: content]
}
def response = client.post(
path: webhookPath,
contentType: ContentType.HTML,
body: body,
requestContentType: ContentType.JSON
)
log.info "Final response = \n${new JsonBuilder(response).toPrettyString()}"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @PD Sheehan ,
I was able to get the code implemented into the listener. Here is the resulting log after creating an escalation to the Database Administration Team. The log is stating that the post was successful but no message was posted in the channel.
Once again it is only the escalation tickets that are failing.
2023-01-31 11:08:14,342 WARN [runner.ScriptBindingsManager]: Atl Admin: false Network Admin: false Sys Admin: false Database Admin: true 2023-01-31 11:08:14,342 INFO [runner.ScriptBindingsManager]: Issue Key: EDCO-250771 Issue Type is Escalation 2023-01-31 11:08:14,342 INFO [runner.ScriptBindingsManager]: Making a POST Request to https://hooks.slack.com$webhookPath with Payload: { "text": "Escalation: Jira Slack Test", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "Escalation: <https://jira.edcohis.com/browse/EDCO-250771|Escalation: Jira Slack Test>" } } ] } 2023-01-31 11:08:14,529 DEBUG [runner.ScriptBindingsManager]: success post detected with response: ok
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Looks like I had a slash in the wrong direction in this line:
log.info "Making a POST Request to $webhookBase\$webhookPath \n with Payload: \n${new JsonBuilder(body).toPrettyString()}"
It should be
log.info "Making a POST Request to $webhookBase/$webhookPath \n with Payload: \n${new JsonBuilder(body).toPrettyString()}"
But after this, seems like the error is perhaps on the slack side.
I would use the information generated in the logs and try to manually create a post using a tool like Postman.
You have a well-formed JSON that can be pasted into the body of the request:
{
"text": "Escalation: Jira Slack Test",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Escalation: <https://jira.edcohis.com/browse/EDCO-250771|Escalation: Jira Slack Test>" }
}
]
}
Just try to make a POST to the same URL as what Jira tried to do and examine all the details of the response from slack. There might be some clues.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @PD Sheehan ,
After the "\" to "/" the webhook url logging part and the actual slack message went through! Do you have any idea as to why the message was previously failing. My only thoughts would be that the following snippet was the culprit.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry no idea why this failed before.
But glad you got it all to work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.