diff --git a/internal/cli/pr.go b/internal/cli/pr.go index 31c7da7..d991441 100644 --- a/internal/cli/pr.go +++ b/internal/cli/pr.go @@ -45,6 +45,7 @@ func prViewCmd() *cobra.Command { var ( flagComments bool flagWeb bool + flagJSON string ) cmd := &cobra.Command{ @@ -52,6 +53,18 @@ func prViewCmd() *cobra.Command { Short: "View a pull request", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + if cmd.Flags().Changed("json") { + var hints []string + hints = append(hints, "forge --output json pr view ") + if strings.Contains(flagJSON, "comments") { + hints = append(hints, "forge pr view --comments ") + } + if strings.Contains(flagJSON, "reviews") { + hints = append(hints, "forge pr review list ") + } + return fmt.Errorf("--json is not supported; use --output json instead (field selection is not supported)\n\nTry: %s", strings.Join(hints, "\n ")) + } + number, err := strconv.Atoi(args[0]) if err != nil { return fmt.Errorf("invalid PR number: %s", args[0]) @@ -96,6 +109,9 @@ func prViewCmd() *cobra.Command { cmd.Flags().BoolVarP(&flagComments, "comments", "c", false, "Show comments") cmd.Flags().BoolVarP(&flagWeb, "web", "w", false, "Open in browser") + cmd.Flags().StringVar(&flagJSON, "json", "", "Not supported; use --output json") + cmd.Flags().Lookup("json").NoOptDefVal = " " + _ = cmd.Flags().MarkHidden("json") return cmd } diff --git a/internal/cli/pr_test.go b/internal/cli/pr_test.go index 4852128..4ad8902 100644 --- a/internal/cli/pr_test.go +++ b/internal/cli/pr_test.go @@ -142,3 +142,49 @@ func TestPRDiffRequiresArg(t *testing.T) { t.Fatal("expected error for missing argument") } } + +func TestPRViewJSONFlagNotSupported(t *testing.T) { + tests := []struct { + name string + args []string + want string + }{ + { + name: "generic field", + args: []string{"pr", "view", "--json=title", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view ", + }, + { + name: "comments field", + args: []string{"pr", "view", "--json=comments", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view \n forge pr view --comments ", + }, + { + name: "reviews field", + args: []string{"pr", "view", "--json=reviews", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view \n forge pr review list ", + }, + { + name: "both fields", + args: []string{"pr", "view", "--json=reviews,comments", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view \n forge pr view --comments \n forge pr review list ", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var buf bytes.Buffer + rootCmd.SetOut(&buf) + rootCmd.SetErr(&buf) + rootCmd.SetArgs(tt.args) + + err := rootCmd.Execute() + if err == nil { + t.Fatal("expected error for --json flag") + } + if err.Error() != tt.want { + t.Errorf("unexpected error:\ngot: %s\nwant: %s", err.Error(), tt.want) + } + }) + } +}