Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
[notifiers/github] Truncate GitHub status if > 140 chars
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wlynch authored and tekton-robot committed Jul 15, 2021
1 parent cd28b7a commit 9f8335e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
19 changes: 13 additions & 6 deletions notifiers/github-app/pkg/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@ 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()),
}
_, _, err = client.Repositories.CreateStatus(ctx, owner, repo, commit, status)
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())
Expand Down
31 changes: 31 additions & 0 deletions notifiers/github-app/pkg/controller/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 9f8335e

Please sign in to comment.