diff --git a/cmd/goblins/cleanup.go b/cmd/goblins/cleanup.go index 363eaddc8..2580a5796 100644 --- a/cmd/goblins/cleanup.go +++ b/cmd/goblins/cleanup.go @@ -8,14 +8,18 @@ import ( "github.com/fpresta0607/code-goblins/internal/cleanup" "github.com/fpresta0607/code-goblins/internal/execx" + "github.com/fpresta0607/code-goblins/internal/gatedb" + "github.com/fpresta0607/code-goblins/internal/githubapi" "github.com/fpresta0607/code-goblins/internal/herdr" "github.com/fpresta0607/code-goblins/internal/home" + "github.com/fpresta0607/code-goblins/internal/pipeline" "github.com/fpresta0607/code-goblins/internal/state" "github.com/fpresta0607/code-goblins/internal/supervisor" + "github.com/fpresta0607/code-goblins/internal/tickets" "github.com/fpresta0607/code-goblins/internal/worktree" ) -const cleanupUsage = `usage: goblins cleanup [--force-archive] +const cleanupUsage = `usage: goblins cleanup [--force-archive] [--keep-issue-open ... --reason ""] Close the task tab and return one clean, proven-inactive task worktree, removing the worktree and pruning its Git administrative entry. @@ -29,16 +33,44 @@ task against every other command while it runs, so a pause, a resume, a kill or a switch of the task that starts meanwhile is refused, and it is refused itself, before it removes anything, while one of those is in flight. +It refuses as well, naming each, while an issue the task names is still +open on GitHub: an issue its brief names in its Task or Acceptance criteria +or in a section added to it under a heading "## ADDED", one its gate runs were started to close with --closes, and one its pull +requests name, which GitHub links them to close or their body names after +a closing word or "Part of". An issue GitHub reports gone, as a deleted +issue or a mistyped number is, refuses it too, named apart as gone on +GitHub, and so does a task whose issues cannot be read. Close each open +issue once its work is on its project's main, or retire the task with an +open or gone issue by --keep-issue-open , written owner/name#N or as +its address, once for each, with --reason "", which the task's status +log records for each kept issue. goblins kill, which stops work unfinished, +does not check issues. + --force-archive retires a task whose worktree can no longer be validated (a directory pinned by a dead handle, or already gone). It archives the task record and leaves the directory untouched - nothing on disk is deleted - and -still refuses a pane with a live agent and a gate run that is still open or -cannot be read, as cleanup does. A paused task's run is the one its pause -saved, read whatever state the worktree is in; the worktree's branch's run -is read as well while the folder is still a Git worktree, so a worktree gone, -or a folder no longer a Git worktree, with no saved run names no run. +still refuses a pane with a live agent, a gate run that is still open or +cannot be read, and an open issue, as cleanup does. A paused task's run is +the one its pause saved, read whatever state the worktree is in; the +worktree's branch's run is read as well while the folder is still a Git +worktree, so a worktree gone, or a folder no longer a Git worktree, with no +saved run names no run. ` +// cleanupRequest is one retirement of a task: goblins cleanup's, which keeps +// the task while an issue it names is open unless the CEO keeps the issue +// open with a reason, or goblins kill's, which stops work unfinished and so +// checks no issue. +type cleanupRequest struct { + ID string + ForceArchive bool + ShouldCheckIssues bool + // KeepOpen are the open issues, each owner/name#N, the task retires with, + // and KeepReason is why. + KeepOpen []string + KeepReason string +} + // runCleanup returns one validated task's worktree through the guarded // cleanup service. It accepts only a local task ID, never a raw path or an // explicit Herdr target. @@ -52,26 +84,51 @@ func runCleanup(args []string, stdout, stderr io.Writer, runtime commandRuntime) fmt.Fprint(stdout, cleanupUsage) return 0 } - var id string - force := false - for _, arg := range args { + request := cleanupRequest{ShouldCheckIssues: true} + isReasonGiven := false + for index := 0; index < len(args); index++ { + arg := args[index] switch { case arg == "--force-archive": - force = true + request.ForceArchive = true + case arg == "--keep-issue-open" || arg == "--reason": + if index+1 == len(args) { + fmt.Fprintf(stderr, "goblins cleanup: %s needs a value\n", arg) + return 2 + } + index++ + if arg == "--reason" { + request.KeepReason, isReasonGiven = strings.TrimSpace(args[index]), true + continue + } + issue, isIssue := tickets.ParseIssueRef(args[index]) + if !isIssue { + fmt.Fprintf(stderr, "goblins cleanup: --keep-issue-open %q is not an issue: write it owner/name#N or as its address\n", args[index]) + return 2 + } + request.KeepOpen = append(request.KeepOpen, issue.String()) case strings.HasPrefix(arg, "-"): fmt.Fprintf(stderr, "goblins cleanup: unknown flag %q\n", arg) return 2 - case id == "": - id = arg + case request.ID == "": + request.ID = arg default: fmt.Fprintln(stderr, "goblins cleanup: unexpected arguments") return 2 } } - if id == "" { + if request.ID == "" { fmt.Fprintln(stderr, "goblins cleanup: task ID is required") return 2 } + if len(request.KeepOpen) > 0 && request.KeepReason == "" { + fmt.Fprintln(stderr, `goblins cleanup: --keep-issue-open needs --reason "", which the task records`) + return 2 + } + if isReasonGiven && len(request.KeepOpen) == 0 { + fmt.Fprintln(stderr, "goblins cleanup: --reason says why an issue is kept open, so it needs --keep-issue-open ") + return 2 + } if runtime.resolveHome == nil || runtime.cleanup == nil { fmt.Fprintln(stderr, "goblins cleanup: command runtime is incomplete") return 1 @@ -85,7 +142,7 @@ func runCleanup(args []string, stdout, stderr io.Writer, runtime commandRuntime) fmt.Fprintln(stderr, "goblins cleanup: not a primary home") return 1 } - output, err := runtime.cleanup(context.Background(), h, id, force) + output, err := runtime.cleanup(context.Background(), h, request) if err != nil { fmt.Fprintln(stderr, err) return 1 @@ -94,8 +151,9 @@ func runCleanup(args []string, stdout, stderr io.Writer, runtime commandRuntime) return 0 } -// defaultCleanup builds the production cleanup service for one invocation. -func defaultCleanup(ctx context.Context, h home.Home, id string, forceArchive bool) (string, error) { +// defaultCleanup builds the production cleanup service for one invocation; +// github reads the issues of a request that checks them. +func defaultCleanup(ctx context.Context, h home.Home, request cleanupRequest, github *githubapi.Client) (string, error) { commands := execx.OSRunner{} service := cleanup.Service{ StateDir: h.State, @@ -103,7 +161,9 @@ func defaultCleanup(ctx context.Context, h home.Home, id string, forceArchive bo Commands: commands, Terminal: &herdr.Client{Commands: commands, Session: herdrSession()}, Worktrees: worktree.Service{Commands: commands}, - ForceArchive: forceArchive, + ForceArchive: request.ForceArchive, + KeepOpen: request.KeepOpen, + KeepReason: request.KeepReason, EndLeft: func(ctx context.Context, meta state.TaskMeta) ([]string, error) { return endLeftProcesses(ctx, h, meta) @@ -114,7 +174,16 @@ func defaultCleanup(ctx context.Context, h home.Home, id string, forceArchive bo }, OpenGateRun: openGateRun(h), } - result, err := service.Cleanup(ctx, id) + if request.ShouldCheckIssues { + gate := pipeline.Reader{Commands: gatedb.Runner{Next: commands}} + root, err := pipeline.DefaultRoot() + if err != nil { + return "", fmt.Errorf("cleanup: find the gate's home, whose runs say which issues the task closes: %w", err) + } + gate.Root = root + service.OpenIssues = openTaskIssues(h, tickets.GitHub{Commands: commands, API: github}, gate) + } + result, err := service.Cleanup(ctx, request.ID) if err != nil { return "", err } diff --git a/cmd/goblins/cleanup_kill_test.go b/cmd/goblins/cleanup_kill_test.go index 72c97c25e..f11f33de4 100644 --- a/cmd/goblins/cleanup_kill_test.go +++ b/cmd/goblins/cleanup_kill_test.go @@ -7,10 +7,13 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "testing" "github.com/fpresta0607/code-goblins/internal/execx" + "github.com/fpresta0607/code-goblins/internal/fsx" + "github.com/fpresta0607/code-goblins/internal/githubtest" "github.com/fpresta0607/code-goblins/internal/herdr" "github.com/fpresta0607/code-goblins/internal/home" "github.com/fpresta0607/code-goblins/internal/lifecycle" @@ -26,7 +29,12 @@ import ( func killedTask(t *testing.T) (home.Home, state.TaskMeta) { t.Helper() t.Setenv("NM_HOME", t.TempDir()) - root := t.TempDir() + // git reports the canonical path of a checkout, so the task's paths are + // built from the canonical temp root, not a short name or a link to it. + root, err := fsx.Canonical(t.TempDir()) + if err != nil { + t.Fatal(err) + } git := func(dir string, args ...string) { t.Helper() command := exec.Command("git", args...) @@ -96,7 +104,7 @@ func TestACleanupOfAPausedGoblinHandsItsKeptMessagesToTheCEO(t *testing.T) { pausedWithKeptMessage(t, h, meta) // Act - output, err := defaultCleanup(t.Context(), h, meta.ID, false) + output, err := defaultCleanup(t.Context(), h, cleanupRequest{ID: meta.ID}, nil) // Assert if err != nil { @@ -122,7 +130,7 @@ func TestAKillOfAPausedGoblinHandsItsKeptMessagesToTheCEOOnce(t *testing.T) { service := lifecycle.Service{StateDir: h.State, Operations: lifecycle.Operations{ Stop: func(context.Context, state.TaskMeta, *state.Lifecycle) ([]string, error) { return nil, nil }, Archive: func(ctx context.Context, meta state.TaskMeta, _ *state.Lifecycle) (lifecycle.Preservation, error) { - _, err := defaultCleanup(ctx, h, meta.ID, false) + _, err := defaultCleanup(ctx, h, cleanupRequest{ID: meta.ID}, nil) return lifecycle.Preservation{}, err }, Notify: func(state.Lifecycle) error { return nil }, @@ -164,7 +172,7 @@ func TestKillStillArchivesThroughItsOwnCleanup(t *testing.T) { heldDuringArchive = append(heldDuringArchive, name) } } - _, err := defaultCleanup(ctx, h, meta.ID, false) + _, err := defaultCleanup(ctx, h, cleanupRequest{ID: meta.ID}, nil) return lifecycle.Preservation{}, err }, Notify: func(state.Lifecycle) error { return nil }, @@ -193,6 +201,101 @@ func TestKillStillArchivesThroughItsOwnCleanup(t *testing.T) { } } +// goblins cleanup reads the issues its task names through the production +// service, refusing while one is open and retiring the task with it kept open +// for a reason the status log records, while goblins kill, which stops work +// unfinished, reads none, so it retires a task whose issues cannot be read. +func TestCleanupChecksTheIssuesItsTaskNamesAndKillDoesNot(t *testing.T) { + const issue = "acme/widgets#7" + withOpenIssue := func(t *testing.T) (home.Home, state.TaskMeta) { + t.Helper() + h, meta := killedTask(t) + writeFixtureFile(t, filepath.Join(h.Data, meta.ID, "brief.md"), "# Brief g1\n\n## Task\n\nFix https://github.com/acme/widgets/issues/7.\n") + return h, meta + } + github := githubtest.Client(taskIssuesGitHub{states: map[string]string{issue: "open"}}) + + t.Run("goblins cleanup refuses while the issue is open", func(t *testing.T) { + // Arrange + h, meta := withOpenIssue(t) + + // Act + _, err := defaultCleanup(t.Context(), h, cleanupRequest{ID: meta.ID, ShouldCheckIssues: true}, github) + + // Assert + if err == nil || !strings.Contains(err.Error(), "names the open issue(s) "+issue) { + t.Fatalf("the cleanup = %v, want a refusal naming %s", err, issue) + } + if _, err := state.ReadTaskMeta(h.State, meta.ID); err != nil { + t.Errorf("the task record after a refused cleanup: %v, want it kept", err) + } + }) + t.Run("goblins cleanup refuses while an issue only an added brief section names is open", func(t *testing.T) { + // Arrange + h, meta := killedTask(t) + writeFixtureFile(t, filepath.Join(h.Data, meta.ID, "brief.md"), "# Brief g1\n\n## Task\n\nTidy the docs.\n\n## ADDED 2026-11-19 - the stats page\nIssue: https://github.com/acme/widgets/issues/7.\nThe other task (#185) adds a picker, so leave it.\n") + + // Act + _, err := defaultCleanup(t.Context(), h, cleanupRequest{ID: meta.ID, ShouldCheckIssues: true}, github) + + // Assert + if err == nil || !strings.Contains(err.Error(), "names the open issue(s) "+issue) { + t.Fatalf("the cleanup = %v, want a refusal naming %s", err, issue) + } + if strings.Contains(err.Error(), "#185") { + t.Errorf("the cleanup refusal %v names #185, a bare number in the added section's prose", err) + } + if _, err := state.ReadTaskMeta(h.State, meta.ID); err != nil { + t.Errorf("the task record after a refused cleanup: %v, want it kept", err) + } + }) + t.Run("goblins cleanup retires the task with the issue kept open and records why", func(t *testing.T) { + // Arrange + h, meta := withOpenIssue(t) + request := cleanupRequest{ID: meta.ID, ShouldCheckIssues: true, KeepOpen: []string{issue}, KeepReason: "task g2 carries it on"} + + // Act + output, err := defaultCleanup(t.Context(), h, request, github) + + // Assert + if err != nil { + t.Fatalf("the cleanup = %q, %v, want the task retired", output, err) + } + status, err := state.TailStatus(h.State, meta.ID, 10) + if err != nil || !slices.ContainsFunc(status, func(line string) bool { + _, event := state.SplitStatus(line) + return event == "issue-kept-open: "+issue+": task g2 carries it on" + }) { + t.Errorf("status = %q, %v; want the kept issue recorded with its reason", status, err) + } + if _, err := state.ReadTaskMeta(h.State, meta.ID); !errors.Is(err, os.ErrNotExist) { + t.Errorf("the task record after the cleanup: %v, want it retired", err) + } + }) + t.Run("goblins kill retires a task whose issues cannot even be read", func(t *testing.T) { + // Arrange: the brief the record names is a folder, so a kill that read + // the task's issues would refuse there, before it could ask GitHub + // anything. + h, meta := killedTask(t) + meta.Brief = t.TempDir() + if err := state.WriteTaskMeta(h.State, meta); err != nil { + t.Fatal(err) + } + t.Setenv("GOBLINS_VERIFY_DIR", t.TempDir()) + + // Act + record, err := defaultTaskLifecycle(t.Context(), h, lifecycle.Request{ID: meta.ID, Generation: meta.SpawnGen, Operation: "kill-1", Action: "stop", Reason: "Requested by the operator", IsWatched: true}, "") + + // Assert + if err != nil || record.Phase != "stopped" { + t.Fatalf("the kill = phase %q, %v, want the task retired\nproblems: %s", record.Phase, err, strings.Join(record.Problems, "; ")) + } + if _, err := state.ReadTaskMeta(h.State, meta.ID); !errors.Is(err, os.ErrNotExist) { + t.Errorf("the task record after the kill: %v, want it retired", err) + } + }) +} + // A pause leaves its goblin's gate run running, so goblins cleanup, and the // sweep's cleanup of a task it found, read the task's gate run before they // change anything. A run that completed or was cancelled, as a landed or @@ -221,7 +324,9 @@ func TestCleanupReadsTheTaskBranchsGateRunBeforeRetiringIt(t *testing.T) { name string clean func(ctx context.Context, h home.Home, id string, forceArchive bool) (string, error) }{ - {name: "goblins cleanup", clean: defaultCleanup}, + {name: "goblins cleanup", clean: func(ctx context.Context, h home.Home, id string, forceArchive bool) (string, error) { + return defaultCleanup(ctx, h, cleanupRequest{ID: id, ForceArchive: forceArchive}, nil) + }}, {name: "goblins reap", clean: func(ctx context.Context, h home.Home, id string, forceArchive bool) (string, error) { return reapCleanup(ctx, h, &herdr.Client{Commands: execx.OSRunner{}, Session: herdrSession()}, id, forceArchive) }}, diff --git a/cmd/goblins/cleanup_test.go b/cmd/goblins/cleanup_test.go index 2cd873e51..70b70e878 100644 --- a/cmd/goblins/cleanup_test.go +++ b/cmd/goblins/cleanup_test.go @@ -7,13 +7,14 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "testing" "github.com/fpresta0607/code-goblins/internal/home" ) -func cleanupTestRuntime(h home.Home, cleanup func(context.Context, home.Home, string, bool) (string, error)) commandRuntime { +func cleanupTestRuntime(h home.Home, cleanup func(context.Context, home.Home, cleanupRequest) (string, error)) commandRuntime { return commandRuntime{ resolveHome: func() (home.Home, error) { return h, nil }, cleanup: cleanup, @@ -77,7 +78,7 @@ func TestCleanupArgumentValidation(t *testing.T) { func TestCleanupRefusesNonPrimaryHome(t *testing.T) { h := home.Home{Root: t.TempDir(), State: t.TempDir(), Data: t.TempDir()} called := false - runtime := cleanupTestRuntime(h, func(context.Context, home.Home, string, bool) (string, error) { + runtime := cleanupTestRuntime(h, func(context.Context, home.Home, cleanupRequest) (string, error) { called = true return "", nil }) @@ -95,8 +96,8 @@ func TestCleanupRefusesNonPrimaryHome(t *testing.T) { func TestCleanupDelegatesToService(t *testing.T) { h := primaryHomeFixture(t) var gotID string - runtime := cleanupTestRuntime(h, func(_ context.Context, home home.Home, id string, _ bool) (string, error) { - gotID = id + runtime := cleanupTestRuntime(h, func(_ context.Context, home home.Home, request cleanupRequest) (string, error) { + gotID = request.ID if home.State != h.State { t.Errorf("home = %+v, want the resolved home", home) } @@ -118,7 +119,7 @@ func TestCleanupDelegatesToService(t *testing.T) { func TestCleanupSurfacesServiceFailure(t *testing.T) { h := primaryHomeFixture(t) - runtime := cleanupTestRuntime(h, func(context.Context, home.Home, string, bool) (string, error) { + runtime := cleanupTestRuntime(h, func(context.Context, home.Home, cleanupRequest) (string, error) { return "", errors.New("cleanup: worktree is dirty") }) @@ -128,3 +129,60 @@ func TestCleanupSurfacesServiceFailure(t *testing.T) { t.Fatalf("exit=%d stdout=%q stderr=%q, want the service refusal on stderr", exit, stdout.String(), stderr.String()) } } + +func TestCleanupPassesTheKeptIssuesAndReason(t *testing.T) { + // Arrange + h := primaryHomeFixture(t) + var got cleanupRequest + runtime := cleanupTestRuntime(h, func(_ context.Context, _ home.Home, request cleanupRequest) (string, error) { + got = request + return "cleaned g1", nil + }) + var stdout, stderr bytes.Buffer + + // Act + exit := runCleanup([]string{"g1", "--keep-issue-open", "https://github.com/Acme/Widgets/issues/7", "--keep-issue-open", "acme/widgets#8", "--reason", "its second half is task g2's"}, &stdout, &stderr, runtime) + + // Assert + if exit != 0 { + t.Fatalf("exit=%d stderr=%q, want success", exit, stderr.String()) + } + want := cleanupRequest{ID: "g1", ShouldCheckIssues: true, KeepOpen: []string{"acme/widgets#7", "acme/widgets#8"}, KeepReason: "its second half is task g2's"} + if got.ID != want.ID || !got.ShouldCheckIssues || got.ForceArchive || !slices.Equal(got.KeepOpen, want.KeepOpen) || got.KeepReason != want.KeepReason { + t.Fatalf("request = %+v, want %+v", got, want) + } +} + +func TestCleanupRefusesAMalformedKeep(t *testing.T) { + tests := []struct { + name string + args []string + want string + }{ + {"keep without reason", []string{"g1", "--keep-issue-open", "acme/widgets#7"}, "--keep-issue-open needs --reason"}, + {"blank reason", []string{"g1", "--keep-issue-open", "acme/widgets#7", "--reason", " "}, "--keep-issue-open needs --reason"}, + {"reason without keep", []string{"g1", "--reason", "why"}, "it needs --keep-issue-open"}, + {"bare number", []string{"g1", "--keep-issue-open", "7", "--reason", "why"}, `"7" is not an issue`}, + {"pull request address", []string{"g1", "--keep-issue-open", "https://github.com/acme/widgets/pull/7", "--reason", "why"}, "is not an issue"}, + {"keep with no value", []string{"g1", "--keep-issue-open"}, "--keep-issue-open needs a value"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + // Arrange + called := false + runtime := cleanupTestRuntime(primaryHomeFixture(t), func(context.Context, home.Home, cleanupRequest) (string, error) { + called = true + return "", nil + }) + var stdout, stderr bytes.Buffer + + // Act + exit := runCleanup(test.args, &stdout, &stderr, runtime) + + // Assert + if exit != 2 || !strings.Contains(stderr.String(), test.want) || called { + t.Fatalf("exit=%d stderr=%q called=%v, want 2 with %q and no cleanup", exit, stderr.String(), called, test.want) + } + }) + } +} diff --git a/cmd/goblins/issue.go b/cmd/goblins/issue.go index 36e112dd6..3dbb2ad6a 100644 --- a/cmd/goblins/issue.go +++ b/cmd/goblins/issue.go @@ -30,8 +30,11 @@ const issueUsage = "goblins issue file --project --title [--bo var secretName = regexp.MustCompile(`(?i)token|secret|password|passwd|credential|api_?key|access_?key|private_?key|_pat$|^pat_`) func runIssue(args []string, stdout, stderr io.Writer, runtime commandRuntime) int { + if len(args) > 0 && args[0] == "unowned" { + return runIssueUnowned(args[1:], stdout, stderr, runtime) + } if len(args) == 0 || args[0] != "file" { - fmt.Fprintf(stderr, "goblins issue: %s\n", issueUsage) + fmt.Fprintf(stderr, "goblins issue: %s | %s\n", issueUsage, issueUnownedUsage) return 2 } fs := flag.NewFlagSet("issue file", flag.ContinueOnError) diff --git a/cmd/goblins/issue_unowned.go b/cmd/goblins/issue_unowned.go new file mode 100644 index 000000000..6f0004c8f --- /dev/null +++ b/cmd/goblins/issue_unowned.go @@ -0,0 +1,320 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/fpresta0607/code-goblins/internal/execx" + "github.com/fpresta0607/code-goblins/internal/fleet" + "github.com/fpresta0607/code-goblins/internal/home" + "github.com/fpresta0607/code-goblins/internal/state" + "github.com/fpresta0607/code-goblins/internal/tickets" +) + +const issueUnownedUsage = "goblins issue unowned" + +// unownedIssue is one open issue the listing names, with the tasks that are +// neither live nor queued that name it. +type unownedIssue struct { + Number int + Title string + URL string + Tasks []string +} + +// unownedRepository is one GitHub repository of the home's projects as the +// listing read it: how many issues are open, how many of them carry the +// root clean-up's label, how many a live or queued task names, and apart +// the ones only finished or parked tasks name and the ones no task names. +type unownedRepository struct { + Repository string + Projects []string + Open int + Roots int + Owned int + FinishedOnly []unownedIssue + Unnamed []unownedIssue + Problem string +} + +// unownedListing is the whole listing: each repository, and what could not +// be read, which leaves the issues it would have named listed as unnamed. +type unownedListing struct { + Repositories []unownedRepository + Problems []string +} + +// runIssueUnowned lists, for every project of the home, the open issues +// without the root clean-up's label that no live or queued task names, and +// apart those only finished or parked tasks name. It reads only. +func runIssueUnowned(args []string, stdout, stderr io.Writer, runtime commandRuntime) int { + if len(args) != 0 { + fmt.Fprintf(stderr, "goblins issue unowned: %s\n", issueUnownedUsage) + return 2 + } + if runtime.resolveHome == nil || runtime.repositoryOf == nil || runtime.githubAPI == nil { + fmt.Fprintln(stderr, "goblins issue unowned: command runtime is incomplete") + return 1 + } + h, err := runtime.resolveHome() + if err != nil { + fmt.Fprintf(stderr, "goblins issue unowned: %v\n", err) + return 1 + } + listing, err := listUnownedIssues(context.Background(), h, runtime) + if err != nil { + fmt.Fprintf(stderr, "goblins issue unowned: %v\n", err) + return 1 + } + renderUnownedIssues(stdout, listing) + return 0 +} + +// listUnownedIssues reads the home's projects, its live, queued, parked and +// finished tasks and what GitHub says of their pull requests and of each +// repository's open issues. +func listUnownedIssues(ctx context.Context, h home.Home, runtime commandRuntime) (unownedListing, error) { + commands := runtime.github + if commands == nil { + commands = execx.OSRunner{} + } + github := tickets.GitHub{Commands: commands, API: runtime.githubAPI} + var listing unownedListing + repositories := map[string]string{} + repositoryOf := func(checkout string) string { + key := strings.ToLower(filepath.Clean(checkout)) + if repository, isKnown := repositories[key]; isKnown { + return repository + } + repository := "" + if _, err := os.Stat(checkout); err == nil { + if repository, err = issueRepository(ctx, h, runtime.repositoryOf, checkout); err != nil { + listing.Problems = append(listing.Problems, fmt.Sprintf("the repository of %s cannot be read: %v", checkout, err)) + } + } + repositories[key] = repository + return repository + } + projectRepository := func(name string) string { + if name == "" { + return "" + } + checkout, err := runtime.resolveProject(name) + if err != nil { + return "" + } + return repositoryOf(checkout) + } + + entries, err := os.ReadDir(filepath.Join(h.Data, "projects")) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return unownedListing{}, err + } + for _, entry := range entries { + if !entry.IsDir() { + continue + } + checkout, err := runtime.resolveProject(entry.Name()) + if err != nil { + listing.Problems = append(listing.Problems, fmt.Sprintf("project %s is not read: %v", entry.Name(), err)) + continue + } + repository := repositoryOf(checkout) + if repository == "" { + continue + } + index := slices.IndexFunc(listing.Repositories, func(known unownedRepository) bool { return known.Repository == repository }) + if index < 0 { + listing.Repositories = append(listing.Repositories, unownedRepository{Repository: repository}) + index = len(listing.Repositories) - 1 + } + listing.Repositories[index].Projects = append(listing.Repositories[index].Projects, entry.Name()) + } + + owners, others, err := readNamingTasks(h, repositoryOf, projectRepository, &listing) + if err != nil { + return unownedListing{}, err + } + var pulls []string + for _, task := range append(slices.Clone(owners), others...) { + pulls = append(pulls, task.PullRequests...) + } + pullIssues, unread, err := github.PullRequestIssues(ctx, pulls) + if err != nil { + return unownedListing{}, err + } + if len(unread) > 0 { + listing.Problems = append(listing.Problems, fmt.Sprintf("GitHub shows no pull request at %d of the tasks' addresses, so what they name is not read: %s", len(unread), strings.Join(unread, ", "))) + } + isOwned := map[tickets.IssueRef]bool{} + for _, task := range owners { + for _, issue := range task.issues(pullIssues) { + isOwned[issue] = true + } + } + namedBy := map[tickets.IssueRef][]string{} + for _, task := range others { + for _, issue := range task.issues(pullIssues) { + namedBy[issue] = append(namedBy[issue], task.ID) + } + } + + for index := range listing.Repositories { + repository := &listing.Repositories[index] + open, err := github.OpenIssues(ctx, repository.Repository) + if err != nil { + repository.Problem = err.Error() + continue + } + repository.Open = len(open) + for _, issue := range open { + ref := tickets.IssueRef{Repository: repository.Repository, Number: issue.Number} + listed := unownedIssue{Number: issue.Number, Title: issue.Title, URL: issue.URL, Tasks: namedBy[ref]} + switch { + case slices.Contains(issue.Labels, tickets.RootLabel): + repository.Roots++ + case isOwned[ref]: + repository.Owned++ + case len(listed.Tasks) > 0: + repository.FinishedOnly = append(repository.FinishedOnly, listed) + default: + repository.Unnamed = append(repository.Unnamed, listed) + } + } + } + return listing, nil +} + +// readNamingTasks reads the home's tasks as the issues they name: owners, +// the live tasks and the queued rows, and others, the parked rows and the +// finished tasks, each a task whose record, status log, outcome or Done row +// the home still keeps. A task named twice is read once, with every row it +// has. +func readNamingTasks(h home.Home, repositoryOf, projectRepository func(string) string, listing *unownedListing) (owners, others []namingTask, err error) { + scan, err := state.ScanIDs(h.State) + if err != nil { + return nil, nil, err + } + backlog, err := fleet.ReadBacklog(h) + if err != nil { + return nil, nil, err + } + tasks := map[string]*namingTask{} + var order []string + isOwner := map[string]bool{} + read := func(id, repository, briefPath string) (*namingTask, error) { + if task, isRead := tasks[id]; isRead { + return task, nil + } + task := &namingTask{ID: id, Repository: repository} + var err error + if task.Brief, err = taskBrief(h, id, briefPath); err != nil { + return nil, err + } + if task.PullRequests, err = taskPullRequests(h.State, id); err != nil { + return nil, err + } + tasks[id] = task + order = append(order, id) + return task, nil + } + for _, id := range scan.MetaIDs { + meta, err := state.ReadTaskMeta(h.State, id) + if err != nil { + listing.Problems = append(listing.Problems, fmt.Sprintf("the record of live task %s cannot be read, so the issues it names are not left out: %v", id, err)) + continue + } + if _, err := read(id, repositoryOf(meta.Project), meta.Brief); err != nil { + return nil, nil, err + } + isOwner[id] = true + } + rows := []struct { + rows []fleet.BacklogRow + isOwner bool + }{{backlog.Queued, true}, {backlog.Parked, false}, {backlog.Done, false}} + for _, section := range rows { + for _, row := range section.rows { + if !row.Structured || state.ValidTaskID(row.ID) != nil { + continue + } + task, err := read(row.ID, projectRepository(row.Repo), "") + if err != nil { + return nil, nil, err + } + if task.Repository == "" { + task.Repository = projectRepository(row.Repo) + } + task.Row = strings.TrimSpace(task.Row + "\n" + row.Raw) + isOwner[row.ID] = isOwner[row.ID] || section.isOwner + } + } + outcomes, err := os.ReadDir(filepath.Join(h.State, "outcomes")) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, nil, err + } + finished := slices.Clone(scan.OrphanStatusIDs) + for _, entry := range outcomes { + if id, isOutcome := strings.CutSuffix(entry.Name(), ".json"); isOutcome && !entry.IsDir() { + finished = append(finished, id) + } + } + for _, id := range finished { + if state.ValidTaskID(id) != nil { + continue + } + repository := "" + if outcome, err := state.ReadOutcome(h.State, id); err == nil { + repository = repositoryOf(outcome.Project) + } + task, err := read(id, repository, "") + if err != nil { + return nil, nil, err + } + if task.Repository == "" { + task.Repository = repository + } + } + for _, id := range order { + if isOwner[id] { + owners = append(owners, *tasks[id]) + } else { + others = append(others, *tasks[id]) + } + } + return owners, others, nil +} + +// renderUnownedIssues writes the listing as one block per repository. +func renderUnownedIssues(w io.Writer, listing unownedListing) { + for _, repository := range listing.Repositories { + fmt.Fprintf(w, "%s (%s): ", repository.Repository, strings.Join(repository.Projects, ", ")) + if repository.Problem != "" { + fmt.Fprintf(w, "not read: %s\n", repository.Problem) + continue + } + fmt.Fprintf(w, "%d open, %d %s, %d named by live or queued tasks, %d named only by finished or parked tasks, %d named by no task\n", repository.Open, repository.Roots, tickets.RootLabel, repository.Owned, len(repository.FinishedOnly), len(repository.Unnamed)) + if len(repository.FinishedOnly) > 0 { + fmt.Fprintln(w, " named only by finished or parked tasks:") + for _, issue := range repository.FinishedOnly { + fmt.Fprintf(w, " #%d %s (%s) %s\n", issue.Number, issue.Title, strings.Join(issue.Tasks, ", "), issue.URL) + } + } + if len(repository.Unnamed) > 0 { + fmt.Fprintln(w, " named by no task:") + for _, issue := range repository.Unnamed { + fmt.Fprintf(w, " #%d %s %s\n", issue.Number, issue.Title, issue.URL) + } + } + } + for _, problem := range listing.Problems { + fmt.Fprintf(w, "not read: %s\n", problem) + } +} diff --git a/cmd/goblins/issue_unowned_test.go b/cmd/goblins/issue_unowned_test.go new file mode 100644 index 000000000..3fc99839b --- /dev/null +++ b/cmd/goblins/issue_unowned_test.go @@ -0,0 +1,225 @@ +package main + +import ( + "bytes" + "context" + "net/http" + "os" + "path/filepath" + "strconv" + "strings" + "testing" + + "github.com/fpresta0607/code-goblins/internal/githubtest" + "github.com/fpresta0607/code-goblins/internal/home" + "github.com/fpresta0607/code-goblins/internal/state" +) + +// unownedGitHub answers the listing's reads: what each pull request names, +// and the open issues of acme/widgets. +type unownedGitHub struct { + pulls map[string]map[string]any + issues []map[string]any + calls []string +} + +func (g *unownedGitHub) ServeHTTP(w http.ResponseWriter, r *http.Request) { + g.calls = append(g.calls, r.Method+" "+r.URL.RequestURI()) + switch { + case r.Method == http.MethodPost && r.URL.Path == "/graphql": + _, variables, err := githubtest.Query(r) + if err != nil { + githubtest.Refuse(w, http.StatusBadRequest, err.Error()) + return + } + data := map[string]any{} + for name, address := range variables { + data["pull"+strings.TrimPrefix(name, "url")] = g.pulls[address.(string)] + } + githubtest.Answer(w, data) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widgets/issues" && r.URL.Query().Get("page") == "1": + githubtest.Write(w, http.StatusOK, g.issues) + default: + githubtest.Refuse(w, http.StatusNotFound, "Not Found") + } +} + +func openIssue(number int, title string, labels ...string) map[string]any { + listed := []map[string]any{} + for _, label := range labels { + listed = append(listed, map[string]any{"name": label}) + } + return map[string]any{"number": number, "title": title, "html_url": "https://github.com/acme/widgets/issues/" + strconv.Itoa(number), "state": "open", "labels": listed} +} + +func writeFixtureFile(t *testing.T, path, text string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(text), 0o600); err != nil { + t.Fatal(err) + } +} + +// unownedHome is a home with one project, widgets, whose issues go on +// acme/widgets: a live task whose brief names issue 1 and whose pull request +// closes issue 4, a queued row that names issue 2, and a finished task whose +// pull request is part of issue 3. +func unownedHome(t *testing.T, github *unownedGitHub) (commandRuntime, home.Home) { + t.Helper() + root := t.TempDir() + checkout := filepath.Join(root, "widgets") + if err := os.MkdirAll(filepath.Join(checkout, ".git"), 0o700); err != nil { + t.Fatal(err) + } + base := t.TempDir() + h := home.Home{Root: base, Data: filepath.Join(base, "data"), State: filepath.Join(base, "state")} + if err := os.MkdirAll(filepath.Join(h.Data, "projects", "widgets"), 0o700); err != nil { + t.Fatal(err) + } + brief := filepath.Join(h.State, "tasktmp", "live-task", "brief.md") + writeFixtureFile(t, brief, "# Brief live-task\n\n## Task\n\nFix https://github.com/acme/widgets/issues/1.\n") + if err := state.WriteMeta(state.TaskMetaPath(h.State, "live-task"), map[string]string{"project": checkout, "brief": brief, "backend": "native"}); err != nil { + t.Fatal(err) + } + if err := state.AppendStatus(h.State, "live-task", "done: PR https://github.com/acme/widgets/pull/51"); err != nil { + t.Fatal(err) + } + writeFixtureFile(t, filepath.Join(h.Data, "backlog.md"), "# Backlog\n\n## Queued\n- [ ] queued-task - Widgets: sort the list, issue 2 (repo: widgets)\n\n## Done\n") + if err := state.WriteOutcome(h.State, state.Outcome{ID: "done-task", Project: checkout, Phase: "done", PR: "https://github.com/acme/widgets/pull/50", Evidence: "recorded pull request"}); err != nil { + t.Fatal(err) + } + return commandRuntime{ + resolveHome: func() (home.Home, error) { return h, nil }, + projectsRoot: func() (string, error) { return root, nil }, + repositoryOf: func(context.Context, string) (string, error) { return "acme/widgets", nil }, + githubAPI: githubtest.Client(github), + }, h +} + +func TestIssueUnownedLeavesOutRootsAndIssuesALiveOrQueuedTaskNames(t *testing.T) { + // Arrange + github := &unownedGitHub{ + pulls: map[string]map[string]any{ + "https://github.com/acme/widgets/pull/50": {"url": "https://github.com/acme/widgets/pull/50", "body": "Part of #3", "closingIssuesReferences": map[string]any{"nodes": []any{}}}, + "https://github.com/acme/widgets/pull/51": {"url": "https://github.com/acme/widgets/pull/51", "body": "", "closingIssuesReferences": map[string]any{"nodes": []any{map[string]any{"number": 4, "repository": map[string]any{"nameWithOwner": "acme/widgets"}}}}}, + }, + issues: []map[string]any{ + openIssue(1, "Named by the live brief"), + openIssue(2, "Named by the queued row"), + openIssue(3, "Named only by a finished pull request"), + openIssue(4, "Closed by the live pull request"), + openIssue(5, "A root", "ops-root"), + openIssue(6, "Named by no task"), + {"number": 7, "title": "A pull request", "html_url": "https://github.com/acme/widgets/pull/7", "state": "open", "pull_request": map[string]any{}}, + }, + } + runtime, _ := unownedHome(t, github) + var stdout, stderr bytes.Buffer + + // Act + exit := runIssue([]string{"unowned"}, &stdout, &stderr, runtime) + + // Assert + if exit != 0 { + t.Fatalf("exit=%d stderr=%q, want 0", exit, stderr.String()) + } + output := stdout.String() + for _, want := range []string{ + "acme/widgets (widgets): 6 open, 1 ops-root, 3 named by live or queued tasks, 1 named only by finished or parked tasks, 1 named by no task", + " #3 Named only by a finished pull request (done-task) https://github.com/acme/widgets/issues/3", + " #6 Named by no task https://github.com/acme/widgets/issues/6", + } { + if !strings.Contains(output, want) { + t.Errorf("output lacks %q:\n%s", want, output) + } + } + for _, unwanted := range []string{"#1 ", "#2 ", "#4 ", "#5 ", "#7 "} { + if strings.Contains(output, unwanted) { + t.Errorf("output lists %q, which it leaves out:\n%s", unwanted, output) + } + } + for _, call := range github.calls { + if !strings.HasPrefix(call, "POST /graphql") && !strings.HasPrefix(call, "GET /repos/acme/widgets/issues") { + t.Errorf("the listing made the request %s, want only reads of pull requests and open issues", call) + } + } +} + +func TestIssueUnownedCountsTheIssuesAnAddedBriefSectionNamesInWordsAsOwned(t *testing.T) { + // Arrange: the live task's brief gains a section the CEO added, whose + // heading and body name issues 8, 9 and 10 in words, beside a bare #11 in + // its prose and a "## Added context" section naming issue 12, neither of + // which is an issue the task takes up. + github := &unownedGitHub{ + pulls: map[string]map[string]any{ + "https://github.com/acme/widgets/pull/50": {"url": "https://github.com/acme/widgets/pull/50", "body": "", "closingIssuesReferences": map[string]any{"nodes": []any{}}}, + "https://github.com/acme/widgets/pull/51": {"url": "https://github.com/acme/widgets/pull/51", "body": "", "closingIssuesReferences": map[string]any{"nodes": []any{}}}, + }, + issues: []map[string]any{ + openIssue(8, "Named in the added heading"), + openIssue(9, "Named in the added heading's list"), + openIssue(10, "Named in the added body"), + openIssue(11, "Named only as a bare number in added prose"), + openIssue(12, "Named only under a heading in another case"), + }, + } + runtime, h := unownedHome(t, github) + writeFixtureFile(t, filepath.Join(h.State, "tasktmp", "live-task", "brief.md"), "# Brief live-task\n\n## Task\n\nTidy the docs.\n\n## ADDED 2026-11-19 - the stats page, issues 8 and 9\nAlso take Issue 10, the tab left open.\nThe other task (#11) adds a picker, so leave it.\n\n## Added context\nSee issue 12 for background.\n") + var stdout, stderr bytes.Buffer + + // Act + exit := runIssue([]string{"unowned"}, &stdout, &stderr, runtime) + + // Assert + if exit != 0 { + t.Fatalf("exit=%d stderr=%q, want 0", exit, stderr.String()) + } + output := stdout.String() + for _, want := range []string{ + "acme/widgets (widgets): 5 open, 0 ops-root, 3 named by live or queued tasks, 0 named only by finished or parked tasks, 2 named by no task", + " #11 Named only as a bare number in added prose https://github.com/acme/widgets/issues/11", + " #12 Named only under a heading in another case https://github.com/acme/widgets/issues/12", + } { + if !strings.Contains(output, want) { + t.Errorf("output lacks %q:\n%s", want, output) + } + } + for _, unwanted := range []string{"#8 ", "#9 ", "#10 "} { + if strings.Contains(output, unwanted) { + t.Errorf("output lists %q, which the added section names:\n%s", unwanted, output) + } + } +} + +func TestIssueUnownedSaysWhatItCouldNotRead(t *testing.T) { + // Arrange + github := &unownedGitHub{issues: []map[string]any{openIssue(6, "Named by no task")}} + runtime, _ := unownedHome(t, github) + var stdout, stderr bytes.Buffer + + // Act + exit := runIssue([]string{"unowned"}, &stdout, &stderr, runtime) + + // Assert + if exit != 0 { + t.Fatalf("exit=%d stderr=%q, want 0", exit, stderr.String()) + } + if want := "GitHub shows no pull request at 2 of the tasks' addresses"; !strings.Contains(stdout.String(), want) { + t.Fatalf("output lacks %q:\n%s", want, stdout.String()) + } +} + +func TestIssueUnownedRefusesUnknownArguments(t *testing.T) { + // Arrange + var stdout, stderr bytes.Buffer + + // Act + exit := runIssue([]string{"unowned", "--all"}, &stdout, &stderr, commandRuntime{}) + + // Assert + if exit != 2 || !strings.Contains(stderr.String(), issueUnownedUsage) { + t.Fatalf("exit=%d stderr=%q, want 2 with the usage", exit, stderr.String()) + } +} diff --git a/cmd/goblins/lifecycle_source_resume_windows_test.go b/cmd/goblins/lifecycle_source_resume_windows_test.go index 0d28e3789..9f60be0e4 100644 --- a/cmd/goblins/lifecycle_source_resume_windows_test.go +++ b/cmd/goblins/lifecycle_source_resume_windows_test.go @@ -671,7 +671,7 @@ INSERT INTO findings VALUES('original-run','ci-1','unresolved');`, strings.Repla } launches := 0 var resumed spawn.SwitchRequest - runtime := commandRuntime{resolveHome: func() (home.Home, error) { return h, nil }, cleanup: defaultCleanup, switchTask: func(_ context.Context, _ home.Home, request spawn.SwitchRequest) (spawn.SwitchResult, error) { + runtime := commandRuntime{resolveHome: func() (home.Home, error) { return h, nil }, cleanup: func(ctx context.Context, h home.Home, request cleanupRequest) (string, error) { return defaultCleanup(ctx, h, request, nil) }, switchTask: func(_ context.Context, _ home.Home, request spawn.SwitchRequest) (spawn.SwitchResult, error) { if test.isProviderRefused { return spawn.SwitchResult{}, errors.New("provider refused this session") } diff --git a/cmd/goblins/lifecycle_windows.go b/cmd/goblins/lifecycle_windows.go index d2b1c7c3c..b34065878 100644 --- a/cmd/goblins/lifecycle_windows.go +++ b/cmd/goblins/lifecycle_windows.go @@ -120,7 +120,7 @@ func defaultTaskLifecycle(ctx context.Context, h home.Home, request lifecycle.Re } else if !errors.Is(err, fleet.ErrNotQueued) && !errors.Is(err, os.ErrNotExist) { return preserved, err } - _, err = runtime.cleanup(ctx, h, meta.ID, !preserved.CanRemove) + _, err = runtime.cleanup(ctx, h, cleanupRequest{ID: meta.ID, ForceArchive: !preserved.CanRemove}) return preserved, err }, Admit: admit, diff --git a/cmd/goblins/main.go b/cmd/goblins/main.go index 38a52e6ac..7ce80cfca 100644 --- a/cmd/goblins/main.go +++ b/cmd/goblins/main.go @@ -134,6 +134,7 @@ commands: goblins tickets <project> [--brief <file>] [--files <paths>] [--json] read-only report of what others have in flight in the project's GitHub repository: whether it is collaborative, its active contributors, open issues, open and draft PRs with their files, and branches others pushed in the last 14 days; with --brief or --files it names the PRs, branches and issues that overlap that area goblins tickets <project> --allow-public-tickets let the supervisor keep each task's ticket in the project's repository although it is public, where every issue is public; asked once per repository goblins issue file --project <project> --title <title> [--body-file <file>] [--evidence <text>]... [--root <key>] file a root issue found outside your brief on the project's repository with the ops-root label, or comment on the open issue of the same root. On a public repository it refuses text naming a private project, a teammate, a local path or a secret + goblins issue unowned read-only: for each project of the home, the open issues without the ops-root label that no live or queued task names in its brief, its backlog row or its pull requests, and apart those only finished or parked tasks name goblins brief <id> --project <name|path> [--kind <ship|scout>] [--mode <no-mistakes|direct-PR|local-only>] [--surface <internal|product|mixed|unknown>] [--risk <low|sensitive-surface>] [--reason "<assessment>"] [--delivery-exception "<authority and reason>"] goblins brief ready <id> validate a completed queued brief and finalize its ready state, preserving its authored launch and auth choices goblins pr check <id> <url> @@ -143,7 +144,7 @@ commands: goblins allowance-floor [<claude|codex> <percent>] say each provider's weekly allowance floor, the percent of its week the fleet keeps back (5 unless set), or set one in the home's config/fleet.json; 0 keeps none, so goblins run until the provider refuses goblins start-hook accept <task-id> <file> [--fleet] accept a start hook a goblin's seal stopped once you judged it safe: the file, by its name, its exact content and the distribution whose RECORD installs it, for the goblins of the task's project, or of every project with --fleet; their seals let it through at every install from then on and still stop it once any of the three differs goblins merge-local <id> - goblins cleanup <id> return a finished task's worktree, every extra worktree it recorded and its scratch folder, refusing while any of them holds uncommitted work and keeping work that is not on the default branch as a local archive tag + goblins cleanup <id> [--force-archive] [--keep-issue-open <issue>... --reason "<why>"] return a finished task's worktree, every extra worktree it recorded and its scratch folder, refusing while any of them holds uncommitted work and keeping work that is not on the default branch as a local archive tag, and refusing while an issue the task names is open or gone on GitHub unless --keep-issue-open <issue> --reason "<why>" keeps it open, which the task records goblins worktree add <id> <name> [--ref <commit>] [--project <name|path>] | remove <id> <name> [--project <name|path>] give a task an extra worktree, <id>-<name>, beside its own in the home, of its own project or with --project of another, recorded on the task so cleanup removes it with the task, or return one early goblins backlog done <id> close a retired delivered task's queued row, preserving its evidence and continuation under Done goblins pause <id> | resume <id> | kill <id> pause, resume or stop a task while preserving its work @@ -193,7 +194,7 @@ type commandRuntime struct { idle func(stateDir, id string, screens harness.Screens) (bool, error) snapshot func(context.Context, home.Home) (fleet.Snapshot, error) localRuntime func(context.Context, home.Home) (runtime.Inventory, error) - cleanup func(context.Context, home.Home, string, bool) (string, error) + cleanup func(context.Context, home.Home, cleanupRequest) (string, error) taskLifecycle func(context.Context, home.Home, lifecycle.Request, string) (state.Lifecycle, error) reap func(context.Context, home.Home, reap.Options) (reap.Result, error) // processPlan reads what the fleet's process sweeps would end now, and @@ -531,7 +532,9 @@ func defaultCommandRuntime() commandRuntime { DispatchNeed: func() (uint64, error) { return dispatchNeed(h, fleettree.Processes, time.Now()) }, }.Collect(ctx) }, - cleanup: defaultCleanup, + cleanup: func(ctx context.Context, h home.Home, request cleanupRequest) (string, error) { + return defaultCleanup(ctx, h, request, github) + }, taskLifecycle: defaultTaskLifecycle, reap: defaultReap, processPlan: defaultProcessPlan, diff --git a/cmd/goblins/task_issues.go b/cmd/goblins/task_issues.go new file mode 100644 index 000000000..93c1a9bb3 --- /dev/null +++ b/cmd/goblins/task_issues.go @@ -0,0 +1,202 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "regexp" + "slices" + "strings" + + "github.com/fpresta0607/code-goblins/internal/execx" + "github.com/fpresta0607/code-goblins/internal/fsx" + "github.com/fpresta0607/code-goblins/internal/home" + "github.com/fpresta0607/code-goblins/internal/pipeline" + projectcfg "github.com/fpresta0607/code-goblins/internal/project" + "github.com/fpresta0607/code-goblins/internal/state" + "github.com/fpresta0607/code-goblins/internal/tickets" +) + +// pullRequestAddress is a pull request's address on GitHub. +var pullRequestAddress = regexp.MustCompile(`^https://github\.com/[\w.-]+/[\w.-]+/pull/\d+$`) + +// namingTask is one task as the issues it names are read from: its +// project's issue repository, which a bare issue number belongs to, its +// brief, its backlog row's line, the --closes of its gate runs and its pull +// requests. +type namingTask struct { + ID string + Repository string + Brief string + Row string + Closes []string + PullRequests []string +} + +// issues are every issue the task names, each once, given what each of its +// pull requests names. A bare number of a task whose repository is unknown +// names no issue. +func (task namingTask) issues(pullIssues map[string][]tickets.IssueRef) []tickets.IssueRef { + var named []tickets.IssueRef + add := func(issues []tickets.IssueRef) { + for _, issue := range issues { + if issue.Repository != "" && !slices.Contains(named, issue) { + named = append(named, issue) + } + } + } + add(tickets.BriefIssueRefs(task.Brief, task.Repository)) + add(tickets.RowIssueRefs(task.Row, task.Repository)) + add(tickets.ClosingIssueRefs(strings.Join(task.Closes, "\n"), task.Repository)) + for _, pull := range task.PullRequests { + add(pullIssues[pull]) + } + return named +} + +// taskPullRequests are the pull requests task id is on record with, each +// once: the one goblins pr check recorded, the one its retirement recorded, +// each its done reports named and each its ticket named. +func taskPullRequests(stateDir, id string) ([]string, error) { + var pulls []string + add := func(pull string) { + if pull = strings.TrimSpace(pull); pullRequestAddress.MatchString(pull) && !slices.Contains(pulls, pull) { + pulls = append(pulls, pull) + } + } + values, err := state.ReadMeta(state.TaskMetaPath(stateDir, id)) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, err + } + add(values["pr"]) + outcome, err := state.ReadOutcome(stateDir, id) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, err + } + add(outcome.PR) + lines, err := fsx.ReadLines(state.StatusPath(stateDir, id)) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, err + } + for _, line := range lines { + if _, event := state.SplitStatus(line); strings.HasPrefix(event, "done: PR ") { + add(strings.TrimPrefix(event, "done: PR ")) + } + } + record, err := tickets.ReadRecord(stateDir, id) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, err + } + for _, pull := range record.PullRequestsOnRecord() { + add(pull) + } + return pulls, nil +} + +// taskBrief is the text of the brief at path, or of the task's brief in the +// home's data folder when path names none or none is there, and "" when the +// task has neither. +func taskBrief(h home.Home, id, path string) (string, error) { + for _, candidate := range []string{path, filepath.Join(h.Data, id, "brief.md")} { + if candidate == "" { + continue + } + text, err := fsx.ReadFile(candidate) + if err == nil { + return string(text), nil + } + if !errors.Is(err, fs.ErrNotExist) { + return "", err + } + } + return "", nil +} + +// issueRepository is the GitHub repository the issues of the project at +// checkout go on: the one its project record names, or its origin. A +// checkout with no GitHub origin has none. +func issueRepository(ctx context.Context, h home.Home, repositoryOf func(context.Context, string) (string, error), checkout string) (string, error) { + manifest, path, err := projectcfg.LoadForCheckout(h.Data, checkout) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return "", fmt.Errorf("read the project record %s: %w", path, err) + } + if manifest.IssueRepository != "" { + return strings.ToLower(manifest.IssueRepository), nil + } + repository, err := repositoryOf(ctx, checkout) + if errors.Is(err, tickets.ErrNotGitHub) { + return "", nil + } + return strings.ToLower(repository), err +} + +// openTaskIssues names, as owner/name#N, each issue a task names that is +// still open on GitHub, and apart each one GitHub reports gone: an issue its +// brief names, one its branch's gate runs were started to close and one its +// pull requests name. An issue or a pull request that cannot be read is an +// error, and a gone issue is never one taken as closed. A project checkout +// gone leaves the task's bare issue numbers no repository, and a worktree no +// longer on a branch leaves its gate runs unread. +func openTaskIssues(h home.Home, github tickets.GitHub, gate pipeline.Reader) func(context.Context, state.TaskMeta) (open, gone []string, err error) { + return func(ctx context.Context, meta state.TaskMeta) (open, gone []string, err error) { + task := namingTask{ID: meta.ID} + if _, err := os.Stat(meta.Project); err == nil { + if task.Repository, err = issueRepository(ctx, h, github.RepositoryOf, meta.Project); err != nil { + return nil, nil, err + } + } else if !errors.Is(err, fs.ErrNotExist) { + return nil, nil, err + } + brief, err := taskBrief(h, meta.ID, meta.Brief) + if err != nil { + return nil, nil, err + } + task.Brief = brief + if task.Closes, err = gateClosingIssues(ctx, github.Commands, gate, meta); err != nil { + return nil, nil, err + } + if task.PullRequests, err = taskPullRequests(h.State, meta.ID); err != nil { + return nil, nil, err + } + pullIssues, unread, err := github.PullRequestIssues(ctx, task.PullRequests) + if err != nil { + return nil, nil, err + } + if len(unread) > 0 { + return nil, nil, fmt.Errorf("GitHub shows no pull request at %s", strings.Join(unread, ", ")) + } + for _, issue := range task.issues(pullIssues) { + issueState, err := github.IssueState(ctx, issue.Repository, issue.Number) + if err != nil { + return nil, nil, fmt.Errorf("read %s: %w", issue, err) + } + switch issueState { + case "open": + open = append(open, issue.String()) + case "missing": + gone = append(gone, issue.String()) + } + } + return open, gone, nil + } +} + +// gateClosingIssues are the --closes of the gate runs of the branch the +// task's worktree is on, none while it is on none or the gate has no +// database. +func gateClosingIssues(ctx context.Context, commands execx.Runner, gate pipeline.Reader, meta state.TaskMeta) ([]string, error) { + if _, err := os.Stat(filepath.Join(gate.Root, "state.sqlite")); errors.Is(err, fs.ErrNotExist) { + return nil, nil + } + if _, err := os.Stat(meta.Worktree); errors.Is(err, fs.ErrNotExist) { + return nil, nil + } + branch, err := commands.Run(ctx, execx.Request{Dir: meta.Worktree, Name: "git", Args: []string{"symbolic-ref", "--quiet", "--short", "HEAD"}}) + if err != nil || branch.ExitCode != 0 { + return nil, nil + } + return gate.ClosingIssues(ctx, meta.Project, strings.TrimSpace(string(branch.Stdout))) +} diff --git a/cmd/goblins/task_issues_test.go b/cmd/goblins/task_issues_test.go new file mode 100644 index 000000000..72cb9742b --- /dev/null +++ b/cmd/goblins/task_issues_test.go @@ -0,0 +1,174 @@ +package main + +import ( + "context" + "database/sql" + "net/http" + "os" + "path/filepath" + "slices" + "strconv" + "strings" + "testing" + + "github.com/fpresta0607/code-goblins/internal/execx" + "github.com/fpresta0607/code-goblins/internal/gatedb" + "github.com/fpresta0607/code-goblins/internal/githubtest" + "github.com/fpresta0607/code-goblins/internal/home" + "github.com/fpresta0607/code-goblins/internal/pipeline" + "github.com/fpresta0607/code-goblins/internal/state" + "github.com/fpresta0607/code-goblins/internal/tickets" +) + +// taskIssuesGit answers the two git reads openTaskIssues makes: the origin +// of the project checkout and the branch of the task's worktree. +type taskIssuesGit struct{} + +func (taskIssuesGit) Run(_ context.Context, request execx.Request) (execx.Result, error) { + arguments := strings.Join(request.Args, " ") + switch { + case arguments == "symbolic-ref --quiet --short HEAD": + return execx.Result{Stdout: []byte("fix/x\n")}, nil + case strings.HasSuffix(arguments, "remote get-url origin"): + return execx.Result{Stdout: []byte("https://github.com/acme/widgets.git\n")}, nil + } + return execx.Result{ExitCode: 129, Stderr: []byte("unexpected git " + arguments)}, nil +} + +// taskIssuesGitHub answers what the task's pull request names, each +// repository as one GitHub shows unless it was told to hide them, and the +// state of each issue it knows, "pull request" answering as GitHub answers an +// issue read of a pull request, and it refuses an issue it does not know and +// a pull request it was told to hide. +type taskIssuesGitHub struct { + states map[string]string + isPullGone bool + isRepositoriesHidden bool +} + +func (g taskIssuesGitHub) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && r.URL.Path == "/graphql" { + pull := map[string]any{"url": "https://github.com/acme/widgets/pull/50", "body": "Part of acme/gadgets#3", "closingIssuesReferences": map[string]any{"nodes": []any{map[string]any{"number": 2, "repository": map[string]any{"nameWithOwner": "acme/widgets"}}}}} + if g.isPullGone { + pull = nil + } + githubtest.Answer(w, map[string]any{"pull0": pull}) + return + } + repository, number, isIssue := strings.Cut(strings.TrimPrefix(r.URL.Path, "/repos/"), "/issues/") + if r.Method == http.MethodGet && !isIssue && !g.isRepositoriesHidden && strings.Count(repository, "/") == 1 { + githubtest.Write(w, http.StatusOK, map[string]any{"full_name": repository}) + return + } + issueState, isKnown := g.states[repository+"#"+number] + issueNumber, err := strconv.Atoi(number) + if r.Method != http.MethodGet || !isIssue || !isKnown || err != nil { + githubtest.Refuse(w, http.StatusNotFound, "Not Found") + return + } + if issueState == "pull request" { + githubtest.Write(w, http.StatusOK, map[string]any{"number": issueNumber, "html_url": "https://github.com/" + repository + "/pull/" + number, "state": "open", "pull_request": map[string]any{}}) + return + } + githubtest.Write(w, http.StatusOK, map[string]any{"number": issueNumber, "html_url": "https://github.com/" + repository + "/issues/" + number, "state": issueState}) +} + +// taskIssuesFixture is a task of acme/widgets whose brief names issue 1 in +// its Task and issue 99 only in its Constraints, whose branch's gate run was +// started to close issue 9, and whose pull request closes issue 2 and is part +// of acme/gadgets#3. +func taskIssuesFixture(t *testing.T) (home.Home, state.TaskMeta, pipeline.Reader) { + t.Helper() + base := t.TempDir() + h := home.Home{Root: base, Data: filepath.Join(base, "data"), State: filepath.Join(base, "state")} + checkout := filepath.Join(base, "widgets") + worktree := filepath.Join(base, "worktrees", "g1") + for _, dir := range []string{checkout, worktree, h.State} { + if err := os.MkdirAll(dir, 0o700); err != nil { + t.Fatal(err) + } + } + brief := filepath.Join(h.Data, "g1", "brief.md") + writeFixtureFile(t, brief, "# Brief g1\n\n## Task\n\nFix https://github.com/acme/widgets/issues/1.\n\n## Constraints\n\n- Leave issue #99 alone.\n") + if err := state.AppendStatus(h.State, "g1", "done: PR https://github.com/acme/widgets/pull/50"); err != nil { + t.Fatal(err) + } + writer, err := sql.Open("sqlite", filepath.Join(base, "state.sqlite")) + if err != nil { + t.Fatal(err) + } + _, err = writer.Exec(`CREATE TABLE repos(id TEXT, working_path TEXT); CREATE TABLE runs(id TEXT, repo_id TEXT, branch TEXT, closing_issue_refs TEXT, created_at INTEGER); INSERT INTO repos VALUES ('repo', ?); INSERT INTO runs VALUES ('run', 'repo', 'fix/x', '9', 100);`, checkout) + if err != nil { + t.Fatal(err) + } + if err := writer.Close(); err != nil { + t.Fatal(err) + } + meta := state.TaskMeta{ID: "g1", Project: checkout, Worktree: worktree} + return h, meta, pipeline.Reader{Root: base, Commands: gatedb.Runner{Next: taskIssuesGit{}}} +} + +func TestOpenTaskIssuesNamesTheOpenIssuesOfTheBriefTheGateAndThePullRequest(t *testing.T) { + // Arrange + h, meta, gate := taskIssuesFixture(t) + answers := taskIssuesGitHub{states: map[string]string{"acme/widgets#1": "open", "acme/widgets#2": "closed", "acme/widgets#9": "open", "acme/gadgets#3": "open", "acme/widgets#99": "open"}} + github := tickets.GitHub{Commands: taskIssuesGit{}, API: githubtest.Client(answers)} + + // Act + open, gone, err := openTaskIssues(h, github, gate)(context.Background(), meta) + + // Assert + if err != nil { + t.Fatal(err) + } + if want := []string{"acme/widgets#1", "acme/widgets#9", "acme/gadgets#3"}; !slices.Equal(open, want) || len(gone) != 0 { + t.Fatalf("open issues = %q, gone %q; want %q open and none gone", open, gone, want) + } +} + +// GitHub answers 404 for an issue that was deleted or never filed, in a +// repository it shows, so the issue is gone, never closed; a number that is a +// pull request is no issue at all. +func TestOpenTaskIssuesNamesAnIssueGitHubReportsGoneApart(t *testing.T) { + // Arrange + h, meta, gate := taskIssuesFixture(t) + answers := taskIssuesGitHub{states: map[string]string{"acme/widgets#1": "open", "acme/widgets#2": "pull request", "acme/gadgets#3": "closed"}} + github := tickets.GitHub{Commands: taskIssuesGit{}, API: githubtest.Client(answers)} + + // Act + open, gone, err := openTaskIssues(h, github, gate)(context.Background(), meta) + + // Assert + if err != nil { + t.Fatal(err) + } + if !slices.Equal(open, []string{"acme/widgets#1"}) || !slices.Equal(gone, []string{"acme/widgets#9"}) { + t.Fatalf("open issues = %q, gone %q; want acme/widgets#1 open and acme/widgets#9 gone", open, gone) + } +} + +func TestOpenTaskIssuesFailsWhatItCannotRead(t *testing.T) { + tests := []struct { + name string + answers taskIssuesGitHub + want string + }{ + {"a pull request GitHub does not show", taskIssuesGitHub{isPullGone: true}, "GitHub shows no pull request at https://github.com/acme/widgets/pull/50"}, + {"an issue of a repository GitHub does not show", taskIssuesGitHub{states: map[string]string{"acme/widgets#1": "open"}, isRepositoriesHidden: true}, "read acme/widgets#9"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + // Arrange + h, meta, gate := taskIssuesFixture(t) + github := tickets.GitHub{Commands: taskIssuesGit{}, API: githubtest.Client(test.answers)} + + // Act + _, _, err := openTaskIssues(h, github, gate)(context.Background(), meta) + + // Assert + if err == nil || !strings.Contains(err.Error(), test.want) { + t.Fatalf("err = %v, want one with %q", err, test.want) + } + }) + } +} diff --git a/docs/commands/cleanup.md b/docs/commands/cleanup.md index 4360274f6..6a1e52bfc 100644 --- a/docs/commands/cleanup.md +++ b/docs/commands/cleanup.md @@ -1,6 +1,6 @@ # goblins cleanup -`goblins cleanup <id>` +`goblins cleanup <id> [--force-archive] [--keep-issue-open <issue>... --reason "<why>"]` Close the task tab and return a finished, proven-inactive task's worktrees and scratch: its worktree (in the home, or where its record says for a task an older build spawned), every extra worktree it recorded with `goblins worktree add`, and its scratch folder are removed and their git administrative entries pruned, an extra of another project's in that project's checkout, and the worktrees the project, or an extra's own project, registers inside those folders, which the task's own harness left there, are returned with them and named in its output, and work that is not on the default branch is first kept as a local tag, `archive/<branch>`. A task whose gate run is still open is refused before anything changes, as a paused goblin's is, since a pause of any reason leaves its run running, and so is one whose gate cannot be read, which is never taken for one with no run. @@ -11,6 +11,17 @@ A task whose lifecycle record, where a pause saves its run, cannot be read is re The run of the worktree's branch is read as well while that folder is still a Git worktree, since a goblin that runs again can start a newer run there, and either run still open refuses. A worktree already gone, or a folder no longer a Git worktree, names no branch, so a task with no saved run retires without its run being read. A task whose project checkout is gone (issue #1578), or whose gate holds no database, names no run either way, as its kill aborts none, so the kill's own retirement goes through. +A task that names an issue still open on GitHub is refused before anything changes, with each open issue named as `owner/name#N`, whichever way it retires, `--force-archive` and an empty folder's included. +The issues a task names are those its brief names to take up in its Task or Acceptance criteria, or in a section added to the brief later under a heading that starts `## ADDED`, which is read as a Task of its own (by address, as "issue #N", or as a bare `#N` that opens a line of its Task, and in an added section also as "issue N" or "issues N and M", its heading included), those its branch's gate runs were started to close with `goblins pipeline run --closes`, and those its pull requests name, which GitHub links them to close or their body names after a closing word or "Part of". +Its pull requests are the one `goblins pr check` recorded, each its done reports named and each its ticket named. +A bare number belongs to the project's issue repository, its project record's `issue_repository` or else its origin. +An issue GitHub reports gone, as a deleted issue or a mistyped number is, refuses the cleanup as well, named as `owner/name#N (gone on GitHub)` apart from the open ones, since a gone issue is never taken for a closed one. +A pull request or a repository GitHub will not show refuses it too, since an issue it cannot read is never taken for a closed one either. +Close each open issue once its work is on its project's main, or retire the task with an open or gone issue by `--keep-issue-open <issue>`, written `owner/name#N` or as its address and repeated for each, with `--reason "<why>"`. +The task's status log records each issue kept open as `issue-kept-open: <issue>: <reason>` and the output names it. +A keep that names no open or gone issue of the task, and a keep without a reason, are refused. +`goblins kill`, which stops work unfinished, and `goblins reap --apply` check no issue. +`goblins issue unowned` lists the open issues that no live or queued task names ([goblins help issue unowned](issue/unowned.md)). A task any of whose worktrees holds uncommitted work is refused before anything changes, never destroyed. goblins removes a worktree's files itself, by the removal every goblin folder goes through, which removes a link as a link and never walks into what it points at: a junction pnpm makes in `node_modules`, which can lead into the store every project on the machine shares, or one into the checkout. A path past 260 characters and a read-only file go the same way. It never runs `git worktree remove`, because Git for Windows takes a junction for a folder: it deletes what the junction points at, and it stopped with "Directory not empty" at a junction its own deletion had left dangling, having already deleted the worktree's `.git` link and its record of the worktree. diff --git a/docs/commands/issue/unowned.md b/docs/commands/issue/unowned.md new file mode 100644 index 000000000..dddb45f95 --- /dev/null +++ b/docs/commands/issue/unowned.md @@ -0,0 +1,12 @@ +# goblins issue unowned + +`goblins issue unowned` + +Lists, for every project of the home, the open issues that are not the root clean-up's (no `ops-root` label) and that no live or queued task names, and apart from them those that only finished or parked tasks name, each with those tasks. +It reads only: it writes nothing on GitHub or in the home. +A project is a folder of `data/projects` whose checkout the projects root holds, and its issues go on its project record's `issue_repository` or else its origin; projects that share one repository are listed together once. +A task is live while the home keeps its record, queued while its row is under `## Queued`, parked under `## Parked`, and finished when only its status log, its outcome or its `## Done` row is left. +An issue a task names is one its brief names to take up, as `goblins cleanup` reads it, one its backlog row's line names (by address, as `owner/name#N`, `#N`, "issue N", "issues N, M and K" or "issues N to M"), and one its pull requests name, which GitHub links them to close or their body names after a closing word or "Part of". +It reads every task's pull requests through GitHub's GraphQL API, fifty to a query, and each repository's open issues a hundred to a page, up to ten pages; a repository with more is named as not read rather than listed in part. +Each repository's line counts its open issues, its `ops-root` ones, those a live or queued task names, those only finished or parked tasks name, and those no task names, and the last two are listed under it with their titles and addresses. +What it could not read, a project that does not resolve, a repository GitHub will not show, a live task's record or a pull request GitHub does not show, is named at the end, since an issue only that would have named is listed as unnamed. diff --git a/docs/contract/delivery.md b/docs/contract/delivery.md index 247801b56..054200106 100644 --- a/docs/contract/delivery.md +++ b/docs/contract/delivery.md @@ -79,6 +79,8 @@ Required Go guards and platform deferrals follow [Verification levels](../pipeli A ready result names the public PR URL and exact published head with required CI green, or a clean committed branch for local-only work. A draft, a different local commit or a commit present only in a disposable validation worktree is not ready for landing. Use existing PR check and guarded merge commands; confirm the forge's landed state before cleanup and never discard unlanded work. +`goblins cleanup` refuses a task while an issue it names is still open or gone on GitHub; close an open issue once its work is on its project's main, or keep the issue open with `--keep-issue-open <issue> --reason "<why>"`, which the task records ([goblins help cleanup](../commands/cleanup.md)). +`goblins issue unowned` lists the open issues no live or queued task names, and apart those only finished or parked tasks name. Delivery mode and merge authority remain independent. A scout leaves a self-contained report, and a recommendation or visual review grants no implementation authority. Keep a review artifact available while feedback is outstanding. diff --git a/docs/contract/ownership.md b/docs/contract/ownership.md index 3bf006697..1505468fc 100644 --- a/docs/contract/ownership.md +++ b/docs/contract/ownership.md @@ -106,6 +106,7 @@ The extraction preserves the existing obligations, including gate budgets, merge | `goblins tickets <project> [--brief <file>] [--files <paths>] [--json]` | [goblins help tickets](../commands/tickets.md) | | `goblins tickets <project> --allow-public-tickets` | [goblins help tickets](../commands/tickets.md) | | `goblins issue file --project <p> --title <t> [--body-file <f>] [--evidence <text>]... [--root <key>]` | [goblins help issue file](../commands/issue/file.md) | +| `goblins issue unowned` | [goblins help issue unowned](../commands/issue/unowned.md) | | `goblins brief <id> --project <p> [--kind <kind>] [--mode <m>] [--surface <s>] [--risk <r>] [--reason "<assessment>"] [--delivery-exception "<authority and reason>"]` | [goblins help brief](../commands/brief.md) | | `goblins pr check <id> <url>` | [goblins help pr check](../commands/pr/check.md) | | `goblins pr merge <url> [--method <m>] [--delete-branch] [--verified "<what verified it>"]` | [goblins help pr merge](../commands/pr/merge.md) | @@ -114,7 +115,7 @@ The extraction preserves the existing obligations, including gate budgets, merge | `goblins allowance-floor [<claude\|codex> <percent>]` | [goblins help allowance-floor](../commands/allowance-floor.md) | | `goblins start-hook accept <task-id> <file> [--fleet]` | [goblins help start-hook accept](../commands/start-hook/accept.md) | | `goblins merge-local <id>` | [goblins help merge-local](../commands/merge-local.md) | -| `goblins cleanup <id>` | [goblins help cleanup](../commands/cleanup.md) | +| `goblins cleanup <id> [--force-archive] [--keep-issue-open <issue>... --reason "<why>"]` | [goblins help cleanup](../commands/cleanup.md) | | `goblins worktree add <id> <name> [--ref <commit>] [--project <name\|path>]` \| `remove <id> <name> [--project <name\|path>]` | [goblins help worktree](../commands/worktree.md) | | `goblins reap [--dry-run] [--apply] [--force <pid\|task-id>]` | [goblins help reap](../commands/reap.md) | | `goblins register` | [goblins help register](../commands/register.md) | diff --git a/internal/cleanup/cleanup.go b/internal/cleanup/cleanup.go index 1be6940f7..02675ddef 100644 --- a/internal/cleanup/cleanup.go +++ b/internal/cleanup/cleanup.go @@ -23,6 +23,7 @@ import ( "io/fs" "os" "path/filepath" + "slices" "strings" "time" @@ -87,6 +88,25 @@ type Service struct { // whose gate cannot be read, is refused before anything changes: retired, // the task would leave its run going on for nobody. Nil reads none. OpenGateRun func(ctx context.Context, meta state.TaskMeta) (string, error) + // OpenIssues names each issue the task names that is still open, and apart + // each one GitHub reports gone, as one deleted or never filed, each by its + // address owner/name#N, and nothing for an issue that is closed or a task + // that names none. A task's work is not settled while an issue it names is + // open, since the issue then still asks for work nobody is doing, nor while + // one is gone, since nothing then shows that its work was done, so every + // retirement, a force archive and an empty folder's included, asks it + // first, and one that finds an issue open or gone that the CEO has not + // kept open with KeepOpen, or whose issues cannot be read, is refused + // before anything changes. Nil reads none. + OpenIssues func(ctx context.Context, meta state.TaskMeta) (open, gone []string, err error) + // KeepOpen are the open or gone issues, each by its address owner/name#N, + // that the CEO retires the task with anyway, and KeepReason is why: an + // issue that stays open on purpose, as one whose work another task carries + // on. The task's status log records each with the reason, so the issue + // left open is never a silent one. A keep naming an issue that is neither + // open nor gone refuses the cleanup. + KeepOpen []string + KeepReason string } // Result reports the exact returned task identity. @@ -193,6 +213,10 @@ func (s Service) Cleanup(ctx context.Context, id string) (result Result, err err if err := s.requireGateRunEnded(ctx, meta); err != nil { return Result{}, err } + keptOpen, err := s.requireIssuesSettled(ctx, meta) + if err != nil { + return Result{}, err + } // A harness's own tooling leaves worktrees of its own, in the task's // scratch folder or inside the checkout the goblin worked in, and they go // with the task's own. They are returned before the guards read the @@ -269,6 +293,10 @@ func (s Service) Cleanup(ctx context.Context, id string) (result Result, err err if err := state.WriteOutcome(s.StateDir, outcome); err != nil { return Result{}, err } + keptNote, err := s.recordKeptIssues(id, keptOpen) + if err != nil { + return Result{}, err + } if err := state.AppendStatus(s.StateDir, id, outcome.Phase+": "+returned+" via goblins cleanup"); err != nil { return Result{}, fmt.Errorf("cleanup: record returned worktree: %w", err) } @@ -286,6 +314,7 @@ func (s Service) Cleanup(ctx context.Context, id string) (result Result, err err if archive != "" { result.Output += " archive=" + archive } + result.Output += keptNote for _, tag := range archived { result.Output += "\nkept unlanded work as the local tag " + tag } @@ -389,6 +418,85 @@ func (s Service) requireGateRunEnded(ctx context.Context, meta state.TaskMeta) e return nil } +// requireIssuesSettled refuses a task that names an issue that is still open +// or gone, or whose issues cannot be read, before anything changes, unless the +// CEO keeps that issue open with a reason, and returns the issues the CEO +// keeps open, each as the address OpenIssues spelled it. A keep that names no +// open or gone issue is a mistake to show, never one to ignore: it would +// otherwise sit beside a retirement that its words never reached. Addresses +// compare without regard to case, as GitHub spells an owner and a repository. +func (s Service) requireIssuesSettled(ctx context.Context, meta state.TaskMeta) (kept []string, err error) { + if s.OpenIssues == nil { + return nil, nil + } + open, gone, err := s.OpenIssues(ctx, meta) + if err != nil { + return nil, fmt.Errorf("cleanup: the issues %s names cannot be read, so nothing was removed, and the cleanup can run again once they can: %w", meta.ID, err) + } + unsettled := append(slices.Clip(open), gone...) + for _, keep := range s.KeepOpen { + if !slices.ContainsFunc(unsettled, func(issue string) bool { return strings.EqualFold(issue, keep) }) { + return nil, fmt.Errorf("cleanup: %s is not an issue %s names that is open or gone, so nothing was removed; its open or gone issues are %s", keep, meta.ID, unsettledIssueList(unsettled)) + } + } + isKept := func(issue string) bool { + return slices.ContainsFunc(s.KeepOpen, func(keep string) bool { return strings.EqualFold(issue, keep) }) + } + var openLeft, goneLeft []string + for _, issue := range open { + if isKept(issue) { + kept = append(kept, issue) + } else { + openLeft = append(openLeft, issue) + } + } + for _, issue := range gone { + if isKept(issue) { + kept = append(kept, issue) + } else { + goneLeft = append(goneLeft, issue+" (gone on GitHub)") + } + } + var named []string + if len(openLeft) > 0 { + named = append(named, "the open issue(s) "+strings.Join(openLeft, ", ")) + } + if len(goneLeft) > 0 { + named = append(named, "the issue(s) "+strings.Join(goneLeft, ", ")) + } + if len(named) > 0 { + return nil, fmt.Errorf("cleanup: %s names %s, so nothing was removed: close each open issue once its work is on its project's main, or retire the task with an open or gone issue by goblins cleanup %s --keep-issue-open <issue> --reason \"<why>\"", meta.ID, strings.Join(named, " and "), meta.ID) + } + return kept, nil +} + +// unsettledIssueList says which issues are open or gone, for a refusal that +// shows what a keep could have named. +func unsettledIssueList(unsettled []string) string { + if len(unsettled) == 0 { + return "none" + } + return strings.Join(unsettled, ", ") +} + +// recordKeptIssues records in the task's status log each open or gone issue +// the task retires with, and why, and says so for the cleanup's output. It +// runs after every check has passed, just before the line that records the +// retirement. +func (s Service) recordKeptIssues(id string, kept []string) (string, error) { + // A reason is one line in a log of lines, so a line break in it cannot + // start a status line of its own. + reason := strings.Join(strings.Fields(s.KeepReason), " ") + output := "" + for _, issue := range kept { + if err := state.AppendStatus(s.StateDir, id, "issue-kept-open: "+issue+": "+reason); err != nil { + return "", fmt.Errorf("cleanup: record the open issue %s kept: %w", issue, err) + } + output += fmt.Sprintf("\nkept the issue %s open: %s", issue, reason) + } + return output, nil +} + // endLeft ends what the task's terminal left running and says what it ended. // A sweep that fails leaves the rest to the janitor, whose hourly sweep ends // what a terminal with no host left, so the cleanup it is part of stands. @@ -529,6 +637,10 @@ func (s Service) forceArchive(ctx context.Context, meta state.TaskMeta, id, work if err := s.requireGateRunEnded(ctx, meta); err != nil { return Result{}, err } + keptOpen, err := s.requireIssuesSettled(ctx, meta) + if err != nil { + return Result{}, err + } handedOver, err := s.handOver(meta) if err != nil { return Result{}, err @@ -549,6 +661,10 @@ func (s Service) forceArchive(ctx context.Context, meta state.TaskMeta, id, work if err := state.WriteOutcome(s.StateDir, outcome); err != nil { return Result{}, err } + keptNote, err := s.recordKeptIssues(id, keptOpen) + if err != nil { + return Result{}, err + } if err := state.AppendStatus(s.StateDir, id, outcome.Phase+": force-archived via goblins cleanup --force-archive; worktree "+worktreePath+" left in place"); err != nil { return Result{}, fmt.Errorf("cleanup: record force archive: %w", err) } @@ -565,6 +681,7 @@ func (s Service) forceArchive(ctx context.Context, meta state.TaskMeta, id, work if archived != "" { result.Output += " archive=" + archived } + result.Output += keptNote for _, note := range notes { result.Output += "\nnote: " + note } @@ -592,6 +709,10 @@ func (s Service) retireEmptyFolder(ctx context.Context, meta state.TaskMeta, id, if err := s.requireGateRunEnded(ctx, meta); err != nil { return Result{}, err } + keptOpen, err := s.requireIssuesSettled(ctx, meta) + if err != nil { + return Result{}, err + } handedOver, err := s.handOver(meta) if err != nil { return Result{}, err @@ -633,6 +754,10 @@ func (s Service) retireEmptyFolder(ctx context.Context, meta state.TaskMeta, id, if err := state.WriteOutcome(s.StateDir, outcome); err != nil { return Result{}, err } + keptNote, err := s.recordKeptIssues(id, keptOpen) + if err != nil { + return Result{}, err + } if err := state.AppendStatus(s.StateDir, id, outcome.Phase+": "+removed+" via goblins cleanup"); err != nil { return Result{}, fmt.Errorf("cleanup: record the removed folder: %w", err) } @@ -645,6 +770,7 @@ func (s Service) retireEmptyFolder(ctx context.Context, meta state.TaskMeta, id, if archive != "" { result.Output += " archive=" + archive } + result.Output += keptNote result.Output += warning if archiveErr != nil { result.Output += fmt.Sprintf("\nwarning: retained state for %s could not be archived, so respawning that id will be refused: %v", id, archiveErr) diff --git a/internal/cleanup/issues_test.go b/internal/cleanup/issues_test.go new file mode 100644 index 000000000..cc6e8cf2c --- /dev/null +++ b/internal/cleanup/issues_test.go @@ -0,0 +1,300 @@ +package cleanup + +import ( + "context" + "errors" + "os" + "strings" + "testing" + + "github.com/fpresta0607/code-goblins/internal/state" +) + +const ( + firstIssue = "acme/widgets#12" + secondIssue = "acme/gadgets#7" + goneIssue = "acme/widgets#4321" +) + +// opensIssues makes the fixture's task name the issues given, each open. +func opensIssues(fixture *cleanupFixture, issues ...string) { + fixture.service.OpenIssues = func(context.Context, state.TaskMeta) ([]string, []string, error) { + return issues, nil, nil + } +} + +// assertNothingRetired proves a refused cleanup left the task record and its +// worktree in place and wrote nothing to the task's status log. +func assertNothingRetired(t *testing.T, fixture *cleanupFixture) { + t.Helper() + fixture.assertMetadataPreserved(t) + if _, err := os.Stat(fixture.worktree); err != nil { + t.Errorf("worktree after a refused cleanup: %v, want it kept", err) + } + if status, err := state.TailStatus(fixture.stateDir, "g1", 5); err != nil || len(status) != 0 { + t.Errorf("status log after a refused cleanup = %v, %v; want it untouched", status, err) + } +} + +// A task's work is not settled while an issue it names is open: the issue +// still asks for work nobody is doing. The cleanup refuses it, naming the +// issue, with the task record and worktree left as they were. +func TestCleanupRefusesATaskWhoseIssueIsStillOpen(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + opensIssues(fixture, firstIssue) + + // Act + _, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err == nil || !strings.Contains(err.Error(), firstIssue) || !strings.Contains(err.Error(), "--keep-issue-open") { + t.Fatalf("Cleanup = %v, want a refusal naming %s and how to keep it open", err, firstIssue) + } + assertNothingRetired(t, fixture) +} + +// An issue GitHub reports gone, as a deleted issue or a mistyped number is, +// shows nothing of the task's work done, so the cleanup refuses it as it +// does an open one, naming it as gone in a clause of its own. +func TestCleanupRefusesATaskWhoseIssueIsGoneAndNamesItApart(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + fixture.service.OpenIssues = func(context.Context, state.TaskMeta) ([]string, []string, error) { + return []string{firstIssue}, []string{goneIssue}, nil + } + + // Act + _, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + want := "names the open issue(s) " + firstIssue + " and the issue(s) " + goneIssue + " (gone on GitHub), so nothing was removed" + if err == nil || !strings.Contains(err.Error(), want) { + t.Fatalf("Cleanup = %v, want a refusal saying %q", err, want) + } + assertNothingRetired(t, fixture) +} + +func TestCleanupRetiresATaskWithAGoneIssueKeptAndRecordsWhy(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + fixture.service.OpenIssues = func(context.Context, state.TaskMeta) ([]string, []string, error) { + return nil, []string{goneIssue}, nil + } + fixture.service.KeepOpen = []string{goneIssue} + fixture.service.KeepReason = "the brief named a mistyped issue" + + // Act + result, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err != nil { + t.Fatalf("Cleanup = %v, want the task retired with its gone issue kept", err) + } + status, err := state.TailStatus(fixture.stateDir, "g1", 10) + if err != nil || len(status) != 2 { + t.Fatalf("status = %v, %v; want the kept issue, then the retirement", status, err) + } + if _, event := state.SplitStatus(status[0]); event != "issue-kept-open: "+goneIssue+": the brief named a mistyped issue" { + t.Errorf("first status line = %q, want the gone issue recorded with its reason", event) + } + if want := "\nkept the issue " + goneIssue + " open: the brief named a mistyped issue"; !strings.Contains(result.Output, want) { + t.Errorf("output = %q, want it to say %q", result.Output, want) + } + if _, err := state.ReadTaskMeta(fixture.stateDir, "g1"); !errors.Is(err, os.ErrNotExist) { + t.Errorf("task record after the cleanup: %v, want it retired", err) + } +} + +func TestCleanupRefusalNamesEveryOpenIssueNotKept(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + opensIssues(fixture, firstIssue, secondIssue) + fixture.service.KeepOpen = []string{secondIssue} + fixture.service.KeepReason = "another task carries it on" + + // Act + _, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err == nil || !strings.Contains(err.Error(), firstIssue) || strings.Contains(err.Error(), secondIssue) { + t.Fatalf("Cleanup = %v, want a refusal naming only %s", err, firstIssue) + } + assertNothingRetired(t, fixture) +} + +func TestCleanupRetiresATaskWhoseIssuesAreAllClosed(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + opensIssues(fixture) + + // Act + result, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err != nil { + t.Fatalf("Cleanup = %v, want the task retired", err) + } + if strings.Contains(result.Output, "kept the issue") { + t.Errorf("output = %q, want no issue kept open", result.Output) + } + if _, err := state.ReadTaskMeta(fixture.stateDir, "g1"); !errors.Is(err, os.ErrNotExist) { + t.Errorf("task record after the cleanup: %v, want it retired", err) + } +} + +func TestCleanupRetiresATaskWithAnIssueKeptOpenAndRecordsWhy(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + opensIssues(fixture, firstIssue, secondIssue) + fixture.service.KeepOpen = []string{"ACME/Widgets#12", secondIssue} + fixture.service.KeepReason = "the follow-up\nis a task of its own" + + // Act + result, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err != nil { + t.Fatalf("Cleanup = %v, want the task retired with its issues open", err) + } + wantReason := "the follow-up is a task of its own" + status, err := state.TailStatus(fixture.stateDir, "g1", 10) + if err != nil || len(status) != 3 { + t.Fatalf("status = %v, %v; want one line per kept issue, then the retirement", status, err) + } + for index, issue := range []string{firstIssue, secondIssue} { + if _, event := state.SplitStatus(status[index]); event != "issue-kept-open: "+issue+": "+wantReason { + t.Errorf("status line %d = %q, want the open issue %s recorded with its reason", index, event, issue) + } + if want := "\nkept the issue " + issue + " open: " + wantReason; !strings.Contains(result.Output, want) { + t.Errorf("output = %q, want it to say %q", result.Output, want) + } + } + if _, event := state.SplitStatus(status[2]); !strings.Contains(event, "returned worktree") { + t.Errorf("last status line = %q, want the retirement after the kept issues", event) + } + if _, err := state.ReadTaskMeta(fixture.stateDir, "g1"); !errors.Is(err, os.ErrNotExist) { + t.Errorf("task record after the cleanup: %v, want it retired", err) + } +} + +func TestCleanupRefusesAnIssueKeepItCannotHonourAndChangesNothing(t *testing.T) { + tests := []struct { + name string + issues func(context.Context, state.TaskMeta) ([]string, []string, error) + keepOpen []string + keepReason string + want []string + }{ + { + name: "a keep naming an issue that is not open", + issues: func(context.Context, state.TaskMeta) ([]string, []string, error) { + return []string{firstIssue}, []string{goneIssue}, nil + }, + keepOpen: []string{firstIssue, secondIssue}, + keepReason: "carried on elsewhere", + want: []string{secondIssue, "not an issue g1 names that is open or gone", firstIssue + ", " + goneIssue}, + }, + { + name: "a keep when no issue is open or gone", + issues: func(context.Context, state.TaskMeta) ([]string, []string, error) { return nil, nil, nil }, + keepOpen: []string{firstIssue}, + keepReason: "carried on elsewhere", + want: []string{firstIssue, "not an issue g1 names that is open or gone", "none"}, + }, + { + name: "issues that cannot be read", + issues: func(context.Context, state.TaskMeta) ([]string, []string, error) { + return nil, nil, errors.New("gh unavailable") + }, + want: []string{"the issues g1 names cannot be read", "nothing was removed", "gh unavailable"}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Arrange + fixture := newCleanupFixture(t) + fixture.service.OpenIssues = tc.issues + fixture.service.KeepOpen = tc.keepOpen + fixture.service.KeepReason = tc.keepReason + + // Act + _, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + for _, want := range tc.want { + if err == nil || !strings.Contains(err.Error(), want) { + t.Fatalf("Cleanup = %v, want a refusal saying %q", err, want) + } + } + assertNothingRetired(t, fixture) + }) + } +} + +// Every retirement asks, --force-archive and an empty folder's included: a +// task is as unsettled with its worktree pinned or gone as with it intact. +func TestEveryRetirementRefusesAnOpenIssueAndKeepsOneOpenWithItsReason(t *testing.T) { + for _, tc := range []struct { + name string + isForceArchive bool + isEmptyFolder bool + wantRetired string + }{ + {name: "a force archive", isForceArchive: true, wantRetired: "force-archived g1"}, + {name: "a cleanup of an empty folder", isEmptyFolder: true, wantRetired: "removed the empty folder"}, + } { + arrange := func(t *testing.T) *cleanupFixture { + fixture := newCleanupFixture(t) + fixture.service.ForceArchive = tc.isForceArchive + if tc.isEmptyFolder { + fixture.git.top = fixture.project + } + opensIssues(fixture, firstIssue) + return fixture + } + t.Run(tc.name+" refuses an open issue", func(t *testing.T) { + // Arrange + fixture := arrange(t) + + // Act + _, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err == nil || !strings.Contains(err.Error(), firstIssue) { + t.Fatalf("Cleanup = %v, want a refusal naming %s", err, firstIssue) + } + assertNothingRetired(t, fixture) + if len(fixture.runner.tabClosed) != 0 { + t.Errorf("tab closes = %v, want none from a refused cleanup", fixture.runner.tabClosed) + } + }) + t.Run(tc.name+" keeps an open issue open", func(t *testing.T) { + // Arrange + fixture := arrange(t) + fixture.service.KeepOpen = []string{firstIssue} + fixture.service.KeepReason = "carried on elsewhere" + + // Act + result, err := fixture.service.Cleanup(t.Context(), "g1") + + // Assert + if err != nil || !strings.Contains(result.Output, tc.wantRetired) { + t.Fatalf("Cleanup = %q, %v; want the task retired", result.Output, err) + } + if want := "\nkept the issue " + firstIssue + " open: carried on elsewhere"; !strings.Contains(result.Output, want) { + t.Errorf("output = %q, want it to say %q", result.Output, want) + } + status, err := state.TailStatus(fixture.stateDir, "g1", 10) + if err != nil || len(status) != 2 { + t.Fatalf("status = %v, %v; want the kept issue, then the retirement", status, err) + } + if _, event := state.SplitStatus(status[0]); event != "issue-kept-open: "+firstIssue+": carried on elsewhere" { + t.Errorf("first status line = %q, want the kept issue and its reason", event) + } + if _, err := state.ReadTaskMeta(fixture.stateDir, "g1"); !errors.Is(err, os.ErrNotExist) { + t.Errorf("task record after the cleanup: %v, want it retired", err) + } + }) + } +} diff --git a/internal/pipeline/closing_issues.go b/internal/pipeline/closing_issues.go new file mode 100644 index 000000000..298601320 --- /dev/null +++ b/internal/pipeline/closing_issues.go @@ -0,0 +1,31 @@ +package pipeline + +import ( + "context" + "path/filepath" + "slices" + "strings" +) + +// ClosingIssues are the issues every gate run of branch in project was +// started to close, as goblins pipeline run --closes handed them to the gate +// and the gate keeps them: each N or owner/name#N, in the order the runs +// started, each once. A branch whose runs closed none has none. +func (r Reader) ClosingIssues(ctx context.Context, project, branch string) ([]string, error) { + var rows []struct { + Refs string `json:"refs"` + } + sql := `SELECT COALESCE(runs.closing_issue_refs,'') AS refs FROM runs JOIN repos ON repos.id=runs.repo_id WHERE lower(replace(repos.working_path,char(92),'/'))=lower(` + sqlString(filepath.ToSlash(project)) + `) AND runs.branch=` + sqlString(branch) + ` ORDER BY runs.created_at, runs.id` + if err := r.query(ctx, sql, &rows); err != nil { + return nil, err + } + var issues []string + for _, row := range rows { + for _, issue := range strings.Split(row.Refs, "\n") { + if issue = strings.TrimSpace(issue); issue != "" && !slices.Contains(issues, issue) { + issues = append(issues, issue) + } + } + } + return issues, nil +} diff --git a/internal/pipeline/closing_issues_test.go b/internal/pipeline/closing_issues_test.go new file mode 100644 index 000000000..3324ff603 --- /dev/null +++ b/internal/pipeline/closing_issues_test.go @@ -0,0 +1,65 @@ +package pipeline + +import ( + "context" + "database/sql" + "path/filepath" + "slices" + "testing" + + "github.com/fpresta0607/code-goblins/internal/execx" + "github.com/fpresta0607/code-goblins/internal/gatedb" +) + +func TestClosingIssuesReadsEveryRunOfTheBranchOnce(t *testing.T) { + // Arrange + root := t.TempDir() + project := filepath.Join(root, "project") + writer, err := sql.Open("sqlite", filepath.Join(root, "state.sqlite")) + if err != nil { + t.Fatal(err) + } + _, err = writer.Exec(` +CREATE TABLE repos(id TEXT, working_path TEXT); +CREATE TABLE runs(id TEXT, repo_id TEXT, branch TEXT, closing_issue_refs TEXT, created_at INTEGER); +INSERT INTO repos VALUES ('repo', ?), ('other-repo', ?); +INSERT INTO runs VALUES + ('first', 'repo', 'fix/x', '12', 100), + ('none', 'repo', 'fix/x', NULL, 150), + ('second', 'repo', 'fix/x', '12 +northwind/api#7', 200), + ('other-branch', 'repo', 'fix/y', '99', 250), + ('other-repo', 'other-repo', 'fix/x', '98', 250); +`, project, filepath.Join(root, "other")) + if err != nil { + t.Fatal(err) + } + if err := writer.Close(); err != nil { + t.Fatal(err) + } + reader := Reader{Root: root, Commands: gatedb.Runner{Next: execx.OSRunner{}}} + + // Act + issues, err := reader.ClosingIssues(context.Background(), project, "fix/x") + + // Assert + if err != nil { + t.Fatal(err) + } + if want := []string{"12", "northwind/api#7"}; !slices.Equal(issues, want) { + t.Fatalf("closing issues = %q, want %q", issues, want) + } +} + +func TestClosingIssuesFailsWithoutADatabase(t *testing.T) { + // Arrange + reader := Reader{Root: t.TempDir(), Commands: gatedb.Runner{Next: execx.OSRunner{}}} + + // Act + _, err := reader.ClosingIssues(context.Background(), "C:/work/project", "fix/x") + + // Assert + if err == nil { + t.Fatal("closing issues read from no database, want an error") + } +} diff --git a/internal/tickets/issue_refs.go b/internal/tickets/issue_refs.go new file mode 100644 index 000000000..f845c6f3f --- /dev/null +++ b/internal/tickets/issue_refs.go @@ -0,0 +1,264 @@ +package tickets + +import ( + "regexp" + "slices" + "strconv" + "strings" +) + +var ( + // issueRefText is one issue as ParseIssueRef reads it, in lower case. + issueRefText = regexp.MustCompile(`^(?:https://github\.com/([\w.-]+/[\w.-]+)/issues/(\d+)|([\w.-]+/[\w.-]+)#(\d+))$`) + + qualifiedIssue = regexp.MustCompile(`(?i)([\w.-]+/[\w.-]+)#(\d+)\b`) + // bareIssue is a #N that stands alone: not the end of owner/name#N and not + // an HTML entity such as '. + bareIssue = regexp.MustCompile(`(?:^|[^\w&])#(\d+)\b`) + // pullRequestWord ends the text before a #N that names a pull request. + pullRequestWord = regexp.MustCompile(`(?i)\b(?:prs?|pull requests?)\s*$`) + + // issueWordsList is the word issue or issues and what it counts off: one + // number, or several set apart by commas, "&", "and" or "or", each a + // number or a range such as "2854 to 2860". + issueWordsList = regexp.MustCompile(`(?i)\bissues?\s+` + rowIssueItem + `(?:(?:\s*(?:,|&)\s*(?:(?:and|or)\s+)?|\s+(?:and|or)\s+)` + rowIssueItem + `)*`) + rowIssueNumber = regexp.MustCompile(`(?i)#?(\d+)(?:\s+to\s+#?(\d+))?`) + // dateTail follows a number that is part of a date or a path, not an issue. + dateTail = regexp.MustCompile(`^[-/.]\d`) + + // closingKeyword is a word that closes the issue it is followed by, or + // says a pull request is part of it, and that issue written as #N, + // owner/name#N or an issue address. + closingKeyword = regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?|part\s+of)\b\s*:?\s*(https://github\.com/[\w.-]+/[\w.-]+/issues/\d+|[\w.-]+/[\w.-]+#\d+|#\d+)`) +) + +const ( + // maxIssueRange is the most numbers a row's "issues N to M" is read as. + maxIssueRange = 50 + // rowIssueItem is one number of a row's issue words, or a range of them. + rowIssueItem = `#?\d+\b(?:\s+to\s+#?\d+\b)?` +) + +// IssueRef is one issue by its repository, owner/name in lower case, and its +// number. +type IssueRef struct { + Repository string + Number int +} + +// String is the issue as owner/name#N. +func (r IssueRef) String() string { + return r.Repository + "#" + strconv.Itoa(r.Number) +} + +// URL is the address of the issue on GitHub. +func (r IssueRef) URL() string { + return "https://github.com/" + r.Repository + "/issues/" + strconv.Itoa(r.Number) +} + +// ParseIssueRef reads one issue written as owner/name#N or as its +// https://github.com/owner/name/issues/N address, with surrounding space +// trimmed and its case folded. Anything else, a bare number included, is not +// one. +func ParseIssueRef(text string) (IssueRef, bool) { + match := issueRefText.FindStringSubmatch(strings.ToLower(strings.TrimSpace(text))) + if match == nil { + return IssueRef{}, false + } + repository, digits := match[1], match[2] + if repository == "" { + repository, digits = match[3], match[4] + } + return issueRefOf(repository, digits) +} + +// issueRefOf is issue digits of repository, or false when digits are not a +// positive number. +func issueRefOf(repository, digits string) (IssueRef, bool) { + number, err := strconv.ParseUint(digits, 10, 31) + if err != nil || number < 1 { + return IssueRef{}, false + } + return IssueRef{Repository: strings.ToLower(repository), Number: int(number)}, true +} + +// withIssueRef is refs with ref added, unless it is there already. +func withIssueRef(refs []IssueRef, ref IssueRef) []IssueRef { + if slices.Contains(refs, ref) { + return refs + } + return append(refs, ref) +} + +// BriefIssueRefs are the issues a task's brief names to take up: every issue +// NamedIssues names, as an issue of repository, owner/name, then every issue +// address of any other repository inside the brief's scope, and then those of +// each section the CEO added to the brief later, under a heading that starts +// "## ADDED", since it adds work to the task: the same, read as a Task of its +// own, and those its heading and body name in words, as "issue 42" or +// "issues 34 and 25", the way the CEO writes them there. Each is there once, +// where it is first mentioned. +func BriefIssueRefs(text, repository string) []IssueRef { + var refs []IssueRef + for _, named := range NamedIssues(text, repository) { + if ref, ok := issueRefOf(repository, strconv.Itoa(named.Number)); ok { + refs = withIssueRef(refs, ref) + } + } + for _, match := range issueURL.FindAllStringSubmatch(briefScope(text), -1) { + if strings.EqualFold(match[1], repository) { + continue + } + if ref, ok := issueRefOf(match[1], match[2]); ok { + refs = withIssueRef(refs, ref) + } + } + for _, added := range addedSections(text) { + for _, ref := range BriefIssueRefs("## Task\n"+added, repository) { + refs = withIssueRef(refs, ref) + } + for _, mention := range issueWordMentions(added, repository) { + refs = withIssueRef(refs, mention.ref) + } + } + return refs +} + +// addedSections are a brief's sections whose heading starts "## ADDED", in +// order, each its heading's words and then its body. +func addedSections(text string) []string { + var sections []string + var body []string + isAdded := false + for _, line := range strings.Split(text, "\n") { + if heading, ok := strings.CutPrefix(strings.TrimSpace(line), "## "); ok { + if isAdded { + sections = append(sections, strings.Join(body, "\n")) + } + isAdded = strings.HasPrefix(strings.TrimSpace(heading), "ADDED") + body = []string{heading} + continue + } + if isAdded { + body = append(body, line) + } + } + if isAdded { + sections = append(sections, strings.Join(body, "\n")) + } + return sections +} + +// RowIssueRefs are the issues a backlog row's title line names, in the order +// it names them and each once: issue addresses of any repository, +// owner/name#N, a bare #N of repository, and the words "issue N", "issue +// #N", "issues N, M and K" and "issues N to M", whatever their case. A range +// is read only when it spans at most maxIssueRange numbers. A number that no +// issue word or # goes with, as in "PR 2844", "pull request 2844" or a date, +// is not an issue, and neither is a #N right after "PR" or "pull request". +func RowIssueRefs(row, repository string) []IssueRef { + mentions := issueWordMentions(row, repository) + add := func(at int, ref IssueRef, ok bool) { + if ok { + mentions = append(mentions, issueMention{at, ref}) + } + } + for _, match := range issueURL.FindAllStringSubmatchIndex(row, -1) { + ref, ok := issueRefOf(row[match[2]:match[3]], row[match[4]:match[5]]) + add(match[0], ref, ok) + } + for _, match := range qualifiedIssue.FindAllStringSubmatchIndex(row, -1) { + if match[0] > 0 && row[match[0]-1] == '/' { + continue + } + ref, ok := issueRefOf(row[match[2]:match[3]], row[match[4]:match[5]]) + add(match[0], ref, ok) + } + for _, match := range bareIssue.FindAllStringSubmatchIndex(row, -1) { + if pullRequestWord.MatchString(row[:match[2]-1]) { + continue + } + ref, ok := issueRefOf(repository, row[match[2]:match[3]]) + add(match[2]-1, ref, ok) + } + slices.SortStableFunc(mentions, func(a, b issueMention) int { return a.at - b.at }) + var refs []IssueRef + for _, m := range mentions { + refs = withIssueRef(refs, m.ref) + } + return refs +} + +// issueMention is an issue a text names and where it names it. +type issueMention struct { + at int + ref IssueRef +} + +// issueWordMentions are the issues of repository that text names in words: +// "issue N", "issue #N", "issues N, M and K" and "issues N to M", whatever +// their case, a range read only when it spans at most maxIssueRange numbers, +// and none whose words run into a date. +func issueWordMentions(text, repository string) []issueMention { + var mentions []issueMention + for _, words := range issueWordsList.FindAllStringIndex(text, -1) { + if dateTail.MatchString(text[words[1]:]) { + continue + } + for _, item := range rowIssueNumber.FindAllStringSubmatchIndex(text[words[0]:words[1]], -1) { + first, err := strconv.Atoi(text[words[0]+item[2] : words[0]+item[3]]) + if err != nil { + continue + } + last := first + if item[4] >= 0 { + if last, err = strconv.Atoi(text[words[0]+item[4] : words[0]+item[5]]); err != nil || last < first || last-first >= maxIssueRange { + continue + } + } + for number := first; number <= last; number++ { + if ref, ok := issueRefOf(repository, strconv.Itoa(number)); ok { + mentions = append(mentions, issueMention{words[0] + item[0], ref}) + } + } + } + } + return mentions +} + +// ClosingIssueRefs are the issues a gate run closes, as no-mistakes stores +// them: one to a line, each "N" of repository or "owner/name#N". Blank and +// unreadable lines are passed over. +func ClosingIssueRefs(value, repository string) []IssueRef { + var refs []IssueRef + for _, line := range strings.Split(value, "\n") { + line = strings.TrimSpace(line) + ref, ok := ParseIssueRef(line) + if !ok { + ref, ok = issueRefOf(repository, line) + } + if ok { + refs = withIssueRef(refs, ref) + } + } + return refs +} + +// BodyIssueRefs are the issues a pull request's body names after a closing +// keyword (close, fix or resolve, in any form) or the words "Part of", +// optionally followed by a colon: each written as #N of repository, +// owner/name#N or an issue address. A bare #N anywhere else in the body is not +// named. +func BodyIssueRefs(body, repository string) []IssueRef { + var refs []IssueRef + for _, match := range closingKeyword.FindAllStringSubmatch(body, -1) { + ref, ok := ParseIssueRef(match[1]) + if digits, isBare := strings.CutPrefix(match[1], "#"); isBare { + ref, ok = issueRefOf(repository, digits) + } + if ok { + refs = withIssueRef(refs, ref) + } + } + return refs +} diff --git a/internal/tickets/issue_refs_test.go b/internal/tickets/issue_refs_test.go new file mode 100644 index 000000000..c1483ef58 --- /dev/null +++ b/internal/tickets/issue_refs_test.go @@ -0,0 +1,203 @@ +package tickets + +import ( + "fmt" + "slices" + "testing" +) + +func issueRefs(repository string, numbers ...int) []IssueRef { + var refs []IssueRef + for _, number := range numbers { + refs = append(refs, IssueRef{Repository: repository, Number: number}) + } + return refs +} + +func TestIssueRefWritesItselfAsAnIdentifierAndAnAddress(t *testing.T) { + // Arrange + ref := IssueRef{Repository: "acme/widgets", Number: 7} + + // Act + identifier, address := ref.String(), ref.URL() + + // Assert + if identifier != "acme/widgets#7" || address != "https://github.com/acme/widgets/issues/7" { + t.Fatalf("String = %q, URL = %q", identifier, address) + } +} + +func TestParseIssueRefReadsOnlyAFullyNamedIssue(t *testing.T) { + cases := []struct { + name, text string + want IssueRef + isIssue bool + }{ + {name: "owner name and number", text: "acme/widgets#7", want: IssueRef{"acme/widgets", 7}, isIssue: true}, + {name: "address", text: "https://github.com/acme/widgets/issues/7", want: IssueRef{"acme/widgets", 7}, isIssue: true}, + {name: "space is trimmed and case folded", text: " Acme/Widgets#7\n", want: IssueRef{"acme/widgets", 7}, isIssue: true}, + {name: "address case is folded", text: "https://github.com/Acme/Widgets/issues/7", want: IssueRef{"acme/widgets", 7}, isIssue: true}, + {name: "bare number", text: "7"}, + {name: "bare hash number", text: "#7"}, + {name: "pull request address", text: "https://github.com/acme/widgets/pull/7"}, + {name: "address with a tail", text: "https://github.com/acme/widgets/issues/7/extra"}, + {name: "zero", text: "acme/widgets#0"}, + {name: "number too large", text: "acme/widgets#99999999999"}, + {name: "empty", text: ""}, + {name: "words around it", text: "see acme/widgets#7"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Act + got, isIssue := ParseIssueRef(tc.text) + + // Assert + if isIssue != tc.isIssue || got != tc.want { + t.Fatalf("ParseIssueRef(%q) = %v, %v, want %v, %v", tc.text, got, isIssue, tc.want, tc.isIssue) + } + }) + } +} + +func TestBriefIssueRefsNamesTheRepositorysIssuesThenOtherRepositoriesAddresses(t *testing.T) { + cases := []struct { + name, text string + want []IssueRef + }{ + { + name: "own issues first then foreign addresses, each once", + text: "## Task\nFix https://github.com/Other/Repo/issues/5 and issue #3, also https://github.com/acme/widgets/issues/9.\nSee https://github.com/other/repo/issues/5 again.\n", + want: append(issueRefs("acme/widgets", 3, 9), IssueRef{"other/repo", 5}), + }, + { + name: "a listed bare number is named", + text: "## Task\n- #1343 Project Home\n", + want: issueRefs("acme/widgets", 1343), + }, + { + name: "constraints are not read", + text: "## Task\nFix issue #3.\n## Constraints\nDo not touch https://github.com/other/repo/issues/5 or issue #4.\n", + want: issueRefs("acme/widgets", 3), + }, + { + name: "a section the CEO added later is read as the task's own", + text: "## Task\nFix issue #3.\n## Constraints\nLeave issue #4 alone.\n## ADDED 2026-11-19 about 22:40Z - the stats page, issue 42, and issues 34 and 25\nIssue: https://github.com/acme/widgets/issues/188.\n- #190 the toggle\nAlso https://github.com/other/repo/issues/7.\n1. Issue 50, the tab left open.\nThe other task (#185) adds a picker, so leave it.\n", + want: append(append(issueRefs("acme/widgets", 3, 188, 190), IssueRef{"other/repo", 7}), issueRefs("acme/widgets", 42, 34, 25, 50)...), + }, + { + name: "a heading that starts Added in another case is not an added section", + text: "## Task\nTidy the docs.\n## Added context\nSee issue 12 for background.\n", + }, + {name: "no issue", text: "## Task\nTidy the docs.\n"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Act + got := BriefIssueRefs(tc.text, "acme/widgets") + + // Assert + if !slices.Equal(got, tc.want) { + t.Fatalf("BriefIssueRefs = %v, want %v", got, tc.want) + } + }) + } +} + +func TestRowIssueRefsReadsTheIssuesATitleLineNames(t *testing.T) { + cases := []struct { + name, row string + want []IssueRef + }{ + {name: "issue with a repository note", row: "find the root cause and fix it, issue 192 (repo: SIQshift)", want: issueRefs("acme/widgets", 192)}, + {name: "list with a comma and", row: "issues 2901, 2900 and 2580", want: issueRefs("acme/widgets", 2901, 2900, 2580)}, + {name: "list with an oxford and", row: "issues 1, 2, and 3", want: issueRefs("acme/widgets", 1, 2, 3)}, + {name: "list with ampersand and or", row: "Issues #1 & #2 or 3", want: issueRefs("acme/widgets", 1, 2, 3)}, + {name: "range", row: "issues 2854 to 2860", want: issueRefs("acme/widgets", 2854, 2855, 2856, 2857, 2858, 2859, 2860)}, + {name: "range of exactly fifty", row: "issues 1 to 50", want: issueRefs("acme/widgets", func() []int { + numbers := make([]int, 50) + for index := range numbers { + numbers[index] = index + 1 + } + return numbers + }()...)}, + {name: "range wider than fifty is ignored", row: "issues 1 to 51", want: nil}, + {name: "backwards range is ignored", row: "issues 9 to 3", want: nil}, + {name: "owner name and number", row: "Issue fpresta0607/peakCraftsman#68", want: []IssueRef{{"fpresta0607/peakcraftsman", 68}}}, + {name: "owner name and number after issue", row: "issue fpresta0607/russo-pool-spa#1", want: []IssueRef{{"fpresta0607/russo-pool-spa", 1}}}, + {name: "address of another repository", row: "fix https://github.com/Other/Repo/issues/12 now", want: []IssueRef{{"other/repo", 12}}}, + {name: "bare hash number", row: "fix #44 in the board", want: issueRefs("acme/widgets", 44)}, + {name: "in the order named, once each", row: "#5, issue 4, other/repo#6, issue #5, issue 4", want: append(issueRefs("acme/widgets", 5, 4), IssueRef{"other/repo", 6})}, + {name: "pull request is not an issue", row: "land PR 2844 and pull request 2845", want: nil}, + {name: "hash after PR is not an issue", row: "land PR #2844 and pull request #2845", want: nil}, + {name: "date is not an issue", row: "release notes for 2026-11-19", want: nil}, + {name: "date after the word issue is not an issue", row: "issue 2026-11-19", want: nil}, + {name: "entity is not an issue", row: "quote 'it'", want: nil}, + {name: "path with a hash is not a repository", row: "see docs/guide/readme#3", want: nil}, + {name: "word inside another word", row: "reissue 5", want: nil}, + {name: "zero is not an issue", row: "issue 0", want: nil}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Act + got := RowIssueRefs(tc.row, "Acme/Widgets") + + // Assert + if !slices.Equal(got, tc.want) { + t.Fatalf("RowIssueRefs(%q) = %v, want %v", tc.row, got, tc.want) + } + }) + } +} + +func TestClosingIssueRefsReadsOneIssueToALine(t *testing.T) { + cases := []struct { + name, value string + want []IssueRef + }{ + {name: "numbers and qualified issues", value: "12\nOther/Repo#3\n 14 \n", want: append(issueRefs("acme/widgets", 12), IssueRef{"other/repo", 3}, IssueRef{"acme/widgets", 14})}, + {name: "windows line ends", value: "12\r\n13\r\n", want: issueRefs("acme/widgets", 12, 13)}, + {name: "blank and unreadable lines are passed over", value: "\nnot an issue\n+5\n-5\n0\n12\n", want: issueRefs("acme/widgets", 12)}, + {name: "each issue once", value: "12\nacme/widgets#12\n", want: issueRefs("acme/widgets", 12)}, + {name: "empty", value: ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Act + got := ClosingIssueRefs(tc.value, "Acme/Widgets") + + // Assert + if !slices.Equal(got, tc.want) { + t.Fatalf("ClosingIssueRefs(%q) = %v, want %v", tc.value, got, tc.want) + } + }) + } +} + +func TestBodyIssueRefsReadsOnlyWhatAKeywordNames(t *testing.T) { + cases := []struct { + body string + want []IssueRef + }{ + {body: "Closes #5", want: issueRefs("acme/widgets", 5)}, + {body: "fixed: #6\nResolves other/repo#7\nPart of https://github.com/Third/Repo/issues/8", want: []IssueRef{{"acme/widgets", 6}, {"other/repo", 7}, {"third/repo", 8}}}, + {body: "CLOSE #1, close #2, Fix #3, FIXES #4, resolve #5, Resolved #6, closed #7, fixed #8, resolves #9", want: issueRefs("acme/widgets", 1, 2, 3, 4, 5, 6, 7, 8, 9)}, + {body: "PART OF: #10", want: issueRefs("acme/widgets", 10)}, + {body: "Closes #5 and Fixes #5", want: issueRefs("acme/widgets", 5)}, + {body: "See #5 and the discussion in #6", want: nil}, + {body: "prefix #5 and disclose #6", want: nil}, + {body: "Closes the loop\n#5", want: nil}, + {body: "Part of the work, issue 3", want: nil}, + {body: "", want: nil}, + } + for _, tc := range cases { + t.Run(fmt.Sprintf("%q", tc.body), func(t *testing.T) { + // Act + got := BodyIssueRefs(tc.body, "Acme/Widgets") + + // Assert + if !slices.Equal(got, tc.want) { + t.Fatalf("BodyIssueRefs(%q) = %v, want %v", tc.body, got, tc.want) + } + }) + } +} diff --git a/internal/tickets/open_issues.go b/internal/tickets/open_issues.go new file mode 100644 index 000000000..08b0562f6 --- /dev/null +++ b/internal/tickets/open_issues.go @@ -0,0 +1,50 @@ +package tickets + +import ( + "context" + "encoding/json" + "fmt" +) + +// OpenIssue is an open issue of a repository as GitHub lists it. +type OpenIssue struct { + Number int + Title string + URL string + Labels []string +} + +// OpenIssues reads every open issue of repository, owner/name, with the pull +// requests GitHub lists among them left out, a page of a hundred at a time, up +// to maxPageRounds pages; a repository with more is an error, never a partial +// answer. +func (g GitHub) OpenIssues(ctx context.Context, repository string) ([]OpenIssue, error) { + var open []OpenIssue + for page := 1; page <= maxPageRounds; page++ { + out, err := g.api(ctx, "GET", fmt.Sprintf("repos/%s/issues?state=open&per_page=100&page=%d", repository, page)) + if err != nil { + return nil, err + } + var issues []struct { + githubIssue + Title string `json:"title"` + } + if err := json.Unmarshal(out, &issues); err != nil { + return nil, fmt.Errorf("read the open issues of %s: %w", repository, err) + } + for _, issue := range issues { + if issue.PullRequest != nil { + continue + } + listed := OpenIssue{Number: issue.Number, Title: issue.Title, URL: issue.URL} + for _, label := range issue.Labels { + listed.Labels = append(listed.Labels, label.Name) + } + open = append(open, listed) + } + if len(issues) < 100 { + return open, nil + } + } + return nil, fmt.Errorf("%s has more than %d open issues, so they cannot all be read", repository, maxPageRounds*100) +} diff --git a/internal/tickets/open_issues_test.go b/internal/tickets/open_issues_test.go new file mode 100644 index 000000000..bdb60b686 --- /dev/null +++ b/internal/tickets/open_issues_test.go @@ -0,0 +1,124 @@ +package tickets + +import ( + "context" + "fmt" + "net/http" + "slices" + "strings" + "testing" + + "github.com/fpresta0607/code-goblins/internal/githubtest" +) + +// openIssuesGitHub answers GitHub's list of open issues a page of a hundred +// at a time: issues is every open issue, as GitHub lists them. It records the +// path of each request. +type openIssuesGitHub struct { + issues []map[string]any + paths []string +} + +func (f *openIssuesGitHub) ServeHTTP(w http.ResponseWriter, r *http.Request) { + call, err := readCall(r) + if err != nil { + githubtest.Refuse(w, http.StatusBadRequest, err.Error()) + return + } + f.paths = append(f.paths, call.Path) + var page int + if _, err := fmt.Sscanf(call.Path[strings.Index(call.Path, "&page=")+len("&page="):], "%d", &page); err != nil || !strings.HasPrefix(call.Path, "repos/"+rootRepository+"/issues?state=open&per_page=100&page=") { + githubtest.Refuse(w, http.StatusNotFound, "Not Found") + return + } + start := min((page-1)*100, len(f.issues)) + githubtest.Write(w, http.StatusOK, f.issues[start:min(start+100, len(f.issues))]) +} + +func numberedIssues(count int) []map[string]any { + issues := make([]map[string]any, count) + for index := range issues { + issues[index] = map[string]any{"number": index + 1, "title": fmt.Sprint("issue ", index+1), "html_url": fmt.Sprintf("https://github.com/%s/issues/%d", rootRepository, index+1)} + } + return issues +} + +func TestOpenIssuesSkipsPullRequestsAndKeepsLabels(t *testing.T) { + // Arrange + fake := &openIssuesGitHub{issues: []map[string]any{ + {"number": 1, "title": "first", "html_url": "https://github.com/acme/widgets/issues/1", "labels": []map[string]any{{"name": "bug"}, {"name": "ops-root"}}}, + {"number": 2, "title": "a pull request", "html_url": "https://github.com/acme/widgets/pull/2", "pull_request": map[string]any{"url": "x"}}, + {"number": 3, "title": "third", "html_url": "https://github.com/acme/widgets/issues/3"}, + }} + + // Act + open, err := GitHub{API: githubtest.Client(fake)}.OpenIssues(context.Background(), rootRepository) + + // Assert + if err != nil { + t.Fatal(err) + } + want := []OpenIssue{ + {Number: 1, Title: "first", URL: "https://github.com/acme/widgets/issues/1", Labels: []string{"bug", "ops-root"}}, + {Number: 3, Title: "third", URL: "https://github.com/acme/widgets/issues/3"}, + } + if fmt.Sprint(open) != fmt.Sprint(want) { + t.Fatalf("open = %+v, want %+v", open, want) + } + if len(fake.paths) != 1 { + t.Fatalf("paths = %v, want one page", fake.paths) + } +} + +func TestOpenIssuesReadsAnotherPageWhenTheFirstIsFull(t *testing.T) { + // Arrange + fake := &openIssuesGitHub{issues: numberedIssues(150)} + + // Act + open, err := GitHub{API: githubtest.Client(fake)}.OpenIssues(context.Background(), rootRepository) + + // Assert + if err != nil { + t.Fatal(err) + } + if len(open) != 150 || open[149].Number != 150 { + t.Fatalf("read %d issues, want 150", len(open)) + } + if want := []string{ + "repos/acme/widgets/issues?state=open&per_page=100&page=1", + "repos/acme/widgets/issues?state=open&per_page=100&page=2", + }; !slices.Equal(fake.paths, want) { + t.Fatalf("paths = %v, want %v", fake.paths, want) + } +} + +func TestOpenIssuesRefusesMoreThanItCanRead(t *testing.T) { + // Arrange + fake := &openIssuesGitHub{issues: numberedIssues(maxPageRounds*100 + 1)} + + // Act + open, err := GitHub{API: githubtest.Client(fake)}.OpenIssues(context.Background(), rootRepository) + + // Assert + if err == nil || !strings.Contains(err.Error(), "more than 1000 open issues") || open != nil { + t.Fatalf("open = %d issues, err = %v, want an error and no partial answer", len(open), err) + } + if len(fake.paths) != maxPageRounds { + t.Fatalf("read %d pages, want %d", len(fake.paths), maxPageRounds) + } +} + +func TestOpenIssuesFailsWhenGitHubRefuses(t *testing.T) { + // Arrange + refuse := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + githubtest.Refuse(w, http.StatusUnauthorized, "Bad credentials") + }) + + // Act + open, err := GitHub{API: githubtest.Client(refuse)}.OpenIssues(context.Background(), rootRepository) + + // Assert + if err == nil || open != nil { + t.Fatalf("open = %v, err = %v, want an error", open, err) + } +} diff --git a/internal/tickets/pull_issues.go b/internal/tickets/pull_issues.go new file mode 100644 index 000000000..767cb1aa2 --- /dev/null +++ b/internal/tickets/pull_issues.go @@ -0,0 +1,99 @@ +package tickets + +import ( + "context" + "fmt" + "regexp" + "strconv" + "strings" +) + +// pullIssuesBatchSize is how many pull requests one GraphQL query reads. +const pullIssuesBatchSize = 50 + +// pullAddress is a pull request's address on GitHub. +var pullAddress = regexp.MustCompile(`^https://github\.com/([\w.-]+/[\w.-]+)/pull/\d+$`) + +// pullIssuesAnswer is one pull request as its aliased field answers. A +// resource that is not a pull request answers an empty object, so no URL. +type pullIssuesAnswer struct { + URL string `json:"url"` + Body string `json:"body"` + Closing struct { + Nodes []struct { + Number int `json:"number"` + Repository struct { + NameWithOwner string `json:"nameWithOwner"` + } `json:"repository"` + } `json:"nodes"` + } `json:"closingIssuesReferences"` +} + +// PullRequestIssues reads, for each pull request address, such as +// https://github.com/owner/name/pull/N, the issues it names: those GitHub +// links it to close and those BodyIssueRefs finds in its body, with the pull +// request's own repository standing for a bare #N. It reads them through +// GitHub's GraphQL API, fifty pull requests to a query, the addresses sent as +// query variables and never spliced into the query text. A duplicate address +// is read once. named has an entry, empty when it names none, for every pull +// request GitHub answered, keyed by the address as given. An address that is +// not a pull request address, or that GitHub answers with no pull request, is +// returned in unread instead of failing the whole read; a query that fails is +// an error. +func (g GitHub) PullRequestIssues(ctx context.Context, addresses []string) (named map[string][]IssueRef, unread []string, err error) { + named = map[string][]IssueRef{} + seen := map[string]bool{} + var readable []string + for _, address := range addresses { + if seen[address] { + continue + } + seen[address] = true + if pullAddress.MatchString(address) { + readable = append(readable, address) + } else { + unread = append(unread, address) + } + } + for start := 0; start < len(readable); start += pullIssuesBatchSize { + batch := readable[start:min(start+pullIssuesBatchSize, len(readable))] + query, variables := pullIssuesQuery(batch) + var answer map[string]*pullIssuesAnswer + if err := g.API.GraphQL(ctx, query, variables, &answer); err != nil { + return nil, nil, fmt.Errorf("read the issues %d pull requests name: %w", len(batch), err) + } + for index, address := range batch { + pull := answer["pull"+strconv.Itoa(index)] + if pull == nil || pull.URL == "" { + unread = append(unread, address) + continue + } + repository := pullAddress.FindStringSubmatch(address)[1] + var refs []IssueRef + for _, node := range pull.Closing.Nodes { + if ref, ok := issueRefOf(node.Repository.NameWithOwner, strconv.Itoa(node.Number)); ok { + refs = withIssueRef(refs, ref) + } + } + for _, ref := range BodyIssueRefs(pull.Body, repository) { + refs = withIssueRef(refs, ref) + } + named[address] = refs + } + } + return named, unread, nil +} + +// pullIssuesQuery is the query that reads each of addresses as an aliased +// field, pull0, pull1 and so on, and the variables that carry the addresses. +func pullIssuesQuery(addresses []string) (string, map[string]any) { + variables := map[string]any{} + var declarations, fields []string + for index, address := range addresses { + number := strconv.Itoa(index) + variables["url"+number] = address + declarations = append(declarations, "$url"+number+": URI!") + fields = append(fields, " pull"+number+": resource(url: $url"+number+") { ... on PullRequest { url body closingIssuesReferences(first: 50) { nodes { number repository { nameWithOwner } } } } }") + } + return "query(" + strings.Join(declarations, ", ") + ") {\n" + strings.Join(fields, "\n") + "\n}", variables +} diff --git a/internal/tickets/pull_issues_test.go b/internal/tickets/pull_issues_test.go new file mode 100644 index 000000000..8923ee679 --- /dev/null +++ b/internal/tickets/pull_issues_test.go @@ -0,0 +1,200 @@ +package tickets + +import ( + "context" + "fmt" + "maps" + "net/http" + "slices" + "strings" + "testing" + + "github.com/fpresta0607/code-goblins/internal/githubtest" +) + +// pullIssuesGitHub answers the GraphQL queries PullRequestIssues makes the way +// GitHub would: answers holds, by pull request address, the aliased field each +// is answered with, and an address it holds none for is answered null. Each +// query's text and variables are recorded. +type pullIssuesGitHub struct { + answers map[string]map[string]any + queries []string + variables []map[string]any + // graphqlErrors answers each query with GitHub's errors. + graphqlErrors bool +} + +func (f *pullIssuesGitHub) ServeHTTP(w http.ResponseWriter, r *http.Request) { + query, variables, err := githubtest.Query(r) + if err != nil { + githubtest.Refuse(w, http.StatusBadRequest, err.Error()) + return + } + f.queries = append(f.queries, query) + f.variables = append(f.variables, variables) + if f.graphqlErrors { + githubtest.Write(w, http.StatusOK, map[string]any{"data": nil, "errors": []map[string]any{{"message": "Something went wrong"}}}) + return + } + data := map[string]any{} + for name, value := range variables { + data["pull"+strings.TrimPrefix(name, "url")] = f.answers[fmt.Sprint(value)] + } + githubtest.Answer(w, data) +} + +func pullAnswer(address, body string, closing ...IssueRef) map[string]any { + nodes := []map[string]any{} + for _, ref := range closing { + nodes = append(nodes, map[string]any{"number": ref.Number, "repository": map[string]any{"nameWithOwner": ref.Repository}}) + } + return map[string]any{"url": address, "body": body, "closingIssuesReferences": map[string]any{"nodes": nodes}} +} + +func TestPullRequestIssuesNamesTheClosingReferencesAndTheBody(t *testing.T) { + // Arrange + const address = "https://github.com/Acme/Widgets/pull/8" + fake := &pullIssuesGitHub{answers: map[string]map[string]any{ + address: pullAnswer(address, "Part of #12\nSee #13 too.\nFixes #3", IssueRef{"Acme/Widgets", 3}, IssueRef{"Other/Repo", 4}), + }} + + // Act + named, unread, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), []string{address}) + + // Assert + if err != nil { + t.Fatal(err) + } + want := []IssueRef{{"acme/widgets", 3}, {"other/repo", 4}, {"acme/widgets", 12}} + if got := named[address]; !slices.Equal(got, want) { + t.Fatalf("named = %v, want %v", got, want) + } + if len(unread) != 0 { + t.Fatalf("unread = %v, want none", unread) + } +} + +func TestPullRequestIssuesReadsFiftyPullRequestsToAQuery(t *testing.T) { + // Arrange + fake := &pullIssuesGitHub{answers: map[string]map[string]any{}} + var addresses []string + for number := 1; number <= 51; number++ { + address := fmt.Sprintf("https://github.com/acme/widgets/pull/%d", number) + addresses = append(addresses, address) + fake.answers[address] = pullAnswer(address, fmt.Sprintf("Closes #%d", number+1000)) + } + + // Act + named, unread, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), addresses) + + // Assert + if err != nil { + t.Fatal(err) + } + if len(fake.queries) != 2 || len(fake.variables[0]) != 50 || len(fake.variables[1]) != 1 { + t.Fatalf("queries = %d with %d and %d addresses, want two with 50 and 1", len(fake.queries), len(fake.variables[0]), len(fake.variables[1])) + } + if len(named) != 51 || len(unread) != 0 { + t.Fatalf("named %d pull requests, unread = %v, want 51 and none", len(named), unread) + } + if got, want := named[addresses[50]], []IssueRef{{"acme/widgets", 1051}}; !slices.Equal(got, want) { + t.Fatalf("last pull request named %v, want %v", got, want) + } +} + +func TestPullRequestIssuesSendsAddressesAsVariablesNeverInTheQuery(t *testing.T) { + // Arrange + const address = "https://github.com/acme/widgets/pull/8" + fake := &pullIssuesGitHub{answers: map[string]map[string]any{address: pullAnswer(address, "")}} + + // Act + _, _, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), []string{address}) + + // Assert + if err != nil { + t.Fatal(err) + } + if strings.Contains(fake.queries[0], "acme") || strings.Contains(fake.queries[0], "github.com") { + t.Fatalf("query spliced the address in: %s", fake.queries[0]) + } + if got := slices.Collect(maps.Values(fake.variables[0])); !slices.Equal(got, []any{address}) { + t.Fatalf("variables = %v, want the address", fake.variables[0]) + } +} + +func TestPullRequestIssuesReadsADuplicateAddressOnce(t *testing.T) { + // Arrange + const address = "https://github.com/acme/widgets/pull/8" + fake := &pullIssuesGitHub{answers: map[string]map[string]any{address: pullAnswer(address, "Fixes #3")}} + + // Act + named, _, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), []string{address, address}) + + // Assert + if err != nil { + t.Fatal(err) + } + if len(fake.variables) != 1 || len(fake.variables[0]) != 1 || len(named) != 1 { + t.Fatalf("variables = %v, named = %v, want one address read once", fake.variables, named) + } +} + +func TestPullRequestIssuesReturnsWhatItCannotReadAsUnread(t *testing.T) { + // Arrange + const ( + missing = "https://github.com/acme/widgets/pull/9" + anIssue = "https://github.com/acme/widgets/pull/10" + malformed = "https://github.com/acme/widgets/issues/11" + readable = "https://github.com/acme/widgets/pull/8" + ) + fake := &pullIssuesGitHub{answers: map[string]map[string]any{ + readable: pullAnswer(readable, "Fixes #3"), + anIssue: {}, + }} + + // Act + named, unread, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), []string{malformed, missing, readable, anIssue, "not an address", malformed}) + + // Assert + if err != nil { + t.Fatal(err) + } + if want := []string{malformed, "not an address", missing, anIssue}; !slices.Equal(unread, want) { + t.Fatalf("unread = %v, want %v", unread, want) + } + if got, want := named[readable], []IssueRef{{"acme/widgets", 3}}; len(named) != 1 || !slices.Equal(got, want) { + t.Fatalf("named = %v, want only %s as %v", named, readable, want) + } + if len(fake.variables) != 1 || len(fake.variables[0]) != 3 { + t.Fatalf("variables = %v, want one query of the three pull request addresses", fake.variables) + } +} + +func TestPullRequestIssuesFailsWhenAQueryFails(t *testing.T) { + // Arrange + fake := &pullIssuesGitHub{graphqlErrors: true} + + // Act + named, unread, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), []string{"https://github.com/acme/widgets/pull/8"}) + + // Assert + if err == nil || !strings.Contains(err.Error(), "Something went wrong") { + t.Fatalf("err = %v, want GitHub's message", err) + } + if named != nil || unread != nil { + t.Fatalf("named = %v, unread = %v, want neither from a failed read", named, unread) + } +} + +func TestPullRequestIssuesReadsNoAddressesWithoutAQuery(t *testing.T) { + // Arrange + fake := &pullIssuesGitHub{} + + // Act + named, unread, err := GitHub{API: githubtest.Client(fake)}.PullRequestIssues(context.Background(), nil) + + // Assert + if err != nil || len(named) != 0 || len(unread) != 0 || len(fake.queries) != 0 { + t.Fatalf("named = %v, unread = %v, err = %v, queries = %d, want nothing", named, unread, err, len(fake.queries)) + } +}