Skip to content

Commit

Permalink
Merge pull request #1836 from oussjarrousse/issue-1091
Browse files Browse the repository at this point in the history
Adding alerts support for Rocket.Chat; closing Issue-#1091
  • Loading branch information
fiftin committed Mar 20, 2024
2 parents f8572bb + 5dd07de commit 194b909
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import (

func main() {
cmd.Execute()
}
}
5 changes: 5 additions & 0 deletions cli/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions deployment/docker/common/semaphore-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ no
no
no
no
no
${SEMAPHORE_LDAP_ACTIVATED}
EOF

Expand Down
1 change: 1 addition & 0 deletions services/tasks/TaskRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
74 changes: 74 additions & 0 deletions services/tasks/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
Expand Down
11 changes: 11 additions & 0 deletions services/tasks/templates/rocketchat.tmpl
Original file line number Diff line number Diff line change
@@ -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 }}"
}
]
}
4 changes: 3 additions & 1 deletion util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down

0 comments on commit 194b909

Please sign in to comment.