Skip to content

Commit

Permalink
ensure that header rows are wrapped in double newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Jan 10, 2024
1 parent 2404245 commit 80d4b5d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func buildComment(ctx context.Context, apps map[string]*AppResults) string {
sb.WriteString("<summary>\n\n")
sb.WriteString(fmt.Sprintf("## ArgoCD Application Checks: `%s` %s\n", appName, appState.Emoji()))
sb.WriteString("</summary>\n\n")
sb.WriteString(strings.Join(checkStrings, "---\n"))
sb.WriteString(strings.Join(checkStrings, "\n\n---\n\n"))
sb.WriteString("</details>")
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -92,3 +93,61 @@ func TestMessageIsSuccess(t *testing.T) {
})
}
}

func TestMultipleItemsWithNewlines(t *testing.T) {
var (
message = NewMessage("name", 1, 2)
ctx = context.Background()
)
message.AddNewApp(ctx, "first-app")
message.AddToAppMessage(ctx, "first-app", CheckResult{
State: StateSuccess,
Summary: "summary-1",
Details: "detail-1",
})
message.AddToAppMessage(ctx, "first-app", CheckResult{
State: StateSuccess,
Summary: "summary-2",
Details: "detail-2",
})
message.AddNewApp(ctx, "second-app")
message.AddToAppMessage(ctx, "second-app", CheckResult{
State: StateSuccess,
Summary: "summary-1",
Details: "detail-1",
})
message.AddToAppMessage(ctx, "second-app", CheckResult{
State: StateSuccess,
Summary: "summary-2",
Details: "detail-2",
})
result := message.BuildComment(ctx)

// header rows need double newlines before and after
index := 0
newline := uint8('\n')
for {
index++
foundAt := strings.Index(result[index:], "---")
if foundAt == -1 {
break // couldn't be found, we're done
}
index += foundAt

if index < 2 {
continue // hyphens are at the beginning of the string, we're fine
}

if result[index-1] == '-' || result[index+3] == '-' {
continue // not a triple-hyphen, but a more-than-triple-hyphen, move on
}

// must be preceded by two newlines
assert.Equal(t, newline, result[index-1])
assert.Equal(t, newline, result[index-2])

// must be followed by two newlines
assert.Equal(t, newline, result[index+3])
assert.Equal(t, newline, result[index+4])
}
}

0 comments on commit 80d4b5d

Please sign in to comment.