From 2d3c687f3aca577f40cdf61dd4aca28402a7dca3 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 4 Sep 2024 15:32:08 +0545 Subject: [PATCH] fix: slack labels in section limit (#1344) * fix: slack labels in section limit * chore: sort labels * feat: accept the entire resoruce in `slackSectionLabels` --- notification/template.go | 87 ++++++++++++++++++++----- notification/templates/check.failed | 2 +- notification/templates/check.passed | 2 +- notification/templates/component.health | 2 +- notification/templates/config.db.update | 2 +- notification/templates/config.health | 2 +- 6 files changed, 74 insertions(+), 23 deletions(-) diff --git a/notification/template.go b/notification/template.go index 9bdbf0233..a635918a5 100644 --- a/notification/template.go +++ b/notification/template.go @@ -3,8 +3,11 @@ package notification import ( "encoding/json" "fmt" + "slices" + "strings" "github.com/flanksource/duty/models" + "github.com/samber/lo" ) var templateFuncs = map[string]any{ @@ -32,31 +35,79 @@ var templateFuncs = map[string]any{ return ":white_circle:" } }, - "slackSectionLabels": func(labels map[string]any) string { - if len(labels) == 0 { + "slackSectionLabels": func(resource map[string]any) string { + if resource == nil { return "" } - var fields []any - for k, v := range labels { - fields = append(fields, map[string]any{ - "type": "mrkdwn", - "text": fmt.Sprintf("*%s*: %s", k, v), - "verbatim": true, - }) + configTags := map[string]any{} + var tagFields []map[string]any + if tagsRaw, ok := resource["tags"]; ok { + if tags, ok := tagsRaw.(map[string]any); ok { + configTags = tags + for k, v := range tags { + tagFields = append(tagFields, map[string]any{ + "type": "mrkdwn", + "text": fmt.Sprintf("*%s*: %s", k, v), + "verbatim": true, + }) + } + } } - var m = map[string]any{ - "type": "section", - "fields": fields, - "text": map[string]any{ - "type": "mrkdwn", - "text": "*Labels*", - }, + var labelFields []map[string]any + if labelsRaw, ok := resource["labels"]; ok { + if labels, ok := labelsRaw.(map[string]any); ok { + for k, v := range labels { + if _, ok := configTags[k]; ok { + continue // Already pulled from tags + } + + labelFields = append(labelFields, map[string]any{ + "type": "mrkdwn", + "text": fmt.Sprintf("*%s*: %s", k, v), + "verbatim": true, + }) + } + } + } + + if len(tagFields) == 0 && len(labelFields) == 0 { + return "" + } + + slices.SortFunc(tagFields, func(a, b map[string]any) int { + return strings.Compare(a["text"].(string), b["text"].(string)) + }) + + slices.SortFunc(labelFields, func(a, b map[string]any) int { + return strings.Compare(a["text"].(string), b["text"].(string)) + }) + + fields := append(tagFields, labelFields...) + + var outputs []string + const maxFieldsPerSection = 10 + for i, chunk := range lo.Chunk(fields, maxFieldsPerSection) { + var m = map[string]any{ + "type": "section", + "fields": chunk, + } + + if i == 0 { + m["text"] = map[string]any{ + "type": "mrkdwn", + "text": "*Labels*", + } + } + + out, err := json.Marshal(m) + if err == nil { + outputs = append(outputs, string(out)) + } } - out, _ := json.Marshal(m) - return string(out) + return strings.Join(outputs, ",") }, "slackSectionTextFieldPlain": func(text string) string { return fmt.Sprintf(`{ diff --git a/notification/templates/check.failed b/notification/templates/check.failed index 44d412be2..7bdc846a5 100644 --- a/notification/templates/check.failed +++ b/notification/templates/check.failed @@ -14,7 +14,7 @@ {{end}} ] }, - {{ if .check.labels}}{{slackSectionLabels .check.labels}},{{end}} + {{ if .check.labels}}{{slackSectionLabels .check}},{{end}} {{ slackURLAction "View Health Check" .permalink}} ] } diff --git a/notification/templates/check.passed b/notification/templates/check.passed index b1bdbcc83..c5ac0e3a9 100644 --- a/notification/templates/check.passed +++ b/notification/templates/check.passed @@ -14,7 +14,7 @@ {{end}} ] }, - {{ if .check.labels}}{{slackSectionLabels .check.labels}},{{end}} + {{ if .check.labels}}{{slackSectionLabels .check}},{{end}} {{ slackURLAction "View Health Check" .permalink}} ] } diff --git a/notification/templates/component.health b/notification/templates/component.health index 8be73c6bd..72e36b0ce 100644 --- a/notification/templates/component.health +++ b/notification/templates/component.health @@ -14,7 +14,7 @@ {{end}} ] }, - {{if .component.labels}}{{slackSectionLabels .component.labels}},{{end}} + {{if .component.labels}}{{slackSectionLabels .component}},{{end}} {{slackURLAction "View Component" .permalink}} ] } diff --git a/notification/templates/config.db.update b/notification/templates/config.db.update index e7a306e23..8d522e147 100644 --- a/notification/templates/config.db.update +++ b/notification/templates/config.db.update @@ -14,7 +14,7 @@ {{end}} ] }, - {{if .config.labels}}{{slackSectionLabels .config.labels}},{{end}} + {{if .config.labels}}{{slackSectionLabels .config}},{{end}} {{slackURLAction "View Catalog" .permalink}} ] } diff --git a/notification/templates/config.health b/notification/templates/config.health index 70f55d41a..56af40be1 100644 --- a/notification/templates/config.health +++ b/notification/templates/config.health @@ -14,7 +14,7 @@ {{end}} ] }, - {{if .config.labels}}{{slackSectionLabels .config.labels}},{{end}} + {{if .config.labels}}{{slackSectionLabels .config}},{{end}} {{slackURLAction "View Catalog" .permalink}} ] }