From eaf13c398dcc2895dc074cfab3b7485d179d9cd3 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Fri, 27 Aug 2021 06:37:18 -0400 Subject: [PATCH] [agent-smioth] Improve report of infringements with long commands --- components/ee/agent-smith/cmd/run.go | 22 +++++++++++++++----- components/ee/agent-smith/pkg/agent/agent.go | 10 +++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/components/ee/agent-smith/cmd/run.go b/components/ee/agent-smith/cmd/run.go index 970d4040de3b72..3d88659e5e1a51 100644 --- a/components/ee/agent-smith/cmd/run.go +++ b/components/ee/agent-smith/cmd/run.go @@ -130,10 +130,11 @@ func startMemoryWatchdog(maxSysMemMib uint64) { func notifySlack(webhook string, hostURL string, ws agent.InfringingWorkspace, penalties []agent.PenaltyKind) error { var ( - region = os.Getenv("GITPOD_REGION") - lblDetails = "Details" - lblActions = "Actions" - lblPenalties = "Penalties" + region = os.Getenv("GITPOD_REGION") + lblDetails = "Details" + lblActions = "Actions" + lblPenalties = "Penalties" + lblInfringements = "Long Infringements details" ) attachments := []slack.Attachment{ @@ -170,8 +171,19 @@ func notifySlack(webhook string, hostURL string, ws agent.InfringingWorkspace, p }, ) + infringements := make([]*slack.Field, len(ws.Infringements)) + for _, v := range ws.Infringements { + infringements = append(infringements, &slack.Field{ + Title: string(v.Kind), Value: v.Description, + }) + } + + attachments = append(attachments, + slack.Attachment{Title: &lblInfringements, Fields: infringements}, + ) + payload := slack.Payload{ - Text: fmt.Sprintf("Agent Smith: %s", ws.DescibeInfringements()), + Text: fmt.Sprintf("Agent Smith: %s", ws.DescribeInfringements(150)), IconEmoji: ":-(", Attachments: attachments, } diff --git a/components/ee/agent-smith/pkg/agent/agent.go b/components/ee/agent-smith/pkg/agent/agent.go index 6411e5d5d6358f..49e97ea21c33bd 100644 --- a/components/ee/agent-smith/pkg/agent/agent.go +++ b/components/ee/agent-smith/pkg/agent/agent.go @@ -227,12 +227,18 @@ func (ws InfringingWorkspace) VID() string { } // DescibeInfringements returns a string representation of all infringements of this workspace -func (ws InfringingWorkspace) DescibeInfringements() string { +func (ws InfringingWorkspace) DescribeInfringements(charCount int) string { res := make([]string, len(ws.Infringements)) for i, v := range ws.Infringements { res[i] = fmt.Sprintf("%s: %s", v.Kind, v.Description) } - return strings.Join(res, "\n") + + infringements := strings.Join(res, "\n") + if len(infringements) > charCount { + infringements = infringements[:charCount] + } + + return infringements } // Infringement reports a users particular wrongdoing