Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trim github notification message - RK-19530 #113

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/event_handler/github_event_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/rookout/piper/pkg/clients"
"github.com/rookout/piper/pkg/conf"
"github.com/rookout/piper/pkg/utils"
)

var workflowTranslationToGithubMap = map[string]string{
Expand Down Expand Up @@ -48,7 +49,7 @@ func (gn *githubNotifier) Notify(ctx *context.Context, workflow *v1alpha1.Workfl
return fmt.Errorf("failed to translate workflow status to github stasuts for %s status: %s", workflow.GetName(), workflow.Status.Phase)
}

message := workflow.Status.Message
message := utils.TrimString(workflow.Status.Message, 140) // Max length of message is 140 characters
err := gn.clients.GitProvider.SetStatus(ctx, &repo, &commit, &workflowLink, &status, &message)
if err != nil {
return fmt.Errorf("failed to set status for workflow %s: %s", workflow.GetName(), err)
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@ func ValidateHTTPFormat(input string) bool {
match, _ := regexp.MatchString(regex, input)
return match
}

func TrimString(s string, maxLength int) string {
if maxLength >= len(s) {
return s
}
return s[:maxLength]
}
22 changes: 22 additions & 0 deletions pkg/utils/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,25 @@ func TestValidateHTTPFormat(t *testing.T) {
}

}

func TestTrimString(t *testing.T) {
assert := assertion.New(t)

// Test cases
testCases := []struct {
input string
maxLength int
expected string
}{
{"This is a sample string.", 10, "This is a "},
{"Short", 10, "Short"},
{"Longer string for testing.", 5, "Longe"},
{"", 10, ""},
}

// Perform tests
for _, tc := range testCases {
result := TrimString(tc.input, tc.maxLength)
assert.Equal(tc.expected, result)
}
}