From 8fdfcdce2c17b99ec97a0b1c5ae5e516f98c71ca Mon Sep 17 00:00:00 2001 From: Thomas J Leach Date: Wed, 16 Oct 2024 08:59:18 -0400 Subject: [PATCH] feat: Add Gotify alerts --- services/tasks/TaskRunner_logging.go | 1 + services/tasks/alert.go | 62 ++++++++++++++++++++++++++++ services/tasks/templates/gotify.tmpl | 9 ++++ util/config.go | 5 ++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 services/tasks/templates/gotify.tmpl diff --git a/services/tasks/TaskRunner_logging.go b/services/tasks/TaskRunner_logging.go index e4924e69..f1d99c8c 100644 --- a/services/tasks/TaskRunner_logging.go +++ b/services/tasks/TaskRunner_logging.go @@ -106,6 +106,7 @@ func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) { t.sendRocketChatAlert() t.sendMicrosoftTeamsAlert() t.sendDingTalkAlert() + t.sendGotifyAlert() } for _, l := range t.statusListeners { diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 9373e371..7d8b75b3 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -418,6 +418,68 @@ func (t *TaskRunner) sendDingTalkAlert() { } } +func (t *TaskRunner) sendGotifyAlert() { + if !util.Config.GotifyAlert || !t.alert { + return + } + + if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus { + return + } + + body := bytes.NewBufferString("") + author, version := t.alertInfos() + + alert := Alert{ + Name: t.Template.Name, + Author: author, + Color: t.alertColor("gotify"), + Task: alertTask{ + ID: strconv.Itoa(t.Task.ID), + URL: t.taskLink(), + Result: t.Task.Status.Format(), + Version: version, + Desc: t.Task.Message, + }, + } + + tpl, err := template.ParseFS(templates, "templates/gotify.tmpl") + + if err != nil { + t.Log("Can't parse gotify alert template!") + panic(err) + } + + if err := tpl.Execute(body, alert); err != nil { + t.Log("Can't generate gotify alert template!") + panic(err) + } + + if body.Len() == 0 { + t.Log("Buffer for gotify alert is empty") + return + } + + t.Log("Attempting to send gotify alert") + + resp, err := http.Post( + fmt.Sprintf( + "%s/message?token=%s", + util.Config.GotifyUrl, + util.Config.GotifyToken), + "application/json", + body, + ) + + if err != nil { + t.Log("Can't send gotify alert! Error: " + err.Error()) + } else if resp.StatusCode != 200 { + t.Log("Can't send gotify alert! Response code: " + strconv.Itoa(resp.StatusCode)) + } else { + t.Log("Sent successfully gotify alert") + } +} + func (t *TaskRunner) alertInfos() (string, string) { version := "" diff --git a/services/tasks/templates/gotify.tmpl b/services/tasks/templates/gotify.tmpl new file mode 100644 index 00000000..c3483058 --- /dev/null +++ b/services/tasks/templates/gotify.tmpl @@ -0,0 +1,9 @@ +{ + "extras": { + "client::display": { + "contentType": "text/markdown" + } + }, + "message": "Execution #: {{ .Task.ID }} \nStatus: {{ .Task.Result }} \nAuthor: {{ .Author }} \n{{ if .Task.Version }}Version: {{ .Task.Version }} \n{{ end }}[Task Link]({{ .Task.URL }})", + "title": "Task: {{ .Name }}" +} \ No newline at end of file diff --git a/util/config.go b/util/config.go index 3b0f9d49..1c92ca7b 100644 --- a/util/config.go +++ b/util/config.go @@ -166,7 +166,7 @@ type ConfigType struct { LdapMappings *ldapMappings `json:"ldap_mappings,omitempty"` LdapNeedTLS bool `json:"ldap_needtls,omitempty" env:"SEMAPHORE_LDAP_NEEDTLS"` - // Telegram, Slack, Rocket.Chat, Microsoft Teams and DingTalk alerting + // Telegram, Slack, Rocket.Chat, Microsoft Teams, DingTalk, and Gotify alerting TelegramAlert bool `json:"telegram_alert,omitempty" env:"SEMAPHORE_TELEGRAM_ALERT"` TelegramChat string `json:"telegram_chat,omitempty" env:"SEMAPHORE_TELEGRAM_CHAT"` TelegramToken string `json:"telegram_token,omitempty" env:"SEMAPHORE_TELEGRAM_TOKEN"` @@ -178,6 +178,9 @@ type ConfigType struct { MicrosoftTeamsUrl string `json:"microsoft_teams_url,omitempty" env:"SEMAPHORE_MICROSOFT_TEAMS_URL"` DingTalkAlert bool `json:"dingtalk_alert,omitempty" env:"SEMAPHORE_DINGTALK_ALERT"` DingTalkUrl string `json:"dingtalk_url,omitempty" env:"SEMAPHORE_DINGTALK_URL"` + GotifyAlert bool `json:"gotify_alert,omitempty" env:"SEMAPHORE_GOTIFY_ALERT"` + GotifyUrl string `json:"gotify_url,omitempty" env:"SEMAPHORE_GOTIFY_URL"` + GotifyToken string `json:"gotify_token,omitempty" env:"SEMAPHORE_GOTIFY_TOKEN"` // oidc settings OidcProviders map[string]OidcProvider `json:"oidc_providers,omitempty"`