Skip to content

Commit c1ee92b

Browse files
authored
Add the task output in the webhook Slack payload in case of run failure #1587 (#1634)
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 16ed5c4 commit c1ee92b

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

scanpipe/models.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,22 +4191,34 @@ def get_slack_payload(pipeline_run):
41914191
pipeline_name = pipeline_run.pipeline_name
41924192
pretext = f"Project *{project_display}* update:"
41934193
text = f"Pipeline `{pipeline_name}` completed with {status}."
4194+
blocks = [
4195+
{
4196+
"type": "section",
4197+
"text": {
4198+
"type": "mrkdwn",
4199+
"text": text,
4200+
},
4201+
}
4202+
]
4203+
4204+
# Adds the task output in case of run failure
4205+
if pipeline_run.status == Run.Status.FAILURE and pipeline_run.task_output:
4206+
task_output_block = {
4207+
"type": "section",
4208+
"text": {
4209+
"type": "mrkdwn",
4210+
"text": f"```{pipeline_run.task_output}```",
4211+
},
4212+
}
4213+
blocks.append(task_output_block)
41944214

41954215
return {
41964216
"username": "ScanCode.io",
41974217
"text": pretext,
41984218
"attachments": [
41994219
{
42004220
"color": color,
4201-
"blocks": [
4202-
{
4203-
"type": "section",
4204-
"text": {
4205-
"type": "mrkdwn",
4206-
"text": text,
4207-
},
4208-
}
4209-
],
4221+
"blocks": blocks,
42104222
}
42114223
],
42124224
}

scanpipe/tests/test_models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,17 @@ def test_scanpipe_webhook_subscription_model_get_slack_payload(self):
22322232
payload = WebhookSubscription.get_slack_payload(run1)
22332233
self.assertDictEqual(expected_payload, payload)
22342234

2235+
run1.set_task_ended(exitcode=1, output="Exception")
2236+
self.assertEqual(Run.Status.FAILURE, run1.status)
2237+
payload = WebhookSubscription.get_slack_payload(run1)
2238+
payload_blocks = payload["attachments"][0]["blocks"]
2239+
self.assertEqual(2, len(payload_blocks))
2240+
expected_task_output_block = {
2241+
"text": {"text": "```Exception```", "type": "mrkdwn"},
2242+
"type": "section",
2243+
}
2244+
self.assertEqual(expected_task_output_block, payload_blocks[1])
2245+
22352246
def test_scanpipe_discovered_package_model_extract_purl_data(self):
22362247
package_data = {}
22372248
expected = {

0 commit comments

Comments
 (0)