From 9f8335e1805d2d0c3fe4c83ed60fd457912b29bc Mon Sep 17 00:00:00 2001 From: Billy Lynch Date: Thu, 15 Jul 2021 16:07:07 -0400 Subject: [PATCH] [notifiers/github] Truncate GitHub status if > 140 chars Certain statuses are failing with the error: ``` description is too long (maximum is 140 characters) ``` This truncates and descriptions to 140 characters if they exceed the limit. --- notifiers/github-app/pkg/controller/status.go | 19 ++++++++---- .../github-app/pkg/controller/status_test.go | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/notifiers/github-app/pkg/controller/status.go b/notifiers/github-app/pkg/controller/status.go index 5704696e9..354bc186f 100644 --- a/notifiers/github-app/pkg/controller/status.go +++ b/notifiers/github-app/pkg/controller/status.go @@ -20,14 +20,9 @@ func (r *GitHubAppReconciler) HandleStatus(ctx context.Context, tr *v1beta1.Task repo := tr.Annotations[key("repo")] commit := tr.Annotations[key("commit")] - var description *string - if m := tr.GetStatusCondition().GetCondition(apis.ConditionSucceeded).GetMessage(); m != "" { - description = github.String(m) - } - status := &github.RepoStatus{ State: state(tr.Status), - Description: description, + Description: truncateDesc(tr.GetStatusCondition().GetCondition(apis.ConditionSucceeded).GetMessage()), TargetURL: github.String(dashboardURL(tr)), Context: github.String(tr.GetName()), } @@ -35,6 +30,18 @@ func (r *GitHubAppReconciler) HandleStatus(ctx context.Context, tr *v1beta1.Task return err } +// truncateDesc truncates a given string to fit within GitHub status character +// limits (140 chars). +func truncateDesc(m string) *string { + if m == "" { + return nil + } + if len(m) > 140 { + m = (m)[:137] + "..." + } + return &m +} + func dashboardURL(tr *v1beta1.TaskRun) string { // TODO: generalize host, object type. return fmt.Sprintf("https://dashboard.dogfooding.tekton.dev/#/namespaces/%s/taskruns/%s", tr.GetNamespace(), tr.GetName()) diff --git a/notifiers/github-app/pkg/controller/status_test.go b/notifiers/github-app/pkg/controller/status_test.go index 7ca04daea..d30877f5e 100644 --- a/notifiers/github-app/pkg/controller/status_test.go +++ b/notifiers/github-app/pkg/controller/status_test.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "reflect" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -69,3 +71,32 @@ func TestDashboardURL(t *testing.T) { t.Errorf("want: %s, got: %s", want, got) } } + +func TestTruncateDescription(t *testing.T) { + for _, tc := range []struct { + in string + want *string + }{ + { + in: "", + want: nil, + }, + { + in: "~", + want: github.String("~"), + }, + { + in: strings.Repeat("~", 140), + want: github.String(strings.Repeat("~", 140)), + }, + { + in: strings.Repeat("~", 141), + want: github.String(strings.Repeat("~", 137) + "..."), + }, + } { + out := truncateDesc(tc.in) + if !reflect.DeepEqual(out, tc.want) { + t.Errorf("truncDesc(%s) = %v, want %v", tc.in, out, tc.want) + } + } +}