fix(archied): bound task titles in chat notifications
This commit is contained in:
1 parent
da7efa3f4e
commit
ca44b9e6bb
2 files changed
+52
-3
No files matched your search
@@ -103,13 +103,14 @@ func validOrigin(origin string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func outcomeText(t *task.Task) string {
|
func outcomeText(t *task.Task) string {
|
||||||
|
title := notificationTitle(t.Title)
|
||||||
if kind := taskstate.ApprovalKind(t.Status, t.ReviewGate); kind != "" {
|
if kind := taskstate.ApprovalKind(t.Status, t.ReviewGate); kind != "" {
|
||||||
return fmt.Sprintf("Task %d (%s) needs your approval of its %s. Approve or reject it on the task page.", t.ID, t.Title, approvalNoun[kind])
|
return fmt.Sprintf("Task %d (%s) needs your approval of its %s. Approve or reject it on the task page.", t.ID, title, approvalNoun[kind])
|
||||||
}
|
}
|
||||||
if t.Status == task.StatusPROpen {
|
if t.Status == task.StatusPROpen {
|
||||||
return fmt.Sprintf("Task %d (%s) opened PR #%d for review.", t.ID, t.Title, t.PRNumber)
|
return fmt.Sprintf("Task %d (%s) opened PR #%d for review.", t.ID, title, t.PRNumber)
|
||||||
}
|
}
|
||||||
text := fmt.Sprintf("Task %d (%s) finished: %s", t.ID, t.Title, t.Status)
|
text := fmt.Sprintf("Task %d (%s) finished: %s", t.ID, title, t.Status)
|
||||||
if t.PRNumber > 0 {
|
if t.PRNumber > 0 {
|
||||||
text += fmt.Sprintf(", PR #%d", t.PRNumber)
|
text += fmt.Sprintf(", PR #%d", t.PRNumber)
|
||||||
}
|
}
|
||||||
@@ -119,6 +120,23 @@ func outcomeText(t *task.Task) string {
|
|||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// maxNotificationTitleRunes bounds the title a notification echoes. New titles
|
||||||
|
// are capped at the same length when they are created, but a task from another
|
||||||
|
// writer -- an issue, a schedule -- can still carry a long one, and the
|
||||||
|
// notification is one line of chat.
|
||||||
|
const maxNotificationTitleRunes = 80
|
||||||
|
|
||||||
|
// notificationTitle reduces a task title to a single bounded line. The title
|
||||||
|
// is the one part of the notification a writer other than chat controls.
|
||||||
|
func notificationTitle(title string) string {
|
||||||
|
line := strings.Join(strings.Fields(title), " ")
|
||||||
|
runes := []rune(line)
|
||||||
|
if len(runes) <= maxNotificationTitleRunes {
|
||||||
|
return line
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(runes[:maxNotificationTitleRunes])) + "..."
|
||||||
|
}
|
||||||
|
|
||||||
// approvalNoun names what an approval kind asks the operator to decide.
|
// approvalNoun names what an approval kind asks the operator to decide.
|
||||||
var approvalNoun = map[string]string{
|
var approvalNoun = map[string]string{
|
||||||
taskstate.ApprovalPlan: "plan",
|
taskstate.ApprovalPlan: "plan",
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
@@ -23,6 +25,35 @@ func (s *outcomeStore) MarkOutcomePosted(_ context.Context, id int64, _ string)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOutcomeTextBoundsTheTitle(t *testing.T) {
|
||||||
|
title := strings.Repeat("workflow_draft returns InvalidArgument because ", 30) + "\nsecond line"
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
task *task.Task
|
||||||
|
}{
|
||||||
|
{"approval", &task.Task{ID: 7, Title: title, Status: task.StatusWaitingApproval}},
|
||||||
|
{"pr open", &task.Task{ID: 7, Title: title, Status: task.StatusPROpen, PRNumber: 6}},
|
||||||
|
{"finished", &task.Task{ID: 7, Title: title, Status: task.StatusRejected}},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
text := outcomeText(tt.task)
|
||||||
|
if strings.Contains(text, title) {
|
||||||
|
t.Fatalf("text echoes the whole title: %q", text)
|
||||||
|
}
|
||||||
|
if strings.Contains(text, "\n") {
|
||||||
|
t.Errorf("text is not one line: %q", text)
|
||||||
|
}
|
||||||
|
if got := utf8.RuneCountInString(text); got > 200 {
|
||||||
|
t.Errorf("text is %d runes, want at most 200: %q", got, text)
|
||||||
|
}
|
||||||
|
if !strings.Contains(text, "workflow_draft returns") {
|
||||||
|
t.Errorf("text dropped the start of the title: %q", text)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPostPendingOutcomes(t *testing.T) {
|
func TestPostPendingOutcomes(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
Reference in new issue
Block a user