package comment import ( "fmt" "github.com/spf13/cobra" "tangled.org/eric.wien/tng-cli.git/v2/api" gitpkg "tangled.org/eric.wien/tng-cli.git/v2/git" "tangled.org/eric.wien/tng-cli.git/v2/pkg/cmdutil" "tangled.org/eric.wien/tng-cli.git/v2/pkg/iostreams" ) type Options struct { IO *iostreams.IOStreams ApiClient func() (*api.Client, error) BaseRepo func() (*gitpkg.TangledRepo, error) Rkey string Body string } func NewCmdComment(f *cmdutil.Factory) *cobra.Command { opts := &Options{ IO: f.IOStreams, ApiClient: f.ApiClient, BaseRepo: f.BaseRepo, } cmd := &cobra.Command{ Use: "comment ", Short: "Comment on a pull request", Example: ` tng pr comment 3jxn7q7bklr2r --body "Looks good!"`, Args: cmdutil.ExactArgs(1, "pull request rkey is required"), RunE: func(cmd *cobra.Command, args []string) error { opts.Rkey = args[0] return commentRun(cmd, opts) }, } cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Comment body (required)") cmd.MarkFlagRequired("body") return cmd } func commentRun(cmd *cobra.Command, opts *Options) error { cs := iostreams.NewColorScheme(opts.IO.ColorEnabled()) client, err := opts.ApiClient() if err != nil { return err } resolved, err := cmdutil.ResolveRepo(cmd.Context(), client, opts.BaseRepo) if err != nil { return err } pullURI := fmt.Sprintf("at://%s/%s/%s", resolved.RepoDID, api.PullRecordNSID, opts.Rkey) if err := client.CreatePullComment(cmd.Context(), pullURI, opts.Body); err != nil { return fmt.Errorf("failed to add comment: %w", err) } fmt.Fprintf(opts.IO.ErrOut, "%s Added comment to pull request %s\n", cs.SuccessIcon(), opts.Rkey) return nil }