Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func InteractiveSetup(conf *util.ConfigType) {
askValue("Slack Webhook URL", "", &conf.SlackUrl)
}

askConfirmation("Enable discord alerts?", false, &conf.DiscordAlert)
if conf.DiscordAlert {
askValue("Discord Webhook URL", "", &conf.DiscordUrl)
}

askConfirmation("Enable Rocket.Chat alerts?", false, &conf.RocketChatAlert)
if conf.RocketChatAlert {
askValue("Rocket.Chat Webhook URL", "", &conf.RocketChatUrl)
Expand Down
1 change: 1 addition & 0 deletions services/tasks/TaskRunner_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) {
t.sendMicrosoftTeamsAlert()
t.sendDingTalkAlert()
t.sendGotifyAlert()
t.sendDiscordAlert()
}

for _, l := range t.statusListeners {
Expand Down
63 changes: 63 additions & 0 deletions services/tasks/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,69 @@ func (t *TaskRunner) sendGotifyAlert() {
}
}

func (t *TaskRunner) sendDiscordAlert() {
if !util.Config.DiscordAlert || !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("discord"),
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/discord.tmpl")

if err != nil {
t.Log("Can't parse discord alert template!")
panic(err)
}

if err := tpl.Execute(body, alert); err != nil {
t.Log("Can't generate discord alert template!")
panic(err)
}

if body.Len() == 0 {
t.Log("Buffer for discord alert is empty")
return
}

t.Log("Attempting to send discord alert")

resp, err := http.Post(
util.Config.DiscordUrl,
"application/json",
body,
)

if err != nil {
t.Log("Can't send discord alert! Error: " + err.Error())
} else if resp.StatusCode != 200 {
t.Log("Can't send discord alert! Response code: " + strconv.Itoa(resp.StatusCode))
} else {
t.Log("Sent successfully discord alert")
}

if resp != nil {
defer resp.Body.Close() //nolint:errcheck
}
}

func (t *TaskRunner) alertInfos() (string, string) {
version := ""

Expand Down
1 change: 1 addition & 0 deletions services/tasks/alert_test_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func SendProjectTestAlerts(project db.Project, store db.Store) (err error) {
tr.sendMicrosoftTeamsAlert()
tr.sendDingTalkAlert()
tr.sendGotifyAlert()
tr.sendDiscordAlert()
tr.sendMailAlert()

return
Expand Down
24 changes: 24 additions & 0 deletions services/tasks/templates/discord.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"username": "Semaphore",
"content": "",
"embeds": [{
"title": "Task: {{ .Name }}",
"url": "{{ .Task.URL }}",
"description": "execution #{{ .Task.ID }}, status: {{ .Task.Result }}!",
"color": "{{ .Color }}",
"fields": [
{
"name": "Author",
"value": "{{ .Author }}",
"inline": true
{{ if .Task.Version }}
},
{
"name": "Version",
"value": "{{ .Task.Version }}",
"inline": true
{{ end }}
}
]
}]
}
4 changes: 3 additions & 1 deletion util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,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, DingTalk, and Gotify alerting
// Telegram, Slack, Rocket.Chat, Microsoft Teams, DingTalk, Gotify and Discord 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"`
Expand All @@ -293,6 +293,8 @@ type ConfigType struct {
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"`
DiscordAlert bool `json:"discord_alert,omitempty" env:"SEMAPHORE_DISCORD_ALERT"`
DiscordUrl string `json:"discord_url,omitempty" env:"SEMAPHORE_DISCORD_URL"`

// oidc settings
OidcProviders map[string]OidcProvider `json:"oidc_providers,omitempty" env:"SEMAPHORE_OIDC_PROVIDERS"`
Expand Down
Loading