Skip to content

Commit

Permalink
Removes ticks when error message is empty (#382)
Browse files Browse the repository at this point in the history
* rm ticks if error msg is empty

* rm logs

* more ticks
  • Loading branch information
MariaCFFrandsen authored Jul 4, 2022
1 parent afcd428 commit 2224385
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
14 changes: 10 additions & 4 deletions internal/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ func (c *Client) NotifySlackPolicyFailed(ctx context.Context, email, title, erro
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: title,
Color: MsgColorRed,
Text: fmt.Sprintf("```%s```", errorMessage),
Text: formatErrorMessage(errorMessage),
MarkdownIn: []string{"text", "fields"},
})

_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
Expand Down Expand Up @@ -299,7 +298,7 @@ func (c *Client) NotifyK8SPodErrorEvent(ctx context.Context, event *http.PodErro
for _, container := range event.Errors {
fields = append(fields, slack.AttachmentField{
Title: fmt.Sprintf("Container: %s (%s)", container.Name, container.Type),
Value: fmt.Sprintf("```%s```", container.ErrorMessage),
Value: formatErrorMessage(container.ErrorMessage),
Short: false,
})
}
Expand Down Expand Up @@ -330,7 +329,7 @@ func (c *Client) NotifyK8SJobErrorEvent(ctx context.Context, event *http.JobErro
for _, condition := range event.Errors {
fields = append(fields, slack.AttachmentField{
Title: fmt.Sprintf("Reason: %s", condition.Reason),
Value: fmt.Sprintf("```%s```", condition.Message),
Value: formatErrorMessage(condition.Message),
Short: false,
})
}
Expand Down Expand Up @@ -383,3 +382,10 @@ func generateSlackMessage(msgType, service, environment, branch, namespace strin
return fmt.Sprintf("Failed handling event in release manager for:\nEvent: %s\nService: %s\nEnvironment: %s\nBranch: %s\nNamespace: %s\nError: %s", msgType, service, environment, branch, namespace, err)
}
}

func formatErrorMessage(errorMessage string) string {
if errorMessage == "" { //error message is only formatted if there is one
return errorMessage
}
return fmt.Sprintf("```%s```", errorMessage)
}
32 changes: 17 additions & 15 deletions internal/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ func TestNewMutableClient_mapping(t *testing.T) {
}
)

slackClient := intslack.MockSlackClient{}
slackClient.Test(t)
slackClient.On("PostMessage", slackUser.ID, mock.Anything, mock.Anything).Return("", "", nil)
// return error when looking up non-corp email
slackClient.On("GetUserByEmailContext", mock.Anything, email).Return(&slack.User{}, errors.New("users_not_found"))
// return slack user on anything else
slackClient.On("GetUserByEmailContext", mock.Anything, mock.Anything).Return(slackUser, nil)
t.Run("domain verification", func(t *testing.T) {
slackClient := intslack.MockSlackClient{}
slackClient.Test(t)
slackClient.On("PostMessage", slackUser.ID, mock.Anything, mock.Anything).Return("", "", nil)
// return error when looking up non-corp email
slackClient.On("GetUserByEmailContext", mock.Anything, email).Return(&slack.User{}, errors.New("users_not_found"))
// return slack user on anything else
slackClient.On("GetUserByEmailContext", mock.Anything, mock.Anything).Return(slackUser, nil)

client, err := intslack.NewMuteableClient(&slackClient, mapping, emailDomain, intslack.MuteOptions{})
if !assert.NoError(t, err, "unexpected instantiation error") {
return
}
client, err := intslack.NewMuteableClient(&slackClient, mapping, emailDomain, intslack.MuteOptions{})
if !assert.NoError(t, err, "unexpected instantiation error") {
return
}

_, _, err = client.PostSlackBuildStarted(email, "title", "titleLink", "text", "color")
if !assert.NoError(t, err, "unexpected instantiation error") {
return
}
_, _, err = client.PostSlackBuildStarted(email, "title", "titleLink", "text", "color")
if !assert.NoError(t, err, "unexpected instantiation error") {
return
}
})
}

0 comments on commit 2224385

Please sign in to comment.