Skip to content

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

Go

refactor(cli): make `issue reorder` require exactly one target flag via a cobra flag group [MUL-4222] (#5095)

Firer6be496c65276
diff --git a/server/cmd/multica/cmd_issue.go b/server/cmd/multica/cmd_issue.go
index d8e626a330..69cb77fd57 100644
--- a/server/cmd/multica/cmd_issue.go
+++ b/server/cmd/multica/cmd_issue.go
@@ -419,11 +419,7 @@ func init() {
issueStatusCmd.Flags().String("output", "table", "Output format: table or json")
// issue reorder
- issueReorderCmd.Flags().String("before", "", "Place the issue directly above this issue (same column)")
- issueReorderCmd.Flags().String("after", "", "Place the issue directly below this issue (same column)")
- issueReorderCmd.Flags().Bool("top", false, "Move the issue to the top of its status column")
- issueReorderCmd.Flags().Bool("bottom", false, "Move the issue to the bottom of its status column")
- issueReorderCmd.Flags().String("output", "json", "Output format: table or json")
+ registerIssueReorderFlags(issueReorderCmd)
// issue assign
issueAssignCmd.Flags().String("to", "", "Assignee name (member, agent, or squad; fuzzy match)")
@@ -1332,6 +1328,24 @@ func runIssueStatus(cmd *cobra.Command, args []string) error {
// Reorder command
// ---------------------------------------------------------------------------
+// registerIssueReorderFlags wires the reorder command's flags and declares its
+// target selector as a cobra flag group: mutually exclusive so at most one of
+// --before/--after/--top/--bottom is accepted, and one-required so at least one
+// must be given. Declaring the rule (instead of counting the flags by hand in
+// runIssueReorder) lets cobra reject zero-target and multi-target invocations
+// before RunE with a canonical message, and makes shell completion group-aware
+// — it hides the sibling target flags once one of them is set. It is shared with
+// the command's tests so they exercise the exact flag-group wiring that ships.
+func registerIssueReorderFlags(cmd *cobra.Command) {
+ cmd.Flags().String("before", "", "Place the issue directly above this issue (same column)")
+ cmd.Flags().String("after", "", "Place the issue directly below this issue (same column)")
+ cmd.Flags().Bool("top", false, "Move the issue to the top of its status column")
+ cmd.Flags().Bool("bottom", false, "Move the issue to the bottom of its status column")
+ cmd.Flags().String("output", "json", "Output format: table or json")
+ cmd.MarkFlagsMutuallyExclusive("before", "after", "top", "bottom")
+ cmd.MarkFlagsOneRequired("before", "after", "top", "bottom")
+}
+
// runIssueReorder repositions an issue inside its current status column. The
// new position is computed client-side by computeReorderPosition, which mirrors
// the board/list drag-and-drop math (computePosition in
@@ -1345,14 +1359,26 @@ func runIssueReorder(cmd *cobra.Command, args []string) error {
top, _ := cmd.Flags().GetBool("top")
bottom, _ := cmd.Flags().GetBool("bottom")
- modes := 0
- for _, set := range []bool{before != "", after != "", top, bottom} {
- if set {
- modes++
- }
- }
- if modes != 1 {
- return fmt.Errorf("exactly one of --before, --after, --top, or --bottom is required")
+ // "Exactly one of --before/--after/--top/--bottom" is enforced declaratively
+ // by the command's mutually-exclusive, one-required flag group (see
+ // registerIssueReorderFlags), so cobra rejects zero-target and multi-target
+ // invocations before RunE runs. Cobra keys off flag *presence* (Changed),
+ // not value, so guard the cases it cannot see: a --before/--after passed
+ // empty (e.g. an unset shell variable), or a --top/--bottom explicitly set
+ // to false (e.g. `--top=false` from a generated command). Each counts as
+ // "set" for the group yet selects no real target, and would otherwise fall
+ // through to a confusing "not found in column" error.
+ if cmd.Flags().Changed("before") && before == "" {
+ return fmt.Errorf("--before requires an issue ID or key")
+ }
+ if cmd.Flags().Changed("after") && after == "" {
+ return fmt.Errorf("--after requires an issue ID or key")
+ }
+ if cmd.Flags().Changed("top") && !top {
+ return fmt.Errorf("--top cannot be set to false; pass it on its own to move the issue to the top of its column")
+ }
+ if cmd.Flags().Changed("bottom") && !bottom {
+ return fmt.Errorf("--bottom cannot be set to false; pass it on its own to move the issue to the bottom of its column")
}
client, err := newAPIClient(cmd)
diff --git a/server/cmd/multica/cmd_issue_test.go b/server/cmd/multica/cmd_issue_test.go
index 17aa174951..a9cf087acd 100644
--- a/server/cmd/multica/cmd_issue_test.go
+++ b/server/cmd/multica/cmd_issue_test.go
@@ -2518,11 +2518,7 @@ func newIssueListTestCmd() *cobra.Command {
func newIssueReorderTestCmd() *cobra.Command {
cmd := &cobra.Command{Use: "reorder"}
- cmd.Flags().String("before", "", "")
- cmd.Flags().String("after", "", "")
- cmd.Flags().Bool("top", false, "")
- cmd.Flags().Bool("bottom", false, "")
- cmd.Flags().String("output", "json", "")
+ registerIssueReorderFlags(cmd)
return cmd
}
@@ -2677,23 +2673,91 @@ func TestComputeReorderPosition(t *testing.T) {
}
}
-func TestRunIssueReorderRejectsModeCount(t *testing.T) {
- t.Run("zero modes", func(t *testing.T) {
+// TestIssueReorderTargetFlagGroup drives the command through Execute so the real
+// cobra flag group (registerIssueReorderFlags) validates the "exactly one
+// target" rule — zero targets, two bool targets, and --before+--after are all
+// rejected before RunE. A stub RunE that errors proves validation happens first.
+// The assertions match on cobra's canonical flag-group error strings, so they
+// are coupled to cobra's wording (pinned at v1.10.2 in server/go.mod).
+func TestIssueReorderTargetFlagGroup(t *testing.T) {
+ newCmd := func() *cobra.Command {
cmd := newIssueReorderTestCmd()
- if err := runIssueReorder(cmd, []string{"MUL-1"}); err == nil {
- t.Fatal("expected error when no mode flag is set")
+ cmd.Args = exactArgs(1)
+ cmd.RunE = func(*cobra.Command, []string) error {
+ return fmt.Errorf("RunE ran despite an invalid target flag group")
+ }
+ cmd.SilenceUsage = true
+ cmd.SilenceErrors = true
+ return cmd
+ }
+
+ t.Run("no target flag", func(t *testing.T) {
+ cmd := newCmd()
+ cmd.SetArgs([]string{"MUL-1"})
+ err := cmd.Execute()
+ if err == nil {
+ t.Fatal("expected an error when no target flag is set")
+ }
+ if !strings.Contains(err.Error(), "at least one of the flags in the group") {
+ t.Fatalf("want cobra one-required error, got: %v", err)
}
})
- t.Run("two modes", func(t *testing.T) {
- cmd := newIssueReorderTestCmd()
- _ = cmd.Flags().Set("top", "true")
- _ = cmd.Flags().Set("bottom", "true")
- if err := runIssueReorder(cmd, []string{"MUL-1"}); err == nil {
- t.Fatal("expected error when two mode flags are set")
+
+ t.Run("two bool targets", func(t *testing.T) {
+ cmd := newCmd()
+ cmd.SetArgs([]string{"MUL-1", "--top", "--bottom"})
+ err := cmd.Execute()
+ if err == nil {
+ t.Fatal("expected an error when --top and --bottom are combined")
+ }
+ if !strings.Contains(err.Error(), "none of the others can be") {
+ t.Fatalf("want cobra mutually-exclusive error, got: %v", err)
+ }
+ })
+
+ t.Run("before and after together", func(t *testing.T) {
+ cmd := newCmd()
+ cmd.SetArgs([]string{"MUL-1", "--before", "MUL-2", "--after", "MUL-3"})
+ err := cmd.Execute()
+ if err == nil {
+ t.Fatal("expected an error when --before and --after are combined")
+ }
+ if !strings.Contains(err.Error(), "none of the others can be") {
+ t.Fatalf("want cobra mutually-exclusive error, got: %v", err)
}
})
}
+// TestRunIssueReorderRejectsNoOpTargetValues covers the cases the flag group
+// cannot: cobra keys off flag presence (Changed), not value, so an
+// explicitly-passed but empty --before/--after or an explicit --top=false /
+// --bottom=false satisfies the group yet selects no real target. runIssueReorder
+// rejects each up front rather than falling through to a confusing downstream
+// error.
+func TestRunIssueReorderRejectsNoOpTargetValues(t *testing.T) {
+ cases := []struct{ flag, value string }{
+ {"before", ""},
+ {"after", ""},
+ {"top", "false"},
+ {"bottom", "false"},
+ }
+ for _, tc := range cases {
+ t.Run(tc.flag, func(t *testing.T) {
+ cmd := newIssueReorderTestCmd()
+ if err := cmd.Flags().Set(tc.flag, tc.value); err != nil {
+ t.Fatalf("set --%s=%q: %v", tc.flag, tc.value, err)
+ }
+ err := runIssueReorder(cmd, []string{"MUL-1"})
+ if err == nil {
+ t.Fatalf("expected an error when --%s is passed %q", tc.flag, tc.value)
+ }
+ if !strings.Contains(err.Error(), "--"+tc.flag) {
+ t.Fatalf("want error naming --%s, got: %v", tc.flag, err)
+ }
+ })
+ }
+}
+
// reorderTestServer serves a fixed three-issue "todo" column (positions
// 1/9/20) plus single-issue lookups by key and id, capturing the position of
// any PUT so a test can assert where the issue landed.