From 2bb811446460eb101e0c853a22d551596691f18a Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Fri, 15 Mar 2024 18:20:54 +0100 Subject: [PATCH] Adding rocket.chat alerts support (similar to slack) --- cli/setup/setup.go | 5 ++ services/tasks/TaskRunner.go | 1 + services/tasks/alert.go | 74 ++++++++++++++++++++++++ services/tasks/templates/rocketchat.tmpl | 11 ++++ util/config.go | 4 +- 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 services/tasks/templates/rocketchat.tmpl diff --git a/cli/setup/setup.go b/cli/setup/setup.go index 039d6ec5..da9790a8 100644 --- a/cli/setup/setup.go +++ b/cli/setup/setup.go @@ -69,6 +69,11 @@ func InteractiveSetup(conf *util.ConfigType) { askValue("Slack Webhook URL", "", &conf.SlackUrl) } + askConfirmation("Enable Rocket.Chat alerts?", false, &conf.RocketChatAlert) + if conf.RocketChatAlert { + askValue("Rocket.Chat Webhook URL", "", &conf.RocketChatUrl) + } + askConfirmation("Enable Microsoft Team Channel alerts?", false, &conf.MicrosoftTeamsAlert) if conf.MicrosoftTeamsAlert { askValue("Microsoft Teams Webhook URL", "", &conf.MicrosoftTeamsUrl) diff --git a/services/tasks/TaskRunner.go b/services/tasks/TaskRunner.go index 30d91a05..04eaaf9d 100644 --- a/services/tasks/TaskRunner.go +++ b/services/tasks/TaskRunner.go @@ -84,6 +84,7 @@ func (t *TaskRunner) SetStatus(status lib.TaskStatus) { if status == lib.TaskSuccessStatus || status == lib.TaskFailStatus { t.sendTelegramAlert() t.sendSlackAlert() + t.sendRocketChatAlert() t.sendMicrosoftTeamsAlert() } } diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 27f0be49..2519e1fa 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -242,6 +242,65 @@ func (t *TaskRunner) sendSlackAlert() { t.Log("Sent successfully slack alert") } +func (t *TaskRunner) sendRocketChatAlert() { + if !util.Config.RocketChatAlert || !t.alert { + return + } + + if t.Template.SuppressSuccessAlerts && t.Task.Status == lib.TaskSuccessStatus { + return + } + + body := bytes.NewBufferString("") + author, version := t.alertInfos() + + alert := Alert{ + Name: t.Template.Name, + Author: author, + Color: t.alertColor("rocketchat"), + Task: alertTask{ + ID: strconv.Itoa(t.Task.ID), + URL: t.taskLink(), + Result: strings.ToUpper(string(t.Task.Status)), + Version: version, + Desc: t.Task.Message, + }, + } + + tpl, err := template.ParseFS(templates, "templates/rocketchat.tmpl") + + if err != nil { + t.Log("Can't parse rocketchat alert template!") + panic(err) + } + + if err := tpl.Execute(body, alert); err != nil { + t.Log("Can't generate rocketchat alert template!") + panic(err) + } + + if body.Len() == 0 { + t.Log("Buffer for rocketchat alert is empty") + return + } + + t.Log("Attempting to send rocketchat alert") + + resp, err := http.Post( + util.Config.RocketChatUrl, + "application/json", + body, + ) + + if err != nil { + t.Log("Can't send rocketchat alert! Error: " + err.Error()) + } else if resp.StatusCode != 200 { + t.Log("Can't send rocketchat alert! Response code: " + strconv.Itoa(resp.StatusCode)) + } + + t.Log("Sent successfully rocketchat alert") +} + func (t *TaskRunner) sendMicrosoftTeamsAlert() { if !util.Config.MicrosoftTeamsAlert || !t.alert { return @@ -344,6 +403,21 @@ func (t *TaskRunner) alertColor(kind string) string { case lib.TaskStoppedStatus: return "#5B5B5B" } + case "rocketchat": + switch t.Task.Status { + case lib.TaskSuccessStatus: + return "#00EE00" + case lib.TaskFailStatus: + return "#EE0000" + case lib.TaskRunningStatus: + return "#333CFF" + case lib.TaskWaitingStatus: + return "#FFFC33" + case lib.TaskStoppingStatus: + return "#BEBEBE" + case lib.TaskStoppedStatus: + return "#5B5B5B" + } } return "" diff --git a/services/tasks/templates/rocketchat.tmpl b/services/tasks/templates/rocketchat.tmpl new file mode 100644 index 00000000..5070e8c6 --- /dev/null +++ b/services/tasks/templates/rocketchat.tmpl @@ -0,0 +1,11 @@ +{ + "text": "execution #{{ .Task.ID }}, status: {{ .Task.Result }}!", + "attachments": [ + { + "title": "Task: {{ .Name }}", + "title_link": "{{ .Task.URL }}", + "text": "execution #{{ .Task.ID }}, status: {{ .Task.Result }}!", + "color": "{{ .Color }}" + } + ] +} diff --git a/util/config.go b/util/config.go index 4d5d7adf..657f71f4 100644 --- a/util/config.go +++ b/util/config.go @@ -163,12 +163,14 @@ type ConfigType struct { LdapMappings ldapMappings `json:"ldap_mappings"` LdapNeedTLS bool `json:"ldap_needtls" env:"SEMAPHORE_LDAP_NEEDTLS"` - // Telegram, Slack and Microsoft Teams alerting + // Telegram, Slack, Rocket.Chat and Microsoft Teams alerting TelegramAlert bool `json:"telegram_alert" env:"SEMAPHORE_TELEGRAM_ALERT"` TelegramChat string `json:"telegram_chat" env:"SEMAPHORE_TELEGRAM_CHAT"` TelegramToken string `json:"telegram_token" env:"SEMAPHORE_TELEGRAM_TOKEN"` SlackAlert bool `json:"slack_alert" env:"SEMAPHORE_SLACK_ALERT"` SlackUrl string `json:"slack_url" env:"SEMAPHORE_SLACK_URL"` + RocketChatAlert bool `json:"rocketchat_alert" env:"SEMAPHORE_ROCKETCHAT_ALERT"` + RocketChatUrl string `json:"rocketchat_url" env:"SEMAPHORE_ROCKETCHAT_URL"` MicrosoftTeamsAlert bool `json:"microsoft_teams_alert" env:"SEMAPHORE_MICROSOFT_TEAMS_ALERT"` MicrosoftTeamsUrl string `json:"microsoft_teams_url" env:"SEMAPHORE_MICROSOFT_TEAMS_URL"`