github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/issue/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/MakeNowJust/heredoc"
    10  	"github.com/andrewhsu/cli/v2/api"
    11  	"github.com/andrewhsu/cli/v2/internal/config"
    12  	"github.com/andrewhsu/cli/v2/internal/ghrepo"
    13  	issueShared "github.com/andrewhsu/cli/v2/pkg/cmd/issue/shared"
    14  	"github.com/andrewhsu/cli/v2/pkg/cmd/pr/shared"
    15  	prShared "github.com/andrewhsu/cli/v2/pkg/cmd/pr/shared"
    16  	"github.com/andrewhsu/cli/v2/pkg/cmdutil"
    17  	"github.com/andrewhsu/cli/v2/pkg/iostreams"
    18  	"github.com/andrewhsu/cli/v2/utils"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  type browser interface {
    23  	Browse(string) error
    24  }
    25  
    26  type ListOptions struct {
    27  	HttpClient func() (*http.Client, error)
    28  	Config     func() (config.Config, error)
    29  	IO         *iostreams.IOStreams
    30  	BaseRepo   func() (ghrepo.Interface, error)
    31  	Browser    browser
    32  
    33  	WebMode  bool
    34  	Exporter cmdutil.Exporter
    35  
    36  	Assignee     string
    37  	Labels       []string
    38  	State        string
    39  	LimitResults int
    40  	Author       string
    41  	Mention      string
    42  	Milestone    string
    43  	Search       string
    44  }
    45  
    46  func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
    47  	opts := &ListOptions{
    48  		IO:         f.IOStreams,
    49  		HttpClient: f.HttpClient,
    50  		Config:     f.Config,
    51  		Browser:    f.Browser,
    52  	}
    53  
    54  	cmd := &cobra.Command{
    55  		Use:   "list",
    56  		Short: "List and filter issues in this repository",
    57  		Example: heredoc.Doc(`
    58  			$ gh issue list -l "bug" -l "help wanted"
    59  			$ gh issue list -A monalisa
    60  			$ gh issue list -a "@me"
    61  			$ gh issue list --web
    62  			$ gh issue list --milestone "The big 1.0"
    63  			$ gh issue list --search "error no:assignee sort:created-asc"
    64  		`),
    65  		Args: cmdutil.NoArgsQuoteReminder,
    66  		RunE: func(cmd *cobra.Command, args []string) error {
    67  			// support `-R, --repo` override
    68  			opts.BaseRepo = f.BaseRepo
    69  
    70  			if opts.LimitResults < 1 {
    71  				return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.LimitResults)}
    72  			}
    73  
    74  			if runF != nil {
    75  				return runF(opts)
    76  			}
    77  			return listRun(opts)
    78  		},
    79  	}
    80  
    81  	cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the issue(s)")
    82  	cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
    83  	cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
    84  	cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|all}")
    85  	_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    86  		return []string{"open", "closed", "all"}, cobra.ShellCompDirectiveNoFileComp
    87  	})
    88  	cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of issues to fetch")
    89  	cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
    90  	cmd.Flags().StringVar(&opts.Mention, "mention", "", "Filter by mention")
    91  	cmd.Flags().StringVarP(&opts.Milestone, "milestone", "m", "", "Filter by milestone `number` or `title`")
    92  	cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search issues with `query`")
    93  	cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields)
    94  
    95  	return cmd
    96  }
    97  
    98  var defaultFields = []string{
    99  	"number",
   100  	"title",
   101  	"url",
   102  	"state",
   103  	"updatedAt",
   104  	"labels",
   105  }
   106  
   107  func listRun(opts *ListOptions) error {
   108  	httpClient, err := opts.HttpClient()
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	baseRepo, err := opts.BaseRepo()
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	issueState := strings.ToLower(opts.State)
   119  	if issueState == "open" && shared.QueryHasStateClause(opts.Search) {
   120  		issueState = ""
   121  	}
   122  
   123  	filterOptions := prShared.FilterOptions{
   124  		Entity:    "issue",
   125  		State:     issueState,
   126  		Assignee:  opts.Assignee,
   127  		Labels:    opts.Labels,
   128  		Author:    opts.Author,
   129  		Mention:   opts.Mention,
   130  		Milestone: opts.Milestone,
   131  		Search:    opts.Search,
   132  		Fields:    defaultFields,
   133  	}
   134  
   135  	isTerminal := opts.IO.IsStdoutTTY()
   136  
   137  	if opts.WebMode {
   138  		issueListURL := ghrepo.GenerateRepoURL(baseRepo, "issues")
   139  		openURL, err := prShared.ListURLWithQuery(issueListURL, filterOptions)
   140  		if err != nil {
   141  			return err
   142  		}
   143  
   144  		if isTerminal {
   145  			fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
   146  		}
   147  		return opts.Browser.Browse(openURL)
   148  	}
   149  
   150  	if opts.Exporter != nil {
   151  		filterOptions.Fields = opts.Exporter.Fields()
   152  	}
   153  
   154  	listResult, err := issueList(httpClient, baseRepo, filterOptions, opts.LimitResults)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	err = opts.IO.StartPager()
   160  	if err != nil {
   161  		return err
   162  	}
   163  	defer opts.IO.StopPager()
   164  
   165  	if opts.Exporter != nil {
   166  		return opts.Exporter.Write(opts.IO, listResult.Issues)
   167  	}
   168  
   169  	if isTerminal {
   170  		title := prShared.ListHeader(ghrepo.FullName(baseRepo), "issue", len(listResult.Issues), listResult.TotalCount, !filterOptions.IsDefault())
   171  		fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title)
   172  	}
   173  
   174  	issueShared.PrintIssues(opts.IO, "", len(listResult.Issues), listResult.Issues)
   175  
   176  	return nil
   177  }
   178  
   179  func issueList(client *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.IssuesAndTotalCount, error) {
   180  	apiClient := api.NewClientFromHTTP(client)
   181  
   182  	if filters.Search != "" || len(filters.Labels) > 0 {
   183  		if milestoneNumber, err := strconv.ParseInt(filters.Milestone, 10, 32); err == nil {
   184  			milestone, err := api.MilestoneByNumber(apiClient, repo, int32(milestoneNumber))
   185  			if err != nil {
   186  				return nil, err
   187  			}
   188  			filters.Milestone = milestone.Title
   189  		}
   190  
   191  		return searchIssues(apiClient, repo, filters, limit)
   192  	}
   193  
   194  	var err error
   195  	meReplacer := shared.NewMeReplacer(apiClient, repo.RepoHost())
   196  	filters.Assignee, err = meReplacer.Replace(filters.Assignee)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  	filters.Author, err = meReplacer.Replace(filters.Author)
   201  	if err != nil {
   202  		return nil, err
   203  	}
   204  	filters.Mention, err = meReplacer.Replace(filters.Mention)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  
   209  	return listIssues(apiClient, repo, filters, limit)
   210  }