Skip to content

Commit 0513990

Browse files
committed
Update BuildComment to use BuildCommentParams
Signed-off-by: Mmadu Manasseh <manasseh.mmadu@zapier.com>
1 parent 620972f commit 0513990

File tree

3 files changed

+439
-49
lines changed

3 files changed

+439
-49
lines changed

pkg/events/check.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,18 @@ func (ce *CheckEvent) Process(ctx context.Context) error {
325325

326326
ce.logger.Info().Msg("Finished")
327327

328-
comments := ce.vcsNote.BuildComment(
329-
ctx, start, ce.pullRequest.SHA, ce.ctr.Config.LabelFilter,
330-
ce.ctr.Config.ShowDebugInfo, ce.ctr.Config.Identifier,
331-
len(ce.addedAppsSet), int(ce.appsSent),
332-
ce.ctr.VcsClient.GetMaxCommentLength(),
333-
ce.ctr.VcsClient.GetPrCommentLinkTemplate(ce.pullRequest),
334-
)
328+
buildCommentParams := msg.BuildCommentParams{
329+
Start: start,
330+
CommitSHA: ce.pullRequest.SHA,
331+
LabelFilter: ce.ctr.Config.LabelFilter,
332+
ShowDebugInfo: ce.ctr.Config.ShowDebugInfo,
333+
Identifier: ce.ctr.Config.Identifier,
334+
AppsChecked: len(ce.addedAppsSet),
335+
TotalChecked: int(ce.appsSent),
336+
MaxCommentLength: ce.ctr.VcsClient.GetMaxCommentLength(),
337+
PrLinkTemplate: ce.ctr.VcsClient.GetPrCommentLinkTemplate(ce.pullRequest),
338+
}
339+
comments := ce.vcsNote.BuildComment(ctx, buildCommentParams)
335340

336341
if err = ce.ctr.VcsClient.UpdateMessage(ctx, ce.pullRequest, ce.vcsNote, comments); err != nil {
337342
return errors.Wrap(err, "failed to push comment")

pkg/msg/message.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ func (m *Message) buildFooter(
145145
hostname, duration.Round(time.Second), pkg.GitCommit, envStr, appsChecked, totalChecked)
146146
}
147147

148+
type BuildCommentParams struct {
149+
Start time.Time
150+
CommitSHA string
151+
LabelFilter string
152+
ShowDebugInfo bool
153+
Identifier string
154+
AppsChecked int
155+
TotalChecked int
156+
MaxCommentLength int
157+
PrLinkTemplate string
158+
}
159+
148160
// BuildComment iterates the map of all apps in this message, building a final comment from their current state
149161
//
150162
// This function is responsible for generating the VCS comment(s) for a PR/MR, handling:
@@ -182,8 +194,7 @@ func (m *Message) buildFooter(
182194
//
183195
// The output is a slice of strings, each representing a comment chunk to be posted.
184196
func (m *Message) BuildComment(
185-
ctx context.Context, start time.Time, commitSHA, labelFilter string, showDebugInfo bool, identifier string,
186-
appsChecked, totalChecked int, maxCommentLength int, prLinkTemplate string,
197+
ctx context.Context, params BuildCommentParams,
187198
) []string {
188199
_, span := tracer.Start(ctx, "buildComment")
189200
defer span.End()
@@ -192,14 +203,14 @@ func (m *Message) BuildComment(
192203
names := getSortedKeys(m.apps)
193204

194205
// Header for the first comment
195-
header := pkg.GetMessageHeader(identifier)
206+
header := pkg.GetMessageHeader(params.Identifier)
196207
// Footer warning for split comments
197208
splitCommentFooter := "\n\n> **Warning**: Output length greater than maximum allowed comment size. Continued in next comment"
198209
// Header for continued comments
199210
// this is written from the VCS PostMessage/UpdateMessage function. So, we need to account for it here
200-
continuedHeader := fmt.Sprintf("%s> Continued from previous [comment](%s)\n\n", header, prLinkTemplate)
211+
continuedHeader := fmt.Sprintf("%s> Continued from previous [comment](%s)\n\n", header, params.PrLinkTemplate)
201212
// Max content length for each chunk (accounting for continuedHeader)
202-
maxContentLength := maxCommentLength - len(continuedHeader) - 10 // 10 is a safety margin
213+
maxContentLength := params.MaxCommentLength - len(continuedHeader) - 10 // 10 is a safety margin
203214
contentLength := 0
204215

205216
comments := []string{}
@@ -359,12 +370,12 @@ func (m *Message) BuildComment(
359370
}
360371

361372
// Add the footer (with debug info if requested)
362-
footer := m.buildFooter(start, commitSHA, labelFilter, showDebugInfo, appsChecked, totalChecked)
373+
footer := m.buildFooter(params.Start, params.CommitSHA, params.LabelFilter, params.ShowDebugInfo, params.AppsChecked, params.TotalChecked)
363374
sb.WriteString(fmt.Sprintf("\n\n%s", footer))
364375

365376
// Only split if content exceeds the max length
366-
if len(sb.String()) > maxCommentLength {
367-
comments = append(comments, sb.String()[:maxCommentLength])
377+
if len(sb.String()) > params.MaxCommentLength {
378+
comments = append(comments, sb.String()[:params.MaxCommentLength])
368379
} else {
369380
comments = append(comments, sb.String())
370381
}

0 commit comments

Comments
 (0)