From e34941b1eb3d7c6d96087f982724429cb49ea099 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 9 Nov 2026 14:37:08 +1100 Subject: [PATCH] fix(workflow): record a close refused for open dependencies instead of parking (#1845) --- .../domain/workflow/close_blocked_test.go | 62 +++++++++++++++++++ internal/domain/workflow/control_steps.go | 24 ++++++- internal/domain/workflow/steps.go | 2 +- internal/domain/workflow/test_run.go | 5 ++ internal/domain/workflow/workflow.go | 3 + 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 internal/domain/workflow/close_blocked_test.go diff --git a/internal/domain/workflow/close_blocked_test.go b/internal/domain/workflow/close_blocked_test.go new file mode 100644 index 000000000..5eb93359e --- /dev/null +++ b/internal/domain/workflow/close_blocked_test.go @@ -0,0 +1,62 @@ +package workflow + +import ( + "context" + "errors" + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" +) + +type closeForge struct { + Forger + closeErr error + comments int + labels []string +} + +func (f *closeForge) CloseIssue(context.Context, string, string, int, string) error { + return f.closeErr +} + +func (f *closeForge) Comment(context.Context, string, string, int, string) (int64, error) { + f.comments++ + return 1, nil +} + +func (f *closeForge) SetStateLabel(_ context.Context, _, _ string, _ int, label string, _ []string) error { + f.labels = append(f.labels, label) + return nil +} + +func TestCloseIssueRefusedForDependenciesRecordsTheDecision(t *testing.T) { + tests := []struct { + name string + closeErr error + wantErr bool + wantComments int + wantLabels int + }{ + {name: "closes"}, + {name: "open dependencies are commented and labelled", closeErr: errors.New("close issue: it still has open dependencies"), wantComments: 1, wantLabels: 1}, + {name: "other failures still park", closeErr: errors.New("forge unreachable"), wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + forge := &closeForge{closeErr: tt.closeErr} + tc := &TaskContext{ + Task: &task.Task{Owner: "acme", Repo: "widgets", IssueNumber: 5}, + Forge: forge, + Log: slog.Default(), + } + err := closeIssueOrRecordBlock(t.Context(), tc) + if (err != nil) != tt.wantErr { + t.Fatalf("err = %v, wantErr %v", err, tt.wantErr) + } + if forge.comments != tt.wantComments || len(forge.labels) != tt.wantLabels { + t.Errorf("comments=%d labels=%v, want %d and %d", forge.comments, forge.labels, tt.wantComments, tt.wantLabels) + } + }) + } +} diff --git a/internal/domain/workflow/control_steps.go b/internal/domain/workflow/control_steps.go index 829a14dde..b2eeaabd4 100644 --- a/internal/domain/workflow/control_steps.go +++ b/internal/domain/workflow/control_steps.go @@ -104,10 +104,32 @@ func newCloseIssueStage(settings yaml.Node) (Stage, error) { if !tc.Task.IsForgeBacked() { return nil } - return tc.Forge.CloseIssue(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, "") + return closeIssueOrRecordBlock(ctx, tc) }}, nil } +// blockedLabel records a close the forge refused for open dependencies. +const blockedLabel = "blocked" + +// closeIssueOrRecordBlock closes the task's issue. When the forge refuses +// because the issue has open dependencies, the decision to close is kept as a +// comment and a label and the step completes: parking would lose it. +func closeIssueOrRecordBlock(ctx context.Context, tc *TaskContext) error { + t := tc.Task + err := tc.Forge.CloseIssue(ctx, t.Owner, t.Repo, t.IssueNumber, "") + if err == nil || !strings.Contains(strings.ToLower(err.Error()), "dependenc") { + return err + } + note := fmt.Sprintf("Ready to close, but the forge refused because the issue has open dependencies: %s\n\nClose it once they are resolved.", err) + if _, cerr := tc.Forge.Comment(ctx, t.Owner, t.Repo, t.IssueNumber, note); cerr != nil { + return fmt.Errorf("record blocked close: %w", cerr) + } + if lerr := tc.Forge.SetStateLabel(ctx, t.Owner, t.Repo, t.IssueNumber, blockedLabel, nil); lerr != nil { + tc.Log.WarnContext(ctx, "label blocked issue", "issue", t.IssueNumber, "err", lerr) + } + return nil +} + type commentSettings struct { Body string `yaml:"body" doc:"The comment text."` // On is "issue" (the default) or "pr". diff --git a/internal/domain/workflow/steps.go b/internal/domain/workflow/steps.go index 7dd376c4e..08287208e 100644 --- a/internal/domain/workflow/steps.go +++ b/internal/domain/workflow/steps.go @@ -130,7 +130,7 @@ func closeNoChangesIssue(ctx context.Context, tc *TaskContext) error { tc.Outcome = Outcome{Status: StatusCompleted, Detail: "completed -- no changes required"} return nil } - if err := tc.Forge.CloseIssue(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, ""); err != nil { + if err := closeIssueOrRecordBlock(ctx, tc); err != nil { return err } tc.Outcome = Outcome{Status: StatusCompleted, Detail: "completed -- no changes required"} diff --git a/internal/domain/workflow/test_run.go b/internal/domain/workflow/test_run.go index 60ba9ba0b..83e227763 100644 --- a/internal/domain/workflow/test_run.go +++ b/internal/domain/workflow/test_run.go @@ -55,6 +55,11 @@ func (f recordedForge) CloseIssue(ctx context.Context, owner, repo string, numbe return nil } +func (f recordedForge) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, _ []string) error { + f.deliveries.Record(ctx, fmt.Sprintf("label %s/%s#%d %q", owner, repo, number, label)) + return nil +} + func (f recordedForge) CreatePR(ctx context.Context, owner, repo, title, head, base, _ string) (int, error) { f.deliveries.Record(ctx, fmt.Sprintf("open PR %q in %s/%s from %s into %s", title, owner, repo, head, base)) return 0, nil diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 17c0a3372..9c7ee96e0 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -40,6 +40,9 @@ type Forger interface { // operator why it stopped remediating, and a whole-review reply (no // single inline comment to thread onto) falls back to it. Comment(ctx context.Context, owner, repo string, number int, body string) (int64, error) + // SetStateLabel adds label to the issue; knownLabels lists the state + // labels it replaces. + SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) error // ReplyToReview posts a threaded reply to one review comment. The // remediate workflow calls it once per remediation run, summarising // what changed.