From d86fb48b9713ffd9a0ed05db33bff20c32dfd167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Tosser?= Date: Tue, 19 Apr 2022 16:32:03 +0400 Subject: [PATCH] Improve slack messages --- services/tasks/alert.go | 18 +++++++++++++----- services/tasks/runner.go | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 7ae42309..4e7e233a 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -3,11 +3,11 @@ package tasks import ( "bytes" "github.com/ansible-semaphore/semaphore/db" + "github.com/ansible-semaphore/semaphore/util" "html/template" "net/http" "strconv" "strings" - "github.com/ansible-semaphore/semaphore/util" ) const emailTemplate = `Subject: Task '{{ .Name }}' failed @@ -17,7 +17,7 @@ Task log: {{ .TaskURL }}` const telegramTemplate = `{"chat_id": "{{ .ChatID }}","parse_mode":"HTML","text":"{{ .Name }}\n#{{ .TaskID }} {{ .TaskResult }} {{ .TaskVersion }} {{ .TaskDescription }}\nby {{ .Author }}\n{{ .TaskURL }}"}` -const slackTemplate = `{ "attachments": [ { "title": "{{ .Name }}", "title_link": "{{ .TaskURL }}", "text": "execution ID #{{ .TaskID }} was a {{ .TaskResult }}!", "color": "{{ .Color }}", "mrkdwn_in": ["text"], "fields": [ { "title": "Author", "value": "{{ .Author }}", "short": true }] } ]}` +const slackTemplate = `{ "attachments": [ { "title": "Task: {{ .Name }}", "title_link": "{{ .TaskURL }}", "text": "execution ID #{{ .TaskID }}, status: {{ .TaskResult }}!", "color": "{{ .Color }}", "mrkdwn_in": ["text"], "fields": [ { "title": "Author", "value": "{{ .Author }}", "short": true }] } ]}` // Alert represents an alert that will be templated and sent to the appropriate service type Alert struct { @@ -29,7 +29,7 @@ type Alert struct { TaskDescription string TaskVersion string Author string - Color string + Color string } func (t *TaskRunner) sendMailAlert() { @@ -181,8 +181,16 @@ func (t *TaskRunner) sendSlackAlert() { var color string if t.task.Status == db.TaskSuccessStatus { color = "good" - } else { + } else if t.task.Status == db.TaskFailStatus { color = "bad" + } else if t.task.Status == db.TaskRunningStatus { + color = "#333CFF" + } else if t.task.Status == db.TaskWaitingStatus { + color = "#FFFC33" + } else if t.task.Status == db.TaskStoppingStatus { + color = "#BEBEBE" + } else if t.task.Status == db.TaskStoppedStatus { + color = "#5B5B5B" } alert := Alert{ TaskID: strconv.Itoa(t.task.ID), @@ -192,7 +200,7 @@ func (t *TaskRunner) sendSlackAlert() { TaskVersion: version, TaskDescription: message, Author: author, - Color: color, + Color: color, } tpl := template.New("slack body template") diff --git a/services/tasks/runner.go b/services/tasks/runner.go index 653aece2..1fd1725a 100644 --- a/services/tasks/runner.go +++ b/services/tasks/runner.go @@ -72,13 +72,14 @@ func (t *TaskRunner) setStatus(status db.TaskStatus) { t.updateStatus() + t.sendSlackAlert() + if status == db.TaskFailStatus { t.sendMailAlert() } if status == db.TaskSuccessStatus || status == db.TaskFailStatus { t.sendTelegramAlert() - t.sendSlackAlert() } }