145 lines
5.1 KiB
Go
145 lines
5.1 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
const outcomePostInterval = 15 * time.Second
|
|
|
|
type outcomeDeliverer func(ctx context.Context, origin, text string) error
|
|
|
|
// conversationPoster appends a message to a Gateway conversation.
|
|
type conversationPoster interface {
|
|
PostToConversation(ctx context.Context, platform string, conversation messaging.ConversationID, text string) error
|
|
}
|
|
|
|
// channelDeliverer sends text to a Messaging Service channel.
|
|
type channelDeliverer interface {
|
|
Deliver(ctx context.Context, channel, chatID, text string) error
|
|
}
|
|
|
|
// deliverOutcome sends a dashboard conversation's outcome into its Gateway
|
|
// session, where the dashboard reads it, and every other channel's through
|
|
// the Messaging Service.
|
|
func deliverOutcome(chats conversationPoster, channels channelDeliverer) outcomeDeliverer {
|
|
return func(ctx context.Context, origin, text string) error {
|
|
channel, conversation, _ := strings.Cut(origin, ":")
|
|
if channel == "web" {
|
|
channelID, threadID, _ := strings.Cut(conversation, "/")
|
|
return chats.PostToConversation(ctx, channel, messaging.ConversationID{ChannelID: channelID, ThreadID: threadID}, text)
|
|
}
|
|
chatID, _, _ := strings.Cut(conversation, "/")
|
|
return channels.Deliver(ctx, channel, chatID, text)
|
|
}
|
|
}
|
|
|
|
// postOutcomes reports each finished chat task to the conversation that
|
|
// spawned it. A task stays unposted until delivery succeeds, so a Messaging
|
|
// Service that is down or restarting only delays the report. posted is told
|
|
// of each delivered outcome, so an open conversation can show it.
|
|
func postOutcomes(ctx context.Context, tasks storecontract.OutcomePoster, deliver outcomeDeliverer, posted func(taskID int64), log *slog.Logger) {
|
|
ticker := time.NewTicker(outcomePostInterval)
|
|
defer ticker.Stop()
|
|
for {
|
|
postPendingOutcomes(ctx, tasks, deliver, posted, log)
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func postPendingOutcomes(ctx context.Context, tasks storecontract.OutcomePoster, deliver outcomeDeliverer, posted func(taskID int64), log *slog.Logger) {
|
|
pending, err := tasks.UnpostedOutcomes(ctx)
|
|
if err != nil {
|
|
log.WarnContext(ctx, "list unposted task outcomes", "err", err)
|
|
return
|
|
}
|
|
for _, t := range pending {
|
|
// An origin that names no chat can never be delivered; marking it
|
|
// posted keeps it from being retried forever.
|
|
delivered := false
|
|
if validOrigin(t.Origin) {
|
|
err := deliver(ctx, t.Origin, outcomeText(t))
|
|
delivered = err == nil
|
|
switch {
|
|
case status.Code(err) == codes.NotFound:
|
|
// No such channel or conversation, so a retry can never
|
|
// deliver it.
|
|
log.InfoContext(ctx, "task outcome has nowhere to go", "task", t.ID, "origin", t.Origin)
|
|
case err != nil:
|
|
log.WarnContext(ctx, "post task outcome", "task", t.ID, "err", err)
|
|
continue
|
|
}
|
|
}
|
|
if err := tasks.MarkOutcomePosted(ctx, t.ID, t.Status); err != nil {
|
|
log.WarnContext(ctx, "mark task outcome posted", "task", t.ID, "err", err)
|
|
}
|
|
if delivered {
|
|
posted(t.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// validOrigin reports whether origin is "<channel>:<chat>[/<thread>]", the
|
|
// form messaging.Origin writes.
|
|
func validOrigin(origin string) bool {
|
|
channel, conversation, found := strings.Cut(origin, ":")
|
|
chatID, _, _ := strings.Cut(conversation, "/")
|
|
return found && channel != "" && chatID != ""
|
|
}
|
|
|
|
func outcomeText(t *task.Task) string {
|
|
title := notificationTitle(t.Title)
|
|
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, title, approvalNoun[kind])
|
|
}
|
|
if t.Status == task.StatusPROpen {
|
|
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, title, t.Status)
|
|
if t.PRNumber > 0 {
|
|
text += fmt.Sprintf(", PR #%d", t.PRNumber)
|
|
}
|
|
if t.ParkReason != "" {
|
|
text += "\n" + t.ParkReason
|
|
}
|
|
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.
|
|
var approvalNoun = map[string]string{
|
|
taskstate.ApprovalPlan: "plan",
|
|
taskstate.ApprovalReview: "review findings",
|
|
}
|