github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/ready/ready.go (about)

     1  package ready
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/MakeNowJust/heredoc"
     9  	"github.com/cli/cli/api"
    10  	"github.com/cli/cli/pkg/cmd/pr/shared"
    11  	"github.com/cli/cli/pkg/cmdutil"
    12  	"github.com/cli/cli/pkg/iostreams"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type ReadyOptions struct {
    17  	HttpClient func() (*http.Client, error)
    18  	IO         *iostreams.IOStreams
    19  
    20  	Finder shared.PRFinder
    21  
    22  	SelectorArg string
    23  }
    24  
    25  func NewCmdReady(f *cmdutil.Factory, runF func(*ReadyOptions) error) *cobra.Command {
    26  	opts := &ReadyOptions{
    27  		IO:         f.IOStreams,
    28  		HttpClient: f.HttpClient,
    29  	}
    30  
    31  	cmd := &cobra.Command{
    32  		Use:   "ready [<number> | <url> | <branch>]",
    33  		Short: "Mark a pull request as ready for review",
    34  		Long: heredoc.Doc(`
    35  			Mark a pull request as ready for review
    36  
    37  			Without an argument, the pull request that belongs to the current branch
    38  			is displayed.
    39  		`),
    40  		Args: cobra.MaximumNArgs(1),
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			opts.Finder = shared.NewFinder(f)
    43  
    44  			if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
    45  				return &cmdutil.FlagError{Err: errors.New("argument required when using the --repo flag")}
    46  			}
    47  
    48  			if len(args) > 0 {
    49  				opts.SelectorArg = args[0]
    50  			}
    51  
    52  			if runF != nil {
    53  				return runF(opts)
    54  			}
    55  			return readyRun(opts)
    56  		},
    57  	}
    58  
    59  	return cmd
    60  }
    61  
    62  func readyRun(opts *ReadyOptions) error {
    63  	cs := opts.IO.ColorScheme()
    64  
    65  	findOptions := shared.FindOptions{
    66  		Selector: opts.SelectorArg,
    67  		Fields:   []string{"id", "number", "state", "isDraft"},
    68  	}
    69  	pr, baseRepo, err := opts.Finder.Find(findOptions)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	if !pr.IsOpen() {
    75  		fmt.Fprintf(opts.IO.ErrOut, "%s Pull request #%d is closed. Only draft pull requests can be marked as \"ready for review\"\n", cs.FailureIcon(), pr.Number)
    76  		return cmdutil.SilentError
    77  	} else if !pr.IsDraft {
    78  		fmt.Fprintf(opts.IO.ErrOut, "%s Pull request #%d is already \"ready for review\"\n", cs.WarningIcon(), pr.Number)
    79  		return nil
    80  	}
    81  
    82  	httpClient, err := opts.HttpClient()
    83  	if err != nil {
    84  		return err
    85  	}
    86  	apiClient := api.NewClientFromHTTP(httpClient)
    87  
    88  	err = api.PullRequestReady(apiClient, baseRepo, pr)
    89  	if err != nil {
    90  		return fmt.Errorf("API call failed: %w", err)
    91  	}
    92  
    93  	fmt.Fprintf(opts.IO.ErrOut, "%s Pull request #%d is marked as \"ready for review\"\n", cs.SuccessIconWithColor(cs.Green), pr.Number)
    94  
    95  	return nil
    96  }