fix(workflow): record a close refused for open dependencies instead of parking (#1845)

This commit is contained in:
sam committed 2026-11-09 14:40:58 +11:00
1 parent 67c9b7a00a
commit e34941b1eb
5 files changed
+94 -2

No files matched your search

@@ -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)
}
})
}
}
+23 -1
View File
@@ -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".
+1 -1
View File
@@ -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"}
+5
View File
@@ -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
+3
View File
@@ -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.