feat: Add Gotify alerts

This commit is contained in:
Thomas J Leach 2024-10-16 08:59:18 -04:00
parent 865d0e5e09
commit 8fdfcdce2c
No known key found for this signature in database
4 changed files with 76 additions and 1 deletions

View File

@ -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 {

View File

@ -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 := ""

View File

@ -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 }}"
}

View File

@ -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"`