Skip to content

Commit 3a0f35a

Browse files
committed
fix
Signed-off-by: Mmadu Manasseh <manasseh.mmadu@zapier.com>
1 parent 61d0732 commit 3a0f35a

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

pkg/msg/message.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ func (m *Message) BuildComment(
196196
// Footer warning for split comments
197197
splitCommentFooter := "\n\n> **Warning**: Output length greater than maximum allowed comment size. Continued in next comment"
198198
// Header for continued comments
199+
// this is written from the VCS PostMessage/UpdateMessage function. So, we need to account for it here
199200
continuedHeader := fmt.Sprintf("%s> Continued from previous [comment](%s)\n\n", header, prLinkTemplate)
200201
// Max content length for each chunk (accounting for continuedHeader)
201202
maxContentLength := maxCommentLength - len(continuedHeader)
@@ -211,13 +212,12 @@ func (m *Message) BuildComment(
211212
// and starting new chunks with continuedHeader after the first
212213
appendChunk := func(appHeader string) {
213214
if sb.Len() > 0 {
214-
currentContent := sb.String()
215215
// Only add splitCommentFooter if there's enough space
216-
if len(currentContent)+len(splitCommentFooter) <= maxContentLength {
216+
if (contentLength)+len(splitCommentFooter) <= maxContentLength {
217217
sb.WriteString(splitCommentFooter)
218218
} else {
219219
// Create a truncated copy of splitCommentFooter to make room for it
220-
availableSpace := maxContentLength - len(currentContent)
220+
availableSpace := maxContentLength - contentLength
221221
if availableSpace > 0 {
222222
truncatedFooter := splitCommentFooter[:availableSpace]
223223
sb.WriteString(truncatedFooter)
@@ -226,11 +226,11 @@ func (m *Message) BuildComment(
226226
comments = append(comments, sb.String())
227227
sb.Reset()
228228
sb.WriteString(header)
229-
sb.WriteString(appHeader)
230229
// continuedHeader contains both the header and the comment about the continued fromprevious comment
231230
// header is written here but the rest of the content is written from the vcs PostMessage/UpdateMessage function
232231
// that's why we're setting the contentLength to the length of the continuedHeader
233232
contentLength = len(continuedHeader)
233+
sb.WriteString(appHeader)
234234
contentLength += len(appHeader)
235235

236236
}
@@ -341,13 +341,16 @@ func (m *Message) BuildComment(
341341
}
342342
}
343343

344-
// Don't write if there's no more apps to write. An unclosed details tag wouldn't cause an issue
344+
// Don't split if there's no more apps to write. An unclosed details tag wouldn't cause an issue
345345
// unless there's more contents to write
346+
closingDetailsTag := "\n</details>\n"
346347
if appIndex < len(names)-1 {
347-
closingDetailsTag := "\n</details>\n"
348348
if contentLength+len(closingDetailsTag) > maxContentLength {
349349
appendChunk(appHeader)
350350
} // Close the app details block
351+
}
352+
// if there's space to write the closingTag, write it
353+
if contentLength+len(closingDetailsTag) < maxContentLength {
351354
sb.WriteString(closingDetailsTag)
352355
contentLength += len(closingDetailsTag)
353356
}

pkg/msg/message_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ func TestBuildComment_Deep(t *testing.T) {
414414
if strings.Contains(comments[i], "> **Warning**: Output length greater than maximum allowed comment size. Continued in next comment") {
415415
foundSplitWarnings++
416416
}
417+
assert.Less(t, len(comments[i]), 500)
417418
}
418419
assert.Greater(t, foundSplitWarnings, 0, "Should have at least one split warning")
419420
// Last comment should have footer
@@ -552,7 +553,7 @@ func TestBuildComment_Deep(t *testing.T) {
552553
m := NewMessage("test", 1, 2, fakeVCS)
553554
m.AddNewApp(ctx, "small-limit-app")
554555
m.AddToAppMessage(ctx, "small-limit-app", Result{State: pkg.StateSuccess, Summary: "test summary", Details: "test details"})
555-
comments := m.BuildComment(ctx, time.Now(), "sha", "", false, "id", 1, 1, 100, "prlink")
556+
comments := m.BuildComment(ctx, time.Now(), "sha", "", false, "id", 1, 1, 200, "prlink")
556557
require.Greater(t, len(comments), 1)
557558
// Should have multiple comments due to very small limit
558559
assert.Contains(t, comments[0], "# Kubechecks id Report")
@@ -569,6 +570,9 @@ func TestBuildComment_Deep(t *testing.T) {
569570
if strings.Contains(comment, "small-limit-app") {
570571
foundAppName = true
571572
}
573+
// With the current implementation, comments can be up to maxCommentLength (100)
574+
// Allow for some flexibility in the splitting logic
575+
assert.LessOrEqual(t, len(comment), 200, "Comment should not exceed maxCommentLength")
572576
if strings.Contains(comment, "test summary") {
573577
foundSummary = true
574578
}
@@ -1147,6 +1151,24 @@ func TestSplitContentPreservingCodeBlocks_Debug(t *testing.T) {
11471151
t.Logf("Inside code block: %v", codeBlockMarkers%2 == 1)
11481152
}
11491153

1154+
func TestBuildComment_NoTrailingDetailsTag(t *testing.T) {
1155+
m := NewMessage("test", 1, 2, fakeEmojiable{":ok:"})
1156+
ctx := context.TODO()
1157+
m.AddNewApp(ctx, "app1")
1158+
m.AddToAppMessage(ctx, "app1", Result{State: pkg.StateSuccess, Summary: "all good", Details: "details"})
1159+
comments := m.BuildComment(ctx, time.Now(), "sha", "", false, "id", 1, 1, 1000, "prlink")
1160+
require.Len(t, comments, 1)
1161+
output := comments[0]
1162+
1163+
// The output should not end with </details> followed by the footer
1164+
footer := "<small> _Done. CommitSHA: sha_ <small>"
1165+
assert.True(t, strings.HasSuffix(output, footer+"\n"), "Output should end with the footer")
1166+
// There should not be a trailing </details> before the footer
1167+
lastDetailsIdx := strings.LastIndex(output, "</details>")
1168+
footerIdx := strings.LastIndex(output, footer)
1169+
assert.True(t, lastDetailsIdx < footerIdx, "No trailing </details> after last app block before footer")
1170+
}
1171+
11501172
// Helper function for min
11511173
func min(a, b int) int {
11521174
if a < b {

0 commit comments

Comments
 (0)