Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-9170: feat(cli): add issue delete and batch delete (#10515)

Miran137fe87228cf
diff --git a/CLI_AND_DAEMON.md b/CLI_AND_DAEMON.md
index 25241efd46..323a5b2b25 100644
--- a/CLI_AND_DAEMON.md
+++ b/CLI_AND_DAEMON.md
@@ -724,6 +724,22 @@ multica issue update <id> --attachment revised.png
`--position` sets the raw ordering value within the board column (lower sorts first). For relative moves, `issue reorder` is easier because it works out the value for you.
`--attachment` uploads a local file and appends its Markdown reference to the end of the issue description. Repeat the flag to attach multiple files. To replace an existing image, also use `--description-file` to remove the old reference from the description.
+### Delete Issue
+
+```bash
+multica issue delete <id> # prompts before deleting
+multica issue delete <id> --yes # no prompt
+multica issue delete MUL-101 MUL-102 --yes --output json
+```
+
+Deletion is permanent and removes the issue's comments and attachments, cancels its in-flight runs, fails linked autopilot runs, clears duplicate marks that pointed at it, and detaches its child issues without deleting them.
+
+Ids may be issue keys or full UUIDs. Every id is resolved with a read before the prompt, full UUIDs included, so an unknown, stale or invisible issue aborts the whole command instead of leaving a partial batch; several ids go to one batch request whose permission check covers the whole selection.
+
+`--yes` skips the confirmation. Without it the command reads stdin; if stdin closes without an answer (a non-interactive run) the command fails without deleting, while answering `n` declines and deletes nothing.
+
+With `--output json`, one id prints `{"id": "<uuid>", "deleted": true}` and several print `{"ids": ["<uuid>", ...], "deleted": true}`, always with canonical UUIDs whatever spelling was passed. If the server deletes only part of a batch, the command prints `{"ids": ["<uuid>", ...], "deleted": false, "deleted_count": N}` and exits non-zero.
+
### Reorder Issue
Move an issue within its current status column. The new ordering value is computed the same way the board's drag-and-drop computes it, so the CLI and UI agree on where the issue lands.
diff --git a/server/cmd/multica/cmd_issue.go b/server/cmd/multica/cmd_issue.go
index 22dcc8c52e..e149ff2327 100644
--- a/server/cmd/multica/cmd_issue.go
+++ b/server/cmd/multica/cmd_issue.go
@@ -610,6 +610,7 @@ func init() {
issueCmd.AddCommand(issueUpdateCmd)
issueCmd.AddCommand(issueAssignCmd)
issueCmd.AddCommand(issueStatusCmd)
+ issueCmd.AddCommand(issueDeleteCmd)
issueCmd.AddCommand(issueReorderCmd)
issueCmd.AddCommand(issueCommentCmd)
issueCmd.AddCommand(issueSubscriberCmd)
@@ -711,6 +712,10 @@ func init() {
issueStatusCmd.Flags().String("duplicate-of", "", "Mark the issue as a duplicate of this original issue (key like MUL-123, or full UUID); the status must be cancelled")
issueStatusCmd.Flags().String("output", "table", "Output format: table or json")
+ // issue delete
+ issueDeleteCmd.Flags().BoolP("yes", "y", false, "Skip the confirmation prompt")
+ issueDeleteCmd.Flags().String("output", "table", "Output format: table or json")
+
// issue reorder
registerIssueReorderFlags(issueReorderCmd)
diff --git a/server/cmd/multica/cmd_issue_delete.go b/server/cmd/multica/cmd_issue_delete.go
new file mode 100644
index 0000000000..c0f4c0c093
--- /dev/null
+++ b/server/cmd/multica/cmd_issue_delete.go
@@ -0,0 +1,191 @@
+package main
+
+import (
+ "bufio"
+ "context"
+ "fmt"
+ "net/url"
+ "os"
+ "strings"
+
+ "github.com/spf13/cobra"
+
+ "github.com/multica-ai/multica/server/internal/cli"
+)
+
+// multica issue delete <id>... — permanent deletion for one issue or a batch.
+//
+// The server exposes two routes: DELETE /api/issues/{id} for one issue and
+// POST /api/issues/batch-delete for a multi-select. The batch route accepts
+// UUIDs only and silently skips references it cannot parse, so every argument
+// is resolved first and the UUIDs are sent; a typo therefore fails before
+// anything is deleted instead of producing a partial batch.
+//
+// Deletion is irreversible and has side effects the issue page does not make
+// obvious: comments and attachment objects go with the issue, in-flight runs
+// are cancelled, linked autopilot runs fail, duplicate marks that pointed at
+// the issue are cleared, and child issues are detached rather than deleted.
+// The confirmation says so.
+
+var issueDeleteCmd = &cobra.Command{
+ Use: "delete <id>...",
+ Short: "Delete one or more issues",
+ Long: "Delete one or more issues permanently.\n\n" +
+ "Deletion removes the issue's comments and attachments, cancels its in-flight\n" +
+ "runs, fails linked autopilot runs, clears duplicate marks that pointed at it,\n" +
+ "and detaches its child issues (children are not deleted). It cannot be undone.\n\n" +
+ "Pass several ids to delete them in one batch. Every id is resolved with a read\n" +
+ "before the prompt, full UUIDs included, so an unknown, stale or invisible issue\n" +
+ "aborts the whole command instead of leaving a partial batch. Ids may be issue\n" +
+ "keys (e.g. MUL-123) or full UUIDs.\n\n" +
+ "Without --yes the command asks for confirmation. A non-interactive run that\n" +
+ "cannot answer the prompt fails without deleting; pass --yes to skip it.",
+ Args: minimumArgs(1),
+ RunE: runIssueDelete,
+}
+
+func runIssueDelete(cmd *cobra.Command, args []string) error {
+ client, err := newAPIClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ resolveCtx, cancelResolve := cli.APIContext(context.Background())
+ refs, err := resolveIssueDeleteRefs(resolveCtx, client, args)
+ cancelResolve()
+ if err != nil {
+ return err
+ }
+
+ yes, _ := cmd.Flags().GetBool("yes")
+ if !yes {
+ fmt.Fprint(os.Stderr, issueDeleteConfirmation(refs))
+ answer, readErr := bufio.NewReader(os.Stdin).ReadString('\n')
+ answer = strings.TrimSpace(strings.ToLower(answer))
+ if answer != "y" && answer != "yes" {
+ if answer == "" && readErr != nil {
+ return fmt.Errorf("no confirmation received (stdin is not interactive); re-run with --yes to delete without a prompt")
+ }
+ fmt.Fprintln(os.Stderr, "Aborted.")
+ return nil
+ }
+ }
+
+ // Waiting for confirmation must not consume the deletion request's timeout.
+ ctx, cancel := cli.APIContext(context.Background())
+ defer cancel()
+
+ output, _ := cmd.Flags().GetString("output")
+
+ // One id uses the per-issue route the app uses; several use the batch
+ // route so the permission check covers the whole selection before any
+ // issue is removed.
+ if len(refs) == 1 {
+ if err := client.DeleteJSON(ctx, "/api/issues/"+url.PathEscape(refs[0].ID)); err != nil {
+ return issueDeleteError("delete issue "+refs[0].Display, err)
+ }
+ if output == "json" {
+ return cli.PrintJSON(os.Stdout, map[string]any{"id": refs[0].ID, "deleted": true})
+ }
+ fmt.Fprintf(os.Stdout, "Deleted issue %s\n", refs[0].Display)
+ return nil
+ }
+
+ ids := make([]string, len(refs))
+ for i, ref := range refs {
+ ids[i] = ref.ID
+ }
+ var result struct {
+ Deleted int `json:"deleted"`
+ }
+ if err := client.PostJSON(ctx, "/api/issues/batch-delete", map[string]any{"issue_ids": ids}, &result); err != nil {
+ return issueDeleteError("delete issues", err)
+ }
+ if result.Deleted != len(ids) {
+ // The batch route skips an id that disappeared or became invisible
+ // between resolution and deletion; saying how many landed is more
+ // useful than reporting the whole batch as failed, and JSON consumers
+ // must not see a success-shaped document.
+ if output == "json" {
+ if err := cli.PrintJSON(os.Stdout, map[string]any{
+ "ids": ids,
+ "deleted": false,
+ "deleted_count": result.Deleted,
+ }); err != nil {
+ return err
+ }
+ }
+ return fmt.Errorf("server deleted %d of %d issues; the rest were already gone or not visible to this account — run `multica issue list` to check before retrying", result.Deleted, len(ids))
+ }
+ if output == "json" {
+ return cli.PrintJSON(os.Stdout, map[string]any{"ids": ids, "deleted": true})
+ }
+ for _, ref := range refs {
+ fmt.Fprintf(os.Stdout, "Deleted issue %s\n", ref.Display)
+ }
+ return nil
+}
+
+// issueDeleteError keeps the step that failed and the server's own refusal
+// text — the 403 copy naming who may delete, or the 404 copy — instead of
+// FormatError's generic kind message.
+func issueDeleteError(step string, err error) error {
+ if msg := cli.ServerMessage(err); msg != "" {
+ return cli.WithUserMessage(step+": "+msg, err)
+ }
+ return fmt.Errorf("%s: %w", step, err)
+}
+
+// resolveIssueDeleteRefs resolves and de-duplicates every argument, keeping
+// the caller's order. Resolution is a read, so it can happen before the
+// confirmation without touching anything.
+func resolveIssueDeleteRefs(ctx context.Context, client *cli.APIClient, args []string) ([]resolvedID, error) {
+ refs := make([]resolvedID, 0, len(args))
+ seen := make(map[string]struct{}, len(args))
+ for _, arg := range args {
+ ref, err := resolveIssueDeleteRef(ctx, client, arg)
+ if err != nil {
+ return nil, issueDeleteError(fmt.Sprintf("resolve issue %q", arg), err)
+ }
+ if _, duplicate := seen[ref.ID]; duplicate {
+ continue
+ }
+ seen[ref.ID] = struct{}{}
+ refs = append(refs, ref)
+ }
+ return refs, nil
+}
+
+// resolveIssueDeleteRef resolves one argument with a read. A full UUID goes
+// through the same GET as a key: resolveIssueRef trusts a UUID without asking
+// the server (GH #7017), but delete cannot — the batch route silently skips a
+// stale UUID and deletes the rest, so the failure would land after the writes.
+func resolveIssueDeleteRef(ctx context.Context, client *cli.APIClient, input string) (resolvedID, error) {
+ trimmed := strings.TrimSpace(input)
+ if uuidRegexp.MatchString(trimmed) {
+ return fetchIssueRef(ctx, client, strings.ToLower(trimmed))
+ }
+ return resolveIssueRef(ctx, client, trimmed)
+}
+
+func issueDeleteConfirmation(refs []resolvedID) string {
+ keys := make([]string, len(refs))
+ for i, ref := range refs {
+ keys[i] = ref.Display
+ }
+
+ var target, possessive string
+ if len(keys) == 1 {
+ target = "issue " + keys[0]
+ possessive = "its"
+ } else {
+ target = fmt.Sprintf("%d issues (%s)", len(keys), strings.Join(keys, ", "))
+ possessive = "their"
+ }
+
+ return fmt.Sprintf(
+ "Delete %s? This permanently removes %s comments and attachments, cancels in-flight runs and detaches child issues; it cannot be undone. [y/N] ",
+ target,
+ possessive,
+ )
+}
diff --git a/server/cmd/multica/cmd_issue_delete_test.go b/server/cmd/multica/cmd_issue_delete_test.go
new file mode 100644
index 0000000000..92dc1a948d
--- /dev/null
+++ b/server/cmd/multica/cmd_issue_delete_test.go
@@ -0,0 +1,696 @@
+package main
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "strconv"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/multica-ai/multica/server/internal/cli"
+)
+
+const (
+ issueDeleteTestID1 = "11111111-1111-4111-8111-111111111111"
+ issueDeleteTestID2 = "22222222-2222-4222-8222-222222222222"
+)
+
+// setIssueDeleteFlags drives the command the CLI actually registers, then
+// restores the previous flag values so tests do not leak into each other.
+// Building a throwaway command here would leave the real --yes/--output
+// registration untested.
+func setIssueDeleteFlags(t *testing.T, yes bool, output string) {
+ t.Helper()
+ prevYes, err := issueDeleteCmd.Flags().GetBool("yes")
+ if err != nil {
+ t.Fatalf("read --yes: %v", err)
+ }
+ prevOutput, err := issueDeleteCmd.Flags().GetString("output")
+ if err != nil {
+ t.Fatalf("read --output: %v", err)
+ }
+ t.Cleanup(func() {
+ _ = issueDeleteCmd.Flags().Set("yes", strconv.FormatBool(prevYes))
+ _ = issueDeleteCmd.Flags().Set("output", prevOutput)
+ })
+ if err := issueDeleteCmd.Flags().Set("yes", strconv.FormatBool(yes)); err != nil {
+ t.Fatalf("set --yes: %v", err)
+ }
+ if err := issueDeleteCmd.Flags().Set("output", output); err != nil {
+ t.Fatalf("set --output: %v", err)
+ }
+}
+
+func setIssueDeleteTestEnv(t *testing.T, serverURL string) {
+ t.Helper()
+ t.Setenv("MULTICA_SERVER_URL", serverURL)
+ t.Setenv("MULTICA_WORKSPACE_ID", "ws-1")
+ t.Setenv("MULTICA_TOKEN", "test-token")
+}
+
+type issueDeleteRequest struct {
+ method string
+ path string
+}
+
+// TestIssueDeleteCommandRegistration covers the pieces only the real command
+// carries: the zero-argument guard and the --yes/-y, --output registration.
+func TestIssueDeleteCommandRegistration(t *testing.T) {
+ if got := issueDeleteCmd.Flags().Lookup("yes"); got == nil || got.DefValue != "false" {
+ t.Fatalf("--yes flag = %+v, want a bool defaulting to false", got)
+ }
+ if got := issueDeleteCmd.Flags().ShorthandLookup("y"); got == nil || got.Name != "yes" {
+ t.Fatalf("-y shorthand = %+v, want it bound to --yes", got)
+ }
+ if got := issueDeleteCmd.Flags().Lookup("output"); got == nil || got.DefValue != "table" {
+ t.Fatalf("--output flag = %+v, want table by default", got)
+ }
+ if issueDeleteCmd.Args == nil {
+ t.Fatal("delete command accepts no positional arguments")
+ }
+
+ stderr := captureStderr(t)
+ _, err := captureStdout(t, func() error {
+ return issueDeleteCmd.Args(issueDeleteCmd, nil)
+ })
+ msg := stderr.read()
+ if err == nil {
+ t.Fatal("zero arguments accepted, want the minimum-args error")
+ }
+ if !strings.Contains(msg, "accepts at least 1 arg, received 0") {
+ t.Fatalf("stderr = %q, want the minimum-args message", msg)
+ }
+}
+
+func TestRunIssueDeleteSingleByKey(t *testing.T) {
+ var requests []issueDeleteRequest
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ requests = append(requests, issueDeleteRequest{r.Method, r.URL.Path})
+ switch {
+ case r.Method == http.MethodGet && r.URL.Path == "/api/issues/MUL-1":
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ case r.Method == http.MethodDelete && r.URL.Path == "/api/issues/"+issueDeleteTestID1:
+ w.WriteHeader(http.StatusNoContent)
+ default:
+ t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ out, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, []string{"MUL-1"})
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+
+ want := []issueDeleteRequest{
+ {http.MethodGet, "/api/issues/MUL-1"},
+ {http.MethodDelete, "/api/issues/" + issueDeleteTestID1},
+ }
+ if !issueDeleteRequestsEqual(requests, want) {
+ t.Fatalf("requests = %v, want %v", requests, want)
+ }
+
+ // A single delete keeps the sibling delete shape, with the canonical UUID
+ // rather than whatever spelling the caller used.
+ var result struct {
+ ID string `json:"id"`
+ Deleted bool `json:"deleted"`
+ }
+ if err := json.Unmarshal([]byte(out), &result); err != nil {
+ t.Fatalf("decode output %q: %v", out, err)
+ }
+ if result.ID != issueDeleteTestID1 || !result.Deleted {
+ t.Fatalf("output = %q, want {id:%s deleted:true}", out, issueDeleteTestID1)
+ }
+}
+
+// A full UUID is a self-identifying reference elsewhere in the CLI, but the
+// batch route silently drops a UUID that no longer resolves and deletes the
+// rest; delete therefore reads every argument, UUID included, before writing.
+func TestRunIssueDeleteBatchResolvesStaleUUIDBeforeWrite(t *testing.T) {
+ var requests []issueDeleteRequest
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ requests = append(requests, issueDeleteRequest{r.Method, r.URL.Path})
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodGet && r.URL.Path == "/api/issues/MUL-1":
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ case r.Method == http.MethodGet && r.URL.Path == "/api/issues/"+issueDeleteTestID2:
+ w.WriteHeader(http.StatusNotFound)
+ json.NewEncoder(w).Encode(map[string]string{"error": "issue not found"})
+ default:
+ t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ _, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, []string{"MUL-1", issueDeleteTestID2})
+ })
+ if err == nil {
+ t.Fatal("delete succeeded, want the stale UUID to abort the batch")
+ }
+ if !strings.Contains(err.Error(), `resolve issue "`+issueDeleteTestID2+`"`) {
+ t.Fatalf("error = %v, want it to name the stale UUID", err)
+ }
+ for _, req := range requests {
+ if req.method != http.MethodGet {
+ t.Fatalf("requests = %v, want reads only before an unresolved id aborts", requests)
+ }
+ }
+}
+
+// The batch route accepts UUIDs only and skips anything it cannot parse, so
+// the CLI must resolve keys first. A key sent unchanged would produce
+// {"deleted": 0} instead of failing.
+func TestRunIssueDeleteBatchResolvesKeysToUUIDs(t *testing.T) {
+ keys := map[string]string{"MUL-1": issueDeleteTestID1, "MUL-2": issueDeleteTestID2}
+ var batchIDs []string
+ var deleteCalls int
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/api/issues/"):
+ key := strings.TrimPrefix(r.URL.Path, "/api/issues/")
+ id, ok := keys[key]
+ if !ok {
+ http.NotFound(w, r)
+ return
+ }
+ json.NewEncoder(w).Encode(map[string]any{"id": id, "identifier": key})
+ case r.Method == http.MethodPost && r.URL.Path == "/api/issues/batch-delete":
+ var body struct {
+ IssueIDs []string `json:"issue_ids"`
+ }
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode batch body: %v", err)
+ }
+ batchIDs = body.IssueIDs
+ json.NewEncoder(w).Encode(map[string]any{"deleted": len(body.IssueIDs)})
+ case r.Method == http.MethodDelete:
+ deleteCalls++
+ http.Error(w, "unexpected single delete", http.StatusInternalServerError)
+ default:
+ t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ out, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, []string{"MUL-1", "MUL-2"})
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+
+ if deleteCalls != 0 {
+ t.Fatalf("single delete calls = %d, want 0", deleteCalls)
+ }
+ if len(batchIDs) != 2 || batchIDs[0] != issueDeleteTestID1 || batchIDs[1] != issueDeleteTestID2 {
+ t.Fatalf("batch issue_ids = %v, want [%s %s]", batchIDs, issueDeleteTestID1, issueDeleteTestID2)
+ }
+
+ var result struct {
+ IDs []string `json:"ids"`
+ Deleted bool `json:"deleted"`
+ }
+ if err := json.Unmarshal([]byte(out), &result); err != nil {
+ t.Fatalf("decode output %q: %v", out, err)
+ }
+ if !result.Deleted || len(result.IDs) != 2 || result.IDs[0] != issueDeleteTestID1 || result.IDs[1] != issueDeleteTestID2 {
+ t.Fatalf("output = %q, want canonical UUIDs %v and deleted:true", out, []string{issueDeleteTestID1, issueDeleteTestID2})
+ }
+}
+
+func TestRunIssueDeleteDuplicateKeysDeleteOnce(t *testing.T) {
+ deleteCalls := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.Method {
+ case http.MethodGet:
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ case http.MethodDelete:
+ deleteCalls++
+ w.WriteHeader(http.StatusNoContent)
+ default:
+ t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ _, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, []string{"MUL-1", "MUL-1"})
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+ if deleteCalls != 1 {
+ t.Fatalf("delete calls = %d, want 1", deleteCalls)
+ }
+}
+
+func TestRunIssueDeletePromptAborts(t *testing.T) {
+ requests := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ requests++
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, false, "table")
+
+ stderr := captureStderr(t)
+ out, err := captureStdout(t, func() error {
+ var runErr error
+ pipeStdin(t, "n\n", func() {
+ runErr = runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1})
+ })
+ return runErr
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+ if requests != 1 {
+ t.Fatalf("requests = %d, want only the resolving read", requests)
+ }
+ if strings.TrimSpace(out) != "" {
+ t.Fatalf("stdout = %q, want empty", out)
+ }
+ msg := stderr.read()
+ if !strings.Contains(msg, "Delete issue MUL-1") {
+ t.Fatalf("prompt = %q, want it to name the issue", msg)
+ }
+ if !strings.Contains(msg, "cannot be undone") {
+ t.Fatalf("prompt = %q, want it to state permanence", msg)
+ }
+ if !strings.Contains(msg, "Aborted.") {
+ t.Fatalf("stderr = %q, want Aborted.", msg)
+ }
+}
+
+func TestRunIssueDeletePromptAcceptsYes(t *testing.T) {
+ deleteCalls := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodGet {
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ return
+ }
+ if r.Method != http.MethodDelete {
+ t.Errorf("method = %s, want DELETE", r.Method)
+ }
+ deleteCalls++
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, false, "table")
+
+ _, err := captureStdout(t, func() error {
+ var runErr error
+ pipeStdin(t, "y\n", func() {
+ runErr = runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1})
+ })
+ return runErr
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+ if deleteCalls != 1 {
+ t.Fatalf("delete calls = %d, want 1", deleteCalls)
+ }
+}
+
+func TestRunIssueDeletePromptWaitDoesNotConsumeDeleteTimeout(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ args []string
+ }{
+ {"single", []string{"MUL-1"}},
+ {"batch", []string{"MUL-1", "MUL-2"}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Setenv("MULTICA_HTTP_TIMEOUT", "1s")
+ resolved := make(chan struct{})
+ var reads, writes atomic.Int32
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch r.Method {
+ case http.MethodGet:
+ id := issueDeleteTestID1
+ if r.URL.Path == "/api/issues/MUL-2" {
+ id = issueDeleteTestID2
+ }
+ json.NewEncoder(w).Encode(map[string]any{"id": id, "identifier": strings.TrimPrefix(r.URL.Path, "/api/issues/")})
+ if int(reads.Add(1)) == len(tc.args) {
+ close(resolved)
+ }
+ case http.MethodDelete:
+ writes.Add(1)
+ w.WriteHeader(http.StatusNoContent)
+ case http.MethodPost:
+ writes.Add(1)
+ json.NewEncoder(w).Encode(map[string]any{"deleted": len(tc.args)})
+ default:
+ t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, false, "json")
+
+ r, w, err := os.Pipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer r.Close()
+ defer w.Close()
+ originalStdin := os.Stdin
+ os.Stdin = r
+ defer func() { os.Stdin = originalStdin }()
+ answered := make(chan struct{})
+ go func() {
+ defer close(answered)
+ <-resolved
+ // Let the entire resolution budget expire while the user reads the prompt.
+ time.Sleep(cli.APITimeout() + 100*time.Millisecond)
+ if _, err := w.WriteString("y\n"); err != nil {
+ t.Errorf("confirm deletion: %v", err)
+ }
+ }()
+
+ stderr := captureStderr(t)
+ out, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, tc.args)
+ })
+ <-answered
+ stderr.read()
+ if err != nil {
+ t.Fatalf("delete after delayed confirmation: %v", err)
+ }
+ if got := writes.Load(); got != 1 {
+ t.Fatalf("delete requests = %d, want 1", got)
+ }
+ var result struct {
+ Deleted bool `json:"deleted"`
+ }
+ if err := json.Unmarshal([]byte(out), &result); err != nil || !result.Deleted {
+ t.Fatalf("output = %q, error = %v; want deleted:true", out, err)
+ }
+ })
+ }
+}
+
+// A batch prompt has to name the count and the issues, not just "these".
+func TestIssueDeleteConfirmationNamesCountAndConsequences(t *testing.T) {
+ got := issueDeleteConfirmation([]resolvedID{
+ {ID: issueDeleteTestID1, Display: "MUL-1"},
+ {ID: issueDeleteTestID2, Display: "MUL-2"},
+ })
+ for _, want := range []string{"2 issues", "MUL-1", "MUL-2", "comments and attachments", "cannot be undone"} {
+ if !strings.Contains(got, want) {
+ t.Fatalf("confirmation = %q, want it to contain %q", got, want)
+ }
+ }
+}
+
+// A non-interactive run has no answer to give, so it must fail instead of
+// printing "Aborted." and exiting 0. A deliberate "n" stays a quiet decline.
+func TestRunIssueDeleteNonInteractiveFails(t *testing.T) {
+ requests := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ requests++
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, false, "table")
+
+ stderr := captureStderr(t)
+ _, err := captureStdout(t, func() error {
+ var runErr error
+ pipeStdin(t, "", func() {
+ runErr = runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1})
+ })
+ return runErr
+ })
+ msg := stderr.read()
+ if err == nil {
+ t.Fatal("delete with closed stdin succeeded, want a non-zero failure")
+ }
+ if !strings.Contains(err.Error(), "--yes") {
+ t.Fatalf("error = %v, want it to point at --yes", err)
+ }
+ if strings.Contains(msg, "Aborted.") {
+ t.Fatalf("stderr = %q, want a failure before any Aborted. path", msg)
+ }
+ if requests != 1 {
+ t.Fatalf("requests = %d, want only the resolving read", requests)
+ }
+}
+
+// Resolution happens before the prompt and before the batch request, so one
+// unknown key aborts the whole batch instead of deleting the resolvable ones.
+func TestRunIssueDeleteBatchAbortsOnUnknownKey(t *testing.T) {
+ var wrote bool
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodGet && r.URL.Path == "/api/issues/MUL-1":
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ case r.Method == http.MethodGet:
+ w.WriteHeader(http.StatusNotFound)
+ json.NewEncoder(w).Encode(map[string]string{"error": "issue not found"})
+ default:
+ wrote = true
+ t.Errorf("unexpected write %s %s", r.Method, r.URL.Path)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ err := runIssueDelete(issueDeleteCmd, []string{"MUL-1", "MUL-404"})
+ if err == nil || !strings.Contains(err.Error(), `resolve issue "MUL-404"`) {
+ t.Fatalf("error = %v, want a resolve failure for MUL-404", err)
+ }
+ if wrote {
+ t.Fatal("wrote to the server, want no writes after a failed resolution")
+ }
+}
+
+// The batch route reports only how many ids it deleted, so a partial batch is
+// an error with JSON output that says so instead of a silent success or a
+// success-shaped document.
+func TestRunIssueDeletePartialBatchReportsJSON(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodGet:
+ id := strings.TrimPrefix(r.URL.Path, "/api/issues/")
+ json.NewEncoder(w).Encode(map[string]any{"id": id, "identifier": "MUL-" + id[:1]})
+ case r.Method == http.MethodPost && r.URL.Path == "/api/issues/batch-delete":
+ json.NewEncoder(w).Encode(map[string]any{"deleted": 1})
+ default:
+ t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
+ }
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ out, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1, issueDeleteTestID2})
+ })
+ if err == nil || !strings.Contains(err.Error(), "deleted 1 of 2 issues") {
+ t.Fatalf("error = %v, want a partial-batch error", err)
+ }
+
+ var result struct {
+ IDs []string `json:"ids"`
+ Deleted bool `json:"deleted"`
+ DeletedCount int `json:"deleted_count"`
+ }
+ if err := json.Unmarshal([]byte(out), &result); err != nil {
+ t.Fatalf("decode output %q: %v", out, err)
+ }
+ if result.Deleted {
+ t.Fatalf("output = %q, want deleted:false for a partial batch", out)
+ }
+ if result.DeletedCount != 1 {
+ t.Fatalf("deleted_count = %d, want 1", result.DeletedCount)
+ }
+ if len(result.IDs) != 2 || result.IDs[0] != issueDeleteTestID1 || result.IDs[1] != issueDeleteTestID2 {
+ t.Fatalf("ids = %v, want the requested UUIDs", result.IDs)
+ }
+}
+
+func TestRunIssueDeleteTableOutput(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodGet {
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "table")
+
+ out, err := captureStdout(t, func() error {
+ return runIssueDelete(issueDeleteCmd, []string{"MUL-1"})
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+ if out != "Deleted issue MUL-1\n" {
+ t.Fatalf("stdout = %q, want the human-facing line", out)
+ }
+}
+
+// With --output json the prompt still goes to stderr, so stdout stays one
+// parseable JSON document for scripts.
+func TestRunIssueDeletePromptKeepsStdoutJSON(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodGet {
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, false, "json")
+
+ stderr := captureStderr(t)
+ out, err := captureStdout(t, func() error {
+ var runErr error
+ pipeStdin(t, "y\n", func() {
+ runErr = runIssueDelete(issueDeleteCmd, []string{"MUL-1"})
+ })
+ return runErr
+ })
+ if err != nil {
+ t.Fatalf("run delete: %v", err)
+ }
+ var result struct {
+ ID string `json:"id"`
+ Deleted bool `json:"deleted"`
+ }
+ if err := json.Unmarshal([]byte(out), &result); err != nil {
+ t.Fatalf("stdout %q is not pure JSON: %v", out, err)
+ }
+ if result.ID != issueDeleteTestID1 || !result.Deleted {
+ t.Fatalf("output = %q, want the deleted issue", out)
+ }
+ if !strings.Contains(stderr.read(), "Delete issue MUL-1") {
+ t.Fatal("prompt missing from stderr")
+ }
+}
+
+func TestRunIssueDeletePermissionErrorIsSurfaced(t *testing.T) {
+ const serverMessage = "issue deletion is restricted to its creator, the creating agent's owner, and workspace owners or admins"
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodGet {
+ json.NewEncoder(w).Encode(map[string]any{"id": issueDeleteTestID1, "identifier": "MUL-1"})
+ return
+ }
+ w.WriteHeader(http.StatusForbidden)
+ json.NewEncoder(w).Encode(map[string]string{"error": serverMessage})
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "table")
+
+ err := runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1})
+ if err == nil {
+ t.Fatal("delete succeeded, want the server's refusal")
+ }
+ // FormatError is what the user sees; the 403 copy must be the server's,
+ // not the generic kind message.
+ if got := cli.FormatError(err, false); !strings.Contains(got, serverMessage) {
+ t.Fatalf("formatted error = %q, want the server's %q", got, serverMessage)
+ }
+}
+
+func TestRunIssueDeleteBatchPermissionErrorIsSurfaced(t *testing.T) {
+ const serverMessage = "batch deletion is restricted to workspace owners or admins"
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodGet {
+ id := strings.TrimPrefix(r.URL.Path, "/api/issues/")
+ json.NewEncoder(w).Encode(map[string]any{"id": id, "identifier": "MUL-1"})
+ return
+ }
+ w.WriteHeader(http.StatusForbidden)
+ json.NewEncoder(w).Encode(map[string]string{"error": serverMessage})
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "json")
+
+ err := runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1, issueDeleteTestID2})
+ if err == nil {
+ t.Fatal("delete succeeded, want the server's refusal")
+ }
+ if got := cli.FormatError(err, false); !strings.Contains(got, serverMessage) {
+ t.Fatalf("formatted error = %q, want the server's %q", got, serverMessage)
+ }
+}
+
+func TestRunIssueDeleteNotFoundTextIsSurfaced(t *testing.T) {
+ const serverMessage = "issue not found"
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusNotFound)
+ json.NewEncoder(w).Encode(map[string]string{"error": serverMessage})
+ }))
+ defer srv.Close()
+ setIssueDeleteTestEnv(t, srv.URL)
+ setIssueDeleteFlags(t, true, "table")
+
+ err := runIssueDelete(issueDeleteCmd, []string{issueDeleteTestID1})
+ if err == nil {
+ t.Fatal("delete succeeded, want a 404")
+ }
+ if got := cli.FormatError(err, false); !strings.Contains(got, serverMessage) {
+ t.Fatalf("formatted error = %q, want the server's %q", got, serverMessage)
+ }
+}
+
+func issueDeleteRequestsEqual(got, want []issueDeleteRequest) bool {
+ if len(got) != len(want) {
+ return false
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ return false
+ }
+ }
+ return true
+}
diff --git a/server/cmd/multica/help.go b/server/cmd/multica/help.go
index 326a90eb72..5463018af2 100644
--- a/server/cmd/multica/help.go
+++ b/server/cmd/multica/help.go
@@ -35,6 +35,23 @@ func exactArgs(n int) cobra.PositionalArgs {
}
}
+// minimumArgs returns a cobra.PositionalArgs that requires at least n args
+// and prints help on failure, so users see usage context with the error.
+func minimumArgs(n int) cobra.PositionalArgs {
+ return func(cmd *cobra.Command, args []string) error {
+ if len(args) < n {
+ if n == 1 {
+ fmt.Fprintf(cmd.ErrOrStderr(), "Error: accepts at least 1 arg, received %d\n\n", len(args))
+ } else {
+ fmt.Fprintf(cmd.ErrOrStderr(), "Error: accepts at least %d args, received %d\n\n", n, len(args))
+ }
+ cmd.Help()
+ return errSilent
+ }
+ return nil
+ }
+}
+
// initHelp configures the root command to use gh-style help output.
func initHelp(root *cobra.Command) {
root.SetHelpTemplate(rootHelpTemplate)