github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/status/status.go (about)

     1  package status
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/ungtb10d/cli/v2/api"
     9  	"github.com/ungtb10d/cli/v2/internal/config"
    10  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    11  	issueShared "github.com/ungtb10d/cli/v2/pkg/cmd/issue/shared"
    12  	prShared "github.com/ungtb10d/cli/v2/pkg/cmd/pr/shared"
    13  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    14  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type StatusOptions struct {
    19  	HttpClient func() (*http.Client, error)
    20  	Config     func() (config.Config, error)
    21  	IO         *iostreams.IOStreams
    22  	BaseRepo   func() (ghrepo.Interface, error)
    23  
    24  	Exporter cmdutil.Exporter
    25  }
    26  
    27  func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command {
    28  	opts := &StatusOptions{
    29  		IO:         f.IOStreams,
    30  		HttpClient: f.HttpClient,
    31  		Config:     f.Config,
    32  	}
    33  
    34  	cmd := &cobra.Command{
    35  		Use:   "status",
    36  		Short: "Show status of relevant issues",
    37  		Args:  cmdutil.NoArgsQuoteReminder,
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			// support `-R, --repo` override
    40  			opts.BaseRepo = f.BaseRepo
    41  
    42  			if runF != nil {
    43  				return runF(opts)
    44  			}
    45  			return statusRun(opts)
    46  		},
    47  	}
    48  
    49  	cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields)
    50  
    51  	return cmd
    52  }
    53  
    54  var defaultFields = []string{
    55  	"number",
    56  	"title",
    57  	"url",
    58  	"state",
    59  	"updatedAt",
    60  	"labels",
    61  }
    62  
    63  func statusRun(opts *StatusOptions) error {
    64  	httpClient, err := opts.HttpClient()
    65  	if err != nil {
    66  		return err
    67  	}
    68  	apiClient := api.NewClientFromHTTP(httpClient)
    69  
    70  	baseRepo, err := opts.BaseRepo()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	currentUser, err := api.CurrentLoginName(apiClient, baseRepo.RepoHost())
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	options := api.IssueStatusOptions{
    81  		Username: currentUser,
    82  		Fields:   defaultFields,
    83  	}
    84  	if opts.Exporter != nil {
    85  		options.Fields = opts.Exporter.Fields()
    86  	}
    87  	issuePayload, err := api.IssueStatus(apiClient, baseRepo, options)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	err = opts.IO.StartPager()
    93  	if err != nil {
    94  		fmt.Fprintf(opts.IO.ErrOut, "error starting pager: %v\n", err)
    95  	}
    96  	defer opts.IO.StopPager()
    97  
    98  	if opts.Exporter != nil {
    99  		data := map[string]interface{}{
   100  			"createdBy": issuePayload.Authored.Issues,
   101  			"assigned":  issuePayload.Assigned.Issues,
   102  			"mentioned": issuePayload.Mentioned.Issues,
   103  		}
   104  		return opts.Exporter.Write(opts.IO, data)
   105  	}
   106  
   107  	out := opts.IO.Out
   108  
   109  	fmt.Fprintln(out, "")
   110  	fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo))
   111  	fmt.Fprintln(out, "")
   112  
   113  	prShared.PrintHeader(opts.IO, "Issues assigned to you")
   114  	if issuePayload.Assigned.TotalCount > 0 {
   115  		issueShared.PrintIssues(opts.IO, time.Now(), "  ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues)
   116  	} else {
   117  		message := "  There are no issues assigned to you"
   118  		prShared.PrintMessage(opts.IO, message)
   119  	}
   120  	fmt.Fprintln(out)
   121  
   122  	prShared.PrintHeader(opts.IO, "Issues mentioning you")
   123  	if issuePayload.Mentioned.TotalCount > 0 {
   124  		issueShared.PrintIssues(opts.IO, time.Now(), "  ", issuePayload.Mentioned.TotalCount, issuePayload.Mentioned.Issues)
   125  	} else {
   126  		prShared.PrintMessage(opts.IO, "  There are no issues mentioning you")
   127  	}
   128  	fmt.Fprintln(out)
   129  
   130  	prShared.PrintHeader(opts.IO, "Issues opened by you")
   131  	if issuePayload.Authored.TotalCount > 0 {
   132  		issueShared.PrintIssues(opts.IO, time.Now(), "  ", issuePayload.Authored.TotalCount, issuePayload.Authored.Issues)
   133  	} else {
   134  		prShared.PrintMessage(opts.IO, "  There are no issues opened by you")
   135  	}
   136  	fmt.Fprintln(out)
   137  
   138  	return nil
   139  }