Adding rocket.chat alerts support (similar to slack)

This commit is contained in:
Oussama Jarrousse 2024-03-15 18:20:54 +01:00
parent 26f609b3a5
commit 2bb8114464
5 changed files with 94 additions and 1 deletions

View File

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

View File

@ -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()
}
}

View File

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

View File

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

View File

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