diff --git a/internal/domain/workflow/prreview_own_pr.go b/internal/domain/workflow/prreview_own_pr.go index 7b03ecbf4..5e739e03a 100644 --- a/internal/domain/workflow/prreview_own_pr.go +++ b/internal/domain/workflow/prreview_own_pr.go @@ -92,7 +92,6 @@ func stagePRReviewAndOpenPR(decisionStages []Stage, body func(*TaskContext) stri if tc.PRSource == nil { tc.PRSource = &localPRSource{tc: tc} } - tc.prReview.ownPR = true defer removePRSnapshot(tc) for _, stage := range decisionStages { if err := stage.Run(ctx, tc); err != nil { @@ -110,6 +109,10 @@ func stagePRReviewAndOpenPR(decisionStages []Stage, body func(*TaskContext) stri return nil } + // Intake allocates the review state on every entry, so this flag is set + // on the state the decision stages built, not before them. + tc.prReview.ownPR = true + if err := OpenPR(ctx, tc, body(tc)); err != nil { return err } diff --git a/internal/domain/workflow/prreview_own_pr_test.go b/internal/domain/workflow/prreview_own_pr_test.go new file mode 100644 index 000000000..714a07380 --- /dev/null +++ b/internal/domain/workflow/prreview_own_pr_test.go @@ -0,0 +1,30 @@ +package workflow + +import ( + "context" + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" +) + +// Intake allocates tc.prReview on every entry, so the own-PR flag is recorded +// after the decision stages, on the state they built. Recording it before them +// dereferenced a nil state and panicked the agent container at the end of every +// implement run, before the pull request was opened. +func TestOwnPRReviewRecordsOwnPRAfterTheDecisionStages(t *testing.T) { + tc := &TaskContext{Task: &task.Task{ID: 7, Source: task.SourceTest, Owner: "o", Repo: "r"}, Log: slog.Default()} + RecordDeliveries(tc) + intake := Stage{Name: "intake", Run: func(_ context.Context, tc *TaskContext) error { + tc.prReview = &prReviewState{} + return nil + }} + + stage := stagePRReviewAndOpenPR([]Stage{intake}, func(*TaskContext) string { return "" }) + if err := stage.Run(context.Background(), tc); err != nil { + t.Fatalf("own-PR review: %v", err) + } + if !tc.prReview.ownPR { + t.Fatal("the pull request this run opened is not marked as its own") + } +}