Skip to content

Commit

Permalink
feat: silence button for slack notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 18, 2024
1 parent 9b6ea03 commit a5f441d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
24 changes: 21 additions & 3 deletions notification/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notification

import (
"errors"
"fmt"

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
Expand All @@ -24,8 +25,24 @@ type celVariables struct {
Comment *models.Comment
Author *models.Person

NewState string
Permalink string
NewState string
Permalink string
SilenceURL string
}

func (t *celVariables) SetSilenceURL(frontendURL string) {
baseURL := fmt.Sprintf("%s/settings/notifications/silence", frontendURL)

switch {
case t.ConfigItem != nil:
t.SilenceURL = fmt.Sprintf("%s?config_id=%s", baseURL, t.ConfigItem.ID.String())
case t.Component != nil:
t.SilenceURL = fmt.Sprintf("%s?component_id=%s", baseURL, t.Component.ID.String())
case t.Check != nil:
t.SilenceURL = fmt.Sprintf("%s?check_id=%s", baseURL, t.Check.ID.String())
case t.Canary != nil:
t.SilenceURL = fmt.Sprintf("%s?canary_id=%s", baseURL, t.Canary.ID.String())
}
}

func (t *celVariables) GetResourceHealth(ctx context.Context) (models.Health, error) {
Expand All @@ -48,7 +65,8 @@ func (t *celVariables) GetResourceHealth(ctx context.Context) (models.Health, er

func (t *celVariables) AsMap() map[string]any {
output := map[string]any{
"permalink": t.Permalink,
"permalink": t.Permalink,
"silenceURL": t.SilenceURL,
}

if t.Agent != nil {
Expand Down
1 change: 1 addition & 0 deletions notification/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,6 @@ func getEnvForEvent(ctx context.Context, event models.Event, properties map[stri
env.Permalink = fmt.Sprintf("%s/catalog/%s", api.FrontendURL, configID)
}

env.SetSilenceURL(api.FrontendURL)
return &env, nil
}
29 changes: 21 additions & 8 deletions notification/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,21 @@ var templateFuncs = map[string]any{
}
}`, text)
},
"slackURLAction": func(name, url string) string {
return fmt.Sprintf(`{
"type": "actions",
"elements": [
"slackURLAction": func(val ...string) string {
if len(val)%2 != 0 {
return "slackURLAction received an uneven pair of the action name and url"
}

var elements []string
for i, pair := range lo.Chunk(val, 2) {
name, url := pair[0], pair[1]

var buttonStyle string
if i == 0 {
buttonStyle = "primary"
}

elements = append(elements, fmt.Sprintf(`
{
"type": "button",
"text": {
Expand All @@ -151,9 +162,11 @@ var templateFuncs = map[string]any{
"emoji": true
},
"url": "%s",
"action_id": "%s"
}
]
}`, name, url, name)
"action_id": "%s",
"style": "%s"
}`, name, url, name, buttonStyle))
}

return fmt.Sprintf(`{"type": "actions", "elements": [%s]}`, strings.Join(elements, ","))
},
}
2 changes: 1 addition & 1 deletion notification/templates/check.failed
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
{{ if .check.labels}}{{slackSectionLabels .check}},{{end}}
{{ slackURLAction "View Health Check" .permalink}}
{{ slackURLAction "View Health Check" .permalink "🔕 Silence" .silenceURL}}
]
}
{{ else }}
Expand Down
2 changes: 1 addition & 1 deletion notification/templates/check.passed
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
{{ if .check.labels}}{{slackSectionLabels .check}},{{end}}
{{ slackURLAction "View Health Check" .permalink}}
{{ slackURLAction "View Health Check" .permalink "🔕 Silence" .silenceURL}}
]
}
{{ else }}
Expand Down
2 changes: 1 addition & 1 deletion notification/templates/component.health
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
{{if .component.labels}}{{slackSectionLabels .component}},{{end}}
{{slackURLAction "View Component" .permalink}}
{{slackURLAction "View Component" .permalink "🔕 Silence" .silenceURL}}
]
}

Expand Down
2 changes: 1 addition & 1 deletion notification/templates/config.db.update
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
{{if .config.labels}}{{slackSectionLabels .config}},{{end}}
{{slackURLAction "View Catalog" .permalink}}
{{slackURLAction "View Catalog" .permalink "🔕 Silence" .silenceURL}}
]
}

Expand Down
2 changes: 1 addition & 1 deletion notification/templates/config.health
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
{{if .config.labels}}{{slackSectionLabels .config}},{{end}}
{{slackURLAction "View Catalog" .permalink}}
{{slackURLAction "View Catalog" .permalink "🔕 Silence" .silenceURL}}
]
}

Expand Down

0 comments on commit a5f441d

Please sign in to comment.