github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/issue/reopen/reopen.go (about) 1 package reopen 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/cli/cli/api" 8 "github.com/cli/cli/internal/config" 9 "github.com/cli/cli/internal/ghrepo" 10 "github.com/cli/cli/pkg/cmd/issue/shared" 11 "github.com/cli/cli/pkg/cmdutil" 12 "github.com/cli/cli/pkg/iostreams" 13 "github.com/spf13/cobra" 14 ) 15 16 type ReopenOptions struct { 17 HttpClient func() (*http.Client, error) 18 Config func() (config.Config, error) 19 IO *iostreams.IOStreams 20 BaseRepo func() (ghrepo.Interface, error) 21 22 SelectorArg string 23 } 24 25 func NewCmdReopen(f *cmdutil.Factory, runF func(*ReopenOptions) error) *cobra.Command { 26 opts := &ReopenOptions{ 27 IO: f.IOStreams, 28 HttpClient: f.HttpClient, 29 Config: f.Config, 30 } 31 32 cmd := &cobra.Command{ 33 Use: "reopen {<number> | <url>}", 34 Short: "Reopen issue", 35 Args: cobra.ExactArgs(1), 36 RunE: func(cmd *cobra.Command, args []string) error { 37 // support `-R, --repo` override 38 opts.BaseRepo = f.BaseRepo 39 40 if len(args) > 0 { 41 opts.SelectorArg = args[0] 42 } 43 44 if runF != nil { 45 return runF(opts) 46 } 47 return reopenRun(opts) 48 }, 49 } 50 51 return cmd 52 } 53 54 func reopenRun(opts *ReopenOptions) error { 55 cs := opts.IO.ColorScheme() 56 57 httpClient, err := opts.HttpClient() 58 if err != nil { 59 return err 60 } 61 apiClient := api.NewClientFromHTTP(httpClient) 62 63 issue, baseRepo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg) 64 if err != nil { 65 return err 66 } 67 68 if issue.State == "OPEN" { 69 fmt.Fprintf(opts.IO.ErrOut, "%s Issue #%d (%s) is already open\n", cs.Yellow("!"), issue.Number, issue.Title) 70 return nil 71 } 72 73 err = api.IssueReopen(apiClient, baseRepo, *issue) 74 if err != nil { 75 return err 76 } 77 78 fmt.Fprintf(opts.IO.ErrOut, "%s Reopened issue #%d (%s)\n", cs.SuccessIconWithColor(cs.Green), issue.Number, issue.Title) 79 80 return nil 81 }