From 7e12fd166573c54066ae1fe79eaf1f7cf9e2a787 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Wed, 11 Sep 2024 17:32:48 +0200 Subject: [PATCH] fix: limit the length of the pipeline description --- pkg/scm/gitlab/client.go | 8 ++++---- pkg/scm/helpers.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/scm/gitlab/client.go b/pkg/scm/gitlab/client.go index d631b37..56d8faf 100644 --- a/pkg/scm/gitlab/client.go +++ b/pkg/scm/gitlab/client.go @@ -205,8 +205,8 @@ func (client *Client) Stop(ctx context.Context, evalError error, allowPipelineFa } var ( - status = go_gitlab.Success - message = "OK" + status = go_gitlab.Success + description = "OK" ) if evalError != nil { @@ -214,13 +214,13 @@ func (client *Client) Stop(ctx context.Context, evalError error, allowPipelineFa status = go_gitlab.Failed } - message = evalError.Error() + description = scm.TruncateText(evalError.Error(), 250) } _, response, err := client.wrapped.Commits.SetCommitStatus(state.ProjectID(ctx), state.CommitSHA(ctx), &go_gitlab.SetCommitStatusOptions{ State: status, Context: pipelineName, - Description: scm.Ptr(message), + Description: scm.Ptr(description), TargetURL: targetURL, }) diff --git a/pkg/scm/helpers.go b/pkg/scm/helpers.go index 02bab80..5eb4874 100644 --- a/pkg/scm/helpers.go +++ b/pkg/scm/helpers.go @@ -5,6 +5,7 @@ import ( "path/filepath" "regexp" "strings" + "unicode" ) // Ptr is a helper that returns a pointer to v. @@ -74,6 +75,26 @@ func FindModifiedFiles(files []string, patterns ...string) []string { return output } +func TruncateText(text string, maxLen int) string { + lastSpaceIx := maxLen + curLen := 0 + + for i, r := range text { + if unicode.IsSpace(r) { + lastSpaceIx = i + } + + curLen++ + + if curLen > maxLen { + return text[:lastSpaceIx] + "..." + } + } + + // If here, string is shorter or equal to maxLen + return text +} + // buildPatternRegex compiles a new regexp object from a gitignore-style pattern string func buildPatternRegex(pattern string) (*regexp.Regexp, error) { // Handle specific edge cases first