Files
archie/internal/app/archiegateway/chat_tasks.go
T

158 lines
5.3 KiB
Go

package archiegateway
import (
"context"
"fmt"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/taskactions"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/logging"
)
type chatTaskControllerAdapter struct {
taskByID func(context.Context, int64) (*workflow.Task, error)
approve func(context.Context, *string, taskactions.Actor, int64, taskactions.ActionPayload) error
cancelExecution func(context.Context, int64, string, string) ([]int64, error)
}
func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) {
task, err := a.taskByID(ctx, taskID)
if err != nil {
return gateway.ChatTaskStatus{}, false, err
}
if task == nil {
return gateway.ChatTaskStatus{}, false, nil
}
if !task.IsForgeBacked() {
return gateway.ChatTaskStatus{Status: task.Status, Identity: task.Identity}, true, nil
}
return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID)
}
func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error {
if a.approve == nil {
return fmt.Errorf("task approval is unavailable")
}
// Chat acts for the operator, as the dashboard does, so it is not
// narrowed to one identity's tasks. /approve carries no selection syntax,
// so the review gate answer posts every offered finding.
return a.approve(ctx, nil, actor, taskID, taskactions.ActionPayload{})
}
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error {
task, err := a.taskByID(ctx, taskID)
if err != nil {
return err
}
if task == nil {
return fmt.Errorf("task %d not found", taskID)
}
if task.IsForgeBacked() {
return fmt.Errorf("task %d is not chat-originated", taskID)
}
// An operator declining work lands in the same state whichever surface
// they used, through the one cancel path: the store records the
// cancellation -- every non-terminal step of the current attempt plus the
// execution's own move -- and
// this write is what a late worker's next step write fails against. This
// used to record StatusRejected while the dashboard recorded
// StatusClosedWontDo, so the same decision showed up as two different
// states -- and StatusRejected, which the PR reconciler uses for "the
// pull request was closed without merging", stopped meaning one thing.
if a.cancelExecution == nil {
return fmt.Errorf("execution cancellation is unavailable")
}
_, err = a.cancelExecution(ctx, taskID, reason, workflow.StatusClosedWontDo)
return err
}
// chatTaskListerAdapter gives the gateway a read view of the operator's tasks.
// gateway deliberately does not import the task store, so the projection
// happens here, as it does for the writer and controller adapters below.
type chatTaskListerAdapter struct {
tasks func(context.Context, int) ([]workflow.Task, error)
}
func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) {
rows, err := a.tasks(ctx, limit)
if err != nil {
return nil, err
}
out := make([]gateway.ChatTaskSummary, 0, limit)
for _, task := range rows {
out = append(out, gateway.ChatTaskSummary{
ID: task.ID,
Repo: task.Owner + "/" + task.Repo,
Title: task.Title,
Status: task.Status,
Workflow: task.Workflow,
PRNumber: task.PRNumber,
Attempt: task.Attempt,
ParkReason: task.ParkReason,
UpdatedAt: task.UpdatedAt,
})
}
return out, nil
}
// chatTaskLogReaderAdapter gives the gateway a read view of a task's
// persisted log history without importing internal/logging or the task store
// into the gateway package.
type chatTaskLogReaderAdapter struct {
tasks func(context.Context, int64) (*workflow.Task, error)
taskLogs storecontract.TaskLogStore
}
func (a chatTaskLogReaderAdapter) ReadChatTaskLogs(
ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery,
) (gateway.ChatTaskLogResult, error) {
task, err := a.tasks(ctx, taskID)
if err != nil {
return gateway.ChatTaskLogResult{}, err
}
if task == nil {
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID)
}
if attempt <= 0 {
attempt = task.Attempt
}
page, err := a.taskLogs.TaskLog(ctx, taskID, attempt, logging.Query{
Component: q.Component, Contains: q.Contains, Levels: q.Levels, Since: q.Since, Until: q.Until, Limit: q.Limit, BeforeID: q.AfterID,
})
if err != nil {
return gateway.ChatTaskLogResult{}, err
}
entries := make([]gateway.ChatTaskLogEntry, len(page.Entries))
for i, e := range page.Entries {
entries[i] = gateway.ChatTaskLogEntry{
Time: e.Time,
Level: e.Level,
Message: e.Message,
Fields: e.Fields,
}
}
return gateway.ChatTaskLogResult{
Entries: entries,
Attempt: attempt,
Truncated: page.Truncated,
Cursor: page.Cursor,
MoreAvailable: page.MoreAvailable,
}, nil
}
func configureTaskCommands(
router *gateway.Router,
tasks gateway.TaskCreator,
controller gateway.TaskController,
lister gateway.ChatTaskLister,
identity string,
) {
router.Tasks = tasks
router.Controller = controller
router.TaskLister = lister
router.Identity = identity
}