Skip to content

Commit

Permalink
fix: limit the length of the pipeline description
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Sep 11, 2024
1 parent a0f82a4 commit 7e12fd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/scm/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,22 @@ 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 {
if allowPipelineFailure {
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,
})

Expand Down
21 changes: 21 additions & 0 deletions pkg/scm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"regexp"
"strings"
"unicode"
)

// Ptr is a helper that returns a pointer to v.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7e12fd1

Please sign in to comment.