Skip to content

Commit

Permalink
fix invalid slack attachments (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz authored Oct 12, 2023
1 parent c077cc6 commit dd59c00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
31 changes: 20 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ import (
"github.com/slack-go/slack"
)

const jql = `project in (ROX)
const (
jql = `project in (ROX)
AND issuetype = Bug
AND status != Closed
AND labels = CI_Failure
AND summary ~ %q
ORDER BY created DESC`
// Slack has a 150-character limit for text header
slackHeaderTextLengthLimit = 150
// Slack has a 3000-character limit for (non-field) text objects
slackTextLengthLimit = 3000
)

func main() {
var debug bool
Expand Down Expand Up @@ -639,10 +645,11 @@ func convertJunitToSlack(issues ...*testIssue) []slack.Attachment {

issue := i.issue
if issue != nil {
title = fmt.Sprintf("[**%s**](%s): %s", issue.Key, issue.Self, title)
title = fmt.Sprintf("%s: %s", issue.Key, title)
}
title = crop(title, slackHeaderTextLengthLimit)

titleTextBlock := slack.NewTextBlockObject("mrkdwn", title, false, false)
titleTextBlock := slack.NewTextBlockObject("plain_text", title, false, false)
titleSectionBlock := slack.NewSectionBlock(titleTextBlock, nil, nil)
failedTestsBlocks = append(failedTestsBlocks, titleSectionBlock)

Expand Down Expand Up @@ -691,16 +698,11 @@ func failureToAttachment(title string, tc testCase) (slack.Attachment, error) {
return slack.Attachment{}, fmt.Errorf("no junit failure message or error for %s", title)
}

// Slack has a 3000-character limit for (non-field) text objects
if len(failureMessage) > 3000 {
failureMessage = failureMessage[:3000]
}
if len(failureValue) > 3000 {
failureValue = failureValue[:3000]
}
failureMessage = crop(failureMessage, slackTextLengthLimit)
failureValue = crop(failureValue, slackTextLengthLimit)

// Add some formatting to the failure title
failureTitleTextBlock := slack.NewTextBlockObject("mrkdwn", title, false, false)
failureTitleTextBlock := slack.NewTextBlockObject("plain_text", title, false, false)
failureTitleHeaderBlock := slack.NewHeaderBlock(failureTitleTextBlock)

failureAttachment := slack.Attachment{
Expand Down Expand Up @@ -757,3 +759,10 @@ func failureToBlocks(failureTitleHeaderBlock *slack.HeaderBlock, messageText, va
failureValueSectionBlock,
}}
}

func crop(s string, l int) string {
if len(s) < l {
return s
}
return s[:l-1] + "…"
}
8 changes: 4 additions & 4 deletions testdata/slack/combined-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "[**FOO-1**](some/url/foo-1): MyTest: My Test Case 3"
"type": "plain_text",
"text": "FOO-1: MyTest: My Test Case 3"
}
}
]
Expand All @@ -24,8 +24,8 @@
{
"type": "header",
"text": {
"type": "mrkdwn",
"text": "[**FOO-1**](some/url/foo-1): MyTest: My Test Case 3"
"type": "plain_text",
"text": "FOO-1: MyTest: My Test Case 3"
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions testdata/slack/message-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "[**FOO-1**](some/url/foo-1): MyTest: My Test Case 3"
"type": "plain_text",
"text": "FOO-1: MyTest: My Test Case 3"
}
}
]
Expand All @@ -24,8 +24,8 @@
{
"type": "header",
"text": {
"type": "mrkdwn",
"text": "[**FOO-1**](some/url/foo-1): MyTest: My Test Case 3"
"type": "plain_text",
"text": "FOO-1: MyTest: My Test Case 3"
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions testdata/slack/value-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "[**FOO-1**](some/url/foo-1): MyTest: My Test Case 3"
"type": "plain_text",
"text": "FOO-1: MyTest: My Test Case 3"
}
}
]
Expand All @@ -24,8 +24,8 @@
{
"type": "header",
"text": {
"type": "mrkdwn",
"text": "[**FOO-1**](some/url/foo-1): MyTest: My Test Case 3"
"type": "plain_text",
"text": "FOO-1: MyTest: My Test Case 3"
}
},
{
Expand Down

0 comments on commit dd59c00

Please sign in to comment.