github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/reopen/reopen.go (about) 1 package reopen 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/ungtb10d/cli/v2/api" 8 "github.com/ungtb10d/cli/v2/internal/config" 9 "github.com/ungtb10d/cli/v2/internal/ghrepo" 10 "github.com/ungtb10d/cli/v2/pkg/cmd/issue/shared" 11 prShared "github.com/ungtb10d/cli/v2/pkg/cmd/pr/shared" 12 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 13 "github.com/ungtb10d/cli/v2/pkg/iostreams" 14 "github.com/shurcooL/githubv4" 15 "github.com/spf13/cobra" 16 ) 17 18 type ReopenOptions 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 SelectorArg string 25 Comment string 26 } 27 28 func NewCmdReopen(f *cmdutil.Factory, runF func(*ReopenOptions) error) *cobra.Command { 29 opts := &ReopenOptions{ 30 IO: f.IOStreams, 31 HttpClient: f.HttpClient, 32 Config: f.Config, 33 } 34 35 cmd := &cobra.Command{ 36 Use: "reopen {<number> | <url>}", 37 Short: "Reopen issue", 38 Args: cobra.ExactArgs(1), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 // support `-R, --repo` override 41 opts.BaseRepo = f.BaseRepo 42 43 if len(args) > 0 { 44 opts.SelectorArg = args[0] 45 } 46 47 if runF != nil { 48 return runF(opts) 49 } 50 return reopenRun(opts) 51 }, 52 } 53 54 cmd.Flags().StringVarP(&opts.Comment, "comment", "c", "", "Add a reopening comment") 55 56 return cmd 57 } 58 59 func reopenRun(opts *ReopenOptions) error { 60 cs := opts.IO.ColorScheme() 61 62 httpClient, err := opts.HttpClient() 63 if err != nil { 64 return err 65 } 66 67 issue, baseRepo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.SelectorArg, []string{"id", "number", "title", "state"}) 68 if err != nil { 69 return err 70 } 71 72 if issue.State == "OPEN" { 73 fmt.Fprintf(opts.IO.ErrOut, "%s Issue #%d (%s) is already open\n", cs.Yellow("!"), issue.Number, issue.Title) 74 return nil 75 } 76 77 if opts.Comment != "" { 78 commentOpts := &prShared.CommentableOptions{ 79 Body: opts.Comment, 80 HttpClient: opts.HttpClient, 81 InputType: prShared.InputTypeInline, 82 Quiet: true, 83 RetrieveCommentable: func() (prShared.Commentable, ghrepo.Interface, error) { 84 return issue, baseRepo, nil 85 }, 86 } 87 err := prShared.CommentableRun(commentOpts) 88 if err != nil { 89 return err 90 } 91 } 92 93 err = apiReopen(httpClient, baseRepo, issue) 94 if err != nil { 95 return err 96 } 97 98 fmt.Fprintf(opts.IO.ErrOut, "%s Reopened issue #%d (%s)\n", cs.SuccessIconWithColor(cs.Green), issue.Number, issue.Title) 99 100 return nil 101 } 102 103 func apiReopen(httpClient *http.Client, repo ghrepo.Interface, issue *api.Issue) error { 104 if issue.IsPullRequest() { 105 return api.PullRequestReopen(httpClient, repo, issue.ID) 106 } 107 108 var mutation struct { 109 ReopenIssue struct { 110 Issue struct { 111 ID githubv4.ID 112 } 113 } `graphql:"reopenIssue(input: $input)"` 114 } 115 116 variables := map[string]interface{}{ 117 "input": githubv4.ReopenIssueInput{ 118 IssueID: issue.ID, 119 }, 120 } 121 122 gql := api.NewClientFromHTTP(httpClient) 123 return gql.Mutate(repo.RepoHost(), "IssueReopen", &mutation, variables) 124 }