Skip to content

Commit 75464e3

Browse files
authored
fix: trim github notifiction massge (#113)
Signed-off-by: Gosha <[email protected]>
1 parent 4cc3f0f commit 75464e3

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

pkg/event_handler/github_event_notifier.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
77
"github.com/rookout/piper/pkg/clients"
88
"github.com/rookout/piper/pkg/conf"
9+
"github.com/rookout/piper/pkg/utils"
910
)
1011

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

51-
message := workflow.Status.Message
52+
message := utils.TrimString(workflow.Status.Message, 140) // Max length of message is 140 characters
5253
err := gn.clients.GitProvider.SetStatus(ctx, &repo, &commit, &workflowLink, &status, &message)
5354
if err != nil {
5455
return fmt.Errorf("failed to set status for workflow %s: %s", workflow.GetName(), err)

pkg/utils/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,10 @@ func ValidateHTTPFormat(input string) bool {
131131
match, _ := regexp.MatchString(regex, input)
132132
return match
133133
}
134+
135+
func TrimString(s string, maxLength int) string {
136+
if maxLength >= len(s) {
137+
return s
138+
}
139+
return s[:maxLength]
140+
}

pkg/utils/common_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,25 @@ func TestValidateHTTPFormat(t *testing.T) {
303303
}
304304

305305
}
306+
307+
func TestTrimString(t *testing.T) {
308+
assert := assertion.New(t)
309+
310+
// Test cases
311+
testCases := []struct {
312+
input string
313+
maxLength int
314+
expected string
315+
}{
316+
{"This is a sample string.", 10, "This is a "},
317+
{"Short", 10, "Short"},
318+
{"Longer string for testing.", 5, "Longe"},
319+
{"", 10, ""},
320+
}
321+
322+
// Perform tests
323+
for _, tc := range testCases {
324+
result := TrimString(tc.input, tc.maxLength)
325+
assert.Equal(tc.expected, result)
326+
}
327+
}

0 commit comments

Comments
 (0)