Skip to content

Commit

Permalink
Merge pull request #2363 from shaxiaozz/shaxiaozz-feat-dingtalk-alert
Browse files Browse the repository at this point in the history
feat: Add dingtalk webhook notification medium
  • Loading branch information
fiftin authored Sep 27, 2024
2 parents 3056640 + 41b682f commit ae3f878
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func InteractiveSetup(conf *util.ConfigType) {
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 {
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 @@ -105,6 +105,7 @@ func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) {
t.sendSlackAlert()
t.sendRocketChatAlert()
t.sendMicrosoftTeamsAlert()
t.sendDingTalkAlert()
}

for _, l := range t.statusListeners {
Expand Down
59 changes: 59 additions & 0 deletions services/tasks/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,65 @@ func (t *TaskRunner) sendMicrosoftTeamsAlert() {
t.Log("Sent successfully microsoft teams alert")
}

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

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

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

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

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

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

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

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

Expand Down
7 changes: 7 additions & 0 deletions services/tasks/templates/dingtalk.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"msgtype": "markdown",
"markdown": {
"title": "Task: {{ .Name }}",
"text": "#### Task: {{ .Name }}\nExecution #: {{ .Task.ID }} \nStatus: {{ .Task.Result }} \nAuthor: {{ .Author }} \n{{ if .Task.Version }}Version: {{ .Task.Version }} \n{{ end }}[Task Link]({{ .Task.URL }})"
}
}
4 changes: 3 additions & 1 deletion util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type ConfigType struct {
LdapMappings ldapMappings `json:"ldap_mappings"`
LdapNeedTLS bool `json:"ldap_needtls" env:"SEMAPHORE_LDAP_NEEDTLS"`

// Telegram, Slack, Rocket.Chat and Microsoft Teams alerting
// Telegram, Slack, Rocket.Chat, Microsoft Teams and DingTalk 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"`
Expand All @@ -171,6 +171,8 @@ type ConfigType struct {
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"`
DingTalkAlert bool `json:"dingtalk_alert" env:"SEMAPHORE_DINGTALK_ALERT"`
DingTalkUrl string `json:"dingtalk_url" env:"SEMAPHORE_DINGTALK_URL"`

// oidc settings
OidcProviders map[string]OidcProvider `json:"oidc_providers"`
Expand Down

0 comments on commit ae3f878

Please sign in to comment.