436 lines
14 KiB
Go
436 lines
14 KiB
Go
package daemon
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/forge"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
"github.com/samcharles93/archie-core/internal/worktree"
|
|
)
|
|
|
|
type parkCall struct {
|
|
taskID int64
|
|
from string
|
|
detail string
|
|
class taskstate.ParkClass
|
|
blocker taskstate.Blocker
|
|
}
|
|
|
|
type reconcileStore struct {
|
|
storecontract.TaskStore
|
|
open []workflowtask.Task
|
|
transitions []string
|
|
parks []parkCall
|
|
parkErr error
|
|
reviewCursor int64
|
|
commentCursor int64
|
|
}
|
|
|
|
func (s *reconcileStore) OpenPRs(context.Context) ([]workflowtask.Task, error) {
|
|
var out []workflowtask.Task
|
|
for _, t := range s.open {
|
|
if t.Status == workflow.StatusPROpen {
|
|
out = append(out, t)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *reconcileStore) Transition(_ context.Context, _ int64, _, to, _ string) error {
|
|
s.transitions = append(s.transitions, to)
|
|
return nil
|
|
}
|
|
|
|
func (s *reconcileStore) ParkTask(_ context.Context, taskID int64, from, detail, class, blocker string) error {
|
|
if s.parkErr != nil {
|
|
return s.parkErr
|
|
}
|
|
if !taskstate.CanTransition(from, workflow.StatusParked) {
|
|
return storecontract.ErrIllegalTransition
|
|
}
|
|
for i := range s.open {
|
|
if s.open[i].ID != taskID {
|
|
continue
|
|
}
|
|
if s.open[i].Status != from {
|
|
return storecontract.ErrStaleTransition
|
|
}
|
|
s.open[i].Status = workflow.StatusParked
|
|
s.open[i].ParkReason = detail
|
|
s.open[i].ParkClass = class
|
|
s.open[i].ParkBlocker = blocker
|
|
s.parks = append(s.parks, parkCall{
|
|
taskID: taskID, from: from, detail: detail, class: class, blocker: blocker,
|
|
})
|
|
return nil
|
|
}
|
|
return storecontract.ErrStaleTransition
|
|
}
|
|
|
|
func (s *reconcileStore) SetReviewCursors(_ context.Context, taskID, reviewCursor, commentCursor int64) error {
|
|
s.reviewCursor = reviewCursor
|
|
s.commentCursor = commentCursor
|
|
for i := range s.open {
|
|
if s.open[i].ID == taskID {
|
|
s.open[i].ReviewCursor = reviewCursor
|
|
s.open[i].WatchCommentID = commentCursor
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// identityGatedHub refuses every task the way the execution-identity gate
|
|
// refuses an operator-started one.
|
|
type identityGatedHub struct {
|
|
repoHub
|
|
log *slog.Logger
|
|
repoForge forge.Forge
|
|
repoErr error
|
|
}
|
|
|
|
func (h identityGatedHub) forgeFor(context.Context, *workflow.Task) forge.Forge {
|
|
return forge.NewNoop(h.log)
|
|
}
|
|
|
|
func (identityGatedHub) treesFor(*workflow.Task) *worktree.Manager {
|
|
return &worktree.Manager{WorkDir: "/nonexistent"}
|
|
}
|
|
|
|
func (h identityGatedHub) Repository(context.Context, *workflow.Task, string) (RepositoryAccess, error) {
|
|
if h.repoErr != nil {
|
|
return RepositoryAccess{}, h.repoErr
|
|
}
|
|
return RepositoryAccess{Forge: h.repoForge}, nil
|
|
}
|
|
|
|
type mergedForge struct{ forge.Forge }
|
|
|
|
func (mergedForge) PRState(context.Context, string, string, int) (string, error) {
|
|
return forge.PRStateMerged, nil
|
|
}
|
|
|
|
func (mergedForge) CloseIssue(context.Context, string, string, int, string) error { return nil }
|
|
|
|
type staticBotSource string
|
|
|
|
func (b staticBotSource) botUserForTask(*workflowtask.Task) string { return string(b) }
|
|
|
|
type reconcileReviewForge struct {
|
|
forge.Forge
|
|
fakeReviewForge
|
|
listCalls int
|
|
}
|
|
|
|
func (f *reconcileReviewForge) ListReviews(_ context.Context, _, _ string, _ int, sinceID int64) ([]forge.Review, error) {
|
|
f.listCalls++
|
|
var out []forge.Review
|
|
for _, r := range f.reviews {
|
|
if r.ID > sinceID {
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// TestReconcilePRsIgnoresTaskIdentity guards #1872: a merged PR of an
|
|
// operator-started task stayed In review because the reconciler resolved its
|
|
// forge through the task's execution identity.
|
|
func TestReconcilePRsIgnoresTaskIdentity(t *testing.T) {
|
|
log := slog.New(slog.DiscardHandler)
|
|
task := workflowtask.Task{ID: 7, Owner: "o", Repo: "r", PRNumber: 3, Org: "acme", Identity: "operator", Status: workflow.StatusPROpen}
|
|
store := &reconcileStore{open: []workflowtask.Task{task}}
|
|
var resolvedOrg org.OrgID
|
|
s := &ReviewScanner{
|
|
store: store, bus: events.NewBus(), log: log,
|
|
repos: identityGatedHub{log: log},
|
|
forges: func(ctx context.Context, _, _ string) (forge.Forge, error) {
|
|
resolvedOrg = org.OrgFromContext(ctx)
|
|
return mergedForge{}, nil
|
|
},
|
|
}
|
|
if err := s.reconcilePRs(t.Context()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.Equal(store.transitions, []string{workflow.StatusMerged}) {
|
|
t.Errorf("transitions = %v, want [merged]", store.transitions)
|
|
}
|
|
if resolvedOrg != "acme" {
|
|
t.Errorf("forge resolved in org %q, want the task's org", resolvedOrg)
|
|
}
|
|
}
|
|
|
|
// TestOpenPRForgeResolutionAndParking guards #1975: scanPRReviews resolves
|
|
// operator-started pr_open tasks through s.forges instead of forgeFor,
|
|
// permanent forge resolution failures park pr_open tasks with ParkNeedsDecision
|
|
// and the expected blocker (stopping subsequent retries), and transient
|
|
// errors warn and skip without parking.
|
|
func TestOpenPRForgeResolutionAndParking(t *testing.T) {
|
|
baseTask := func() workflowtask.Task {
|
|
return workflowtask.Task{
|
|
ID: 11, Owner: "o", Repo: "r", IssueNumber: 5, PRNumber: 9,
|
|
Org: "acme", Identity: "operator", Workflow: "implement", Attempt: 2,
|
|
Status: workflow.StatusPROpen,
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
scan bool // true exercises scanPRReviews; false exercises reconcilePRs
|
|
task workflowtask.Task
|
|
nilForges bool
|
|
nilBus bool
|
|
forgeErr error
|
|
repoErr error
|
|
parkErr error
|
|
wantParked bool
|
|
wantBlocker taskstate.Blocker
|
|
wantParkEvent bool
|
|
wantSecondCall bool
|
|
wantScan bool
|
|
wantLog string
|
|
forbidLog string
|
|
}{
|
|
{
|
|
name: "reconcilePRs parks connection_missing and stops retrying",
|
|
task: baseTask(),
|
|
forgeErr: &forge.AccessError{Code: "connection_missing", Message: "The selected connection no longer exists."},
|
|
wantParked: true,
|
|
wantBlocker: taskstate.BlockerRepository,
|
|
wantParkEvent: true,
|
|
wantSecondCall: false,
|
|
},
|
|
{
|
|
name: "scanPRReviews parks operation_denied and stops retrying",
|
|
scan: true,
|
|
task: baseTask(),
|
|
forgeErr: &forge.AccessError{Code: "operation_denied", Operation: "clone", Message: "operation denied"},
|
|
wantParked: true,
|
|
wantBlocker: taskstate.BlockerConnectionPermit,
|
|
wantParkEvent: true,
|
|
wantSecondCall: false,
|
|
},
|
|
{
|
|
name: "scanPRReviews parks blockedPark pinFailure",
|
|
scan: true,
|
|
task: baseTask(),
|
|
forgeErr: blockedPark{blocker: taskstate.BlockerAgentAssignment, err: pinFailure{class: taskstate.ParkNeedsDecision, err: errors.New("inactive agent")}},
|
|
wantParked: true,
|
|
wantBlocker: taskstate.BlockerAgentAssignment,
|
|
wantParkEvent: true,
|
|
wantSecondCall: false,
|
|
},
|
|
{
|
|
name: "reconcilePRs parks non-transient pinFailure without blocker",
|
|
task: baseTask(),
|
|
nilBus: true,
|
|
forgeErr: &pinFailure{class: taskstate.ParkNeedsDecision, err: errors.New("wrong org")},
|
|
wantParked: true,
|
|
wantBlocker: "",
|
|
wantParkEvent: false,
|
|
wantSecondCall: false,
|
|
},
|
|
{
|
|
name: "reconcilePRs skips configuration_unavailable without parking",
|
|
task: baseTask(),
|
|
forgeErr: &forge.AccessError{Code: "configuration_unavailable", Message: "Settings unavailable."},
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: true,
|
|
wantLog: "PR state check skipped",
|
|
},
|
|
{
|
|
name: "scanPRReviews skips generic error without parking",
|
|
scan: true,
|
|
task: baseTask(),
|
|
forgeErr: errors.New("temporary dial error"),
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: true,
|
|
wantLog: "review scan skipped",
|
|
},
|
|
{
|
|
name: "reconcilePRs skips transient pinFailure without parking",
|
|
task: baseTask(),
|
|
forgeErr: pinFailure{class: taskstate.ParkTransient, err: errors.New("store timeout")},
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: true,
|
|
wantLog: "PR state check skipped",
|
|
},
|
|
{
|
|
name: "scanPRReviews skips transient pinFailure without parking",
|
|
scan: true,
|
|
task: baseTask(),
|
|
forgeErr: pinFailure{class: taskstate.ParkTransient, err: errors.New("store timeout")},
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: true,
|
|
wantLog: "review scan skipped",
|
|
},
|
|
{
|
|
name: "scanPRReviews resolves operator-started task via s.forges",
|
|
scan: true,
|
|
task: baseTask(),
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: true,
|
|
wantScan: true,
|
|
forbidLog: "no forge for task",
|
|
},
|
|
{
|
|
name: "reconcilePRs suppresses parked event on stale transition",
|
|
task: baseTask(),
|
|
forgeErr: &forge.AccessError{Code: "connection_missing", Message: "missing"},
|
|
parkErr: storecontract.ErrStaleTransition,
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: true,
|
|
},
|
|
{
|
|
name: "reconcilePRs skips task without repository when noop forge returned",
|
|
task: workflowtask.Task{ID: 12, PRNumber: 4, Identity: "operator", Status: workflow.StatusPROpen},
|
|
wantParked: false,
|
|
wantParkEvent: false,
|
|
wantSecondCall: false,
|
|
wantLog: "PR state check skipped",
|
|
},
|
|
{
|
|
name: "reconcilePRs with nil forges falls back to Repository and preserves typed error",
|
|
task: baseTask(),
|
|
nilForges: true,
|
|
repoErr: &forge.AccessError{Code: "connection_missing", Message: "missing via repo"},
|
|
wantParked: true,
|
|
wantBlocker: taskstate.BlockerRepository,
|
|
wantParkEvent: true,
|
|
wantSecondCall: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var logBuf bytes.Buffer
|
|
log := slog.New(slog.NewTextHandler(&logBuf, nil))
|
|
bus := events.NewBus()
|
|
defer bus.Close()
|
|
sub := bus.Subscribe(4)
|
|
defer sub.Close()
|
|
|
|
store := &reconcileStore{open: []workflowtask.Task{tt.task}, parkErr: tt.parkErr}
|
|
reviewForge := &reconcileReviewForge{
|
|
reviews: []forge.Review{{ID: 10, Author: "maint", State: forge.ReviewStateRequestedChanges, Body: "fix"}},
|
|
writers: map[string]bool{"maint": true},
|
|
}
|
|
var published []string
|
|
var forgeCalls int
|
|
var resolvedOrg org.OrgID
|
|
var forgesFn RepositoryForgeResolver
|
|
if !tt.nilForges {
|
|
forgesFn = func(ctx context.Context, _, _ string) (forge.Forge, error) {
|
|
forgeCalls++
|
|
resolvedOrg = org.OrgFromContext(ctx)
|
|
if tt.forgeErr != nil {
|
|
return nil, tt.forgeErr
|
|
}
|
|
return reviewForge, nil
|
|
}
|
|
}
|
|
|
|
var scannerBus *events.Bus
|
|
if !tt.nilBus {
|
|
scannerBus = bus
|
|
}
|
|
s := &ReviewScanner{
|
|
store: store,
|
|
bus: scannerBus,
|
|
log: log,
|
|
repos: identityGatedHub{log: log, repoErr: tt.repoErr},
|
|
bots: staticBotSource("bot"),
|
|
forges: forgesFn,
|
|
publish: func(_ context.Context, r workintake.ReviewCommentEnvelope) error {
|
|
published = append(published, r.Author)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
run := func() error {
|
|
if tt.scan {
|
|
return s.scanPRReviews(t.Context())
|
|
}
|
|
return s.reconcilePRs(t.Context())
|
|
}
|
|
|
|
if err := run(); err != nil {
|
|
t.Fatalf("first pass error: %v", err)
|
|
}
|
|
if err := run(); err != nil {
|
|
t.Fatalf("second pass error: %v", err)
|
|
}
|
|
|
|
if tt.wantParked {
|
|
if len(store.parks) != 1 {
|
|
t.Fatalf("parks = %d, want 1", len(store.parks))
|
|
}
|
|
got := store.parks[0]
|
|
if got.taskID != tt.task.ID || got.from != workflow.StatusPROpen || got.class != taskstate.ParkNeedsDecision || got.blocker != tt.wantBlocker {
|
|
t.Fatalf("park = %+v, want task=%d from=%s class=%s blocker=%q", got, tt.task.ID, workflow.StatusPROpen, taskstate.ParkNeedsDecision, tt.wantBlocker)
|
|
}
|
|
} else if len(store.parks) != 0 {
|
|
t.Fatalf("unexpected parks: %+v", store.parks)
|
|
}
|
|
|
|
if !tt.nilForges && tt.task.HasRepository() {
|
|
wantCalls := 1
|
|
if tt.wantSecondCall {
|
|
wantCalls = 2
|
|
}
|
|
if forgeCalls != wantCalls {
|
|
t.Fatalf("forgeCalls = %d, want %d", forgeCalls, wantCalls)
|
|
}
|
|
}
|
|
|
|
select {
|
|
case ev := <-sub.C:
|
|
if !tt.wantParkEvent {
|
|
t.Fatalf("unexpected event emitted: %+v", ev)
|
|
}
|
|
if ev.Kind != events.KindParked || ev.TaskID != tt.task.ID || ev.Attempt != tt.task.Attempt || ev.Data["park_class"] != taskstate.ParkNeedsDecision {
|
|
t.Fatalf("park event = %+v, want KindParked task=%d attempt=%d park_class=%s", ev, tt.task.ID, tt.task.Attempt, taskstate.ParkNeedsDecision)
|
|
}
|
|
default:
|
|
if tt.wantParkEvent {
|
|
t.Fatal("expected KindParked event, got none")
|
|
}
|
|
}
|
|
|
|
if tt.wantScan {
|
|
if resolvedOrg != tt.task.Org {
|
|
t.Fatalf("resolvedOrg = %q, want %q", resolvedOrg, tt.task.Org)
|
|
}
|
|
if reviewForge.listCalls == 0 || !slices.Equal(published, []string{"maint"}) || store.reviewCursor != 10 {
|
|
t.Fatalf("scan did not execute via s.forges: listCalls=%d published=%v cursor=%d", reviewForge.listCalls, published, store.reviewCursor)
|
|
}
|
|
}
|
|
|
|
logs := logBuf.String()
|
|
if tt.wantLog != "" && !strings.Contains(logs, tt.wantLog) {
|
|
t.Errorf("logs %q missing %q", logs, tt.wantLog)
|
|
}
|
|
if tt.forbidLog != "" && strings.Contains(logs, tt.forbidLog) {
|
|
t.Errorf("logs %q unexpectedly contained %q", logs, tt.forbidLog)
|
|
}
|
|
})
|
|
}
|
|
}
|