|
| 1 | +package edit |
| 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/internal/ghrepo" |
| 11 | + shared "github.com/cli/cli/pkg/cmd/issue/shared" |
| 12 | + prShared "github.com/cli/cli/pkg/cmd/pr/shared" |
| 13 | + "github.com/cli/cli/pkg/cmdutil" |
| 14 | + "github.com/cli/cli/pkg/iostreams" |
| 15 | + "github.com/shurcooL/githubv4" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +type EditOptions struct { |
| 20 | + HttpClient func() (*http.Client, error) |
| 21 | + IO *iostreams.IOStreams |
| 22 | + BaseRepo func() (ghrepo.Interface, error) |
| 23 | + |
| 24 | + DetermineEditor func() (string, error) |
| 25 | + FieldsToEditSurvey func(*prShared.EditableOptions) error |
| 26 | + EditableSurvey func(string, *prShared.EditableOptions) error |
| 27 | + FetchOptions func(*api.Client, ghrepo.Interface, *prShared.EditableOptions) error |
| 28 | + |
| 29 | + SelectorArg string |
| 30 | + Interactive bool |
| 31 | + |
| 32 | + prShared.EditableOptions |
| 33 | +} |
| 34 | + |
| 35 | +func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { |
| 36 | + opts := &EditOptions{ |
| 37 | + IO: f.IOStreams, |
| 38 | + HttpClient: f.HttpClient, |
| 39 | + DetermineEditor: func() (string, error) { return cmdutil.DetermineEditor(f.Config) }, |
| 40 | + FieldsToEditSurvey: prShared.FieldsToEditSurvey, |
| 41 | + EditableSurvey: prShared.EditableSurvey, |
| 42 | + FetchOptions: prShared.FetchOptions, |
| 43 | + } |
| 44 | + |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "edit {<number> | <url>}", |
| 47 | + Short: "Edit an issue", |
| 48 | + Example: heredoc.Doc(` |
| 49 | + $ gh issue edit 23 --title "I found a bug" --body "Nothing works" |
| 50 | + $ gh issue edit 23 --label "bug,help wanted" |
| 51 | + $ gh issue edit 23 --label bug --label "help wanted" |
| 52 | + $ gh issue edit 23 --assignee monalisa,hubot |
| 53 | + $ gh issue edit 23 --assignee @me |
| 54 | + $ gh issue edit 23 --project "Roadmap" |
| 55 | + `), |
| 56 | + Args: cobra.ExactArgs(1), |
| 57 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | + // support `-R, --repo` override |
| 59 | + opts.BaseRepo = f.BaseRepo |
| 60 | + |
| 61 | + opts.SelectorArg = args[0] |
| 62 | + |
| 63 | + flags := cmd.Flags() |
| 64 | + if flags.Changed("title") { |
| 65 | + opts.EditableOptions.TitleEdited = true |
| 66 | + } |
| 67 | + if flags.Changed("body") { |
| 68 | + opts.EditableOptions.BodyEdited = true |
| 69 | + } |
| 70 | + if flags.Changed("assignee") { |
| 71 | + opts.EditableOptions.AssigneesEdited = true |
| 72 | + } |
| 73 | + if flags.Changed("label") { |
| 74 | + opts.EditableOptions.LabelsEdited = true |
| 75 | + } |
| 76 | + if flags.Changed("project") { |
| 77 | + opts.EditableOptions.ProjectsEdited = true |
| 78 | + } |
| 79 | + if flags.Changed("milestone") { |
| 80 | + opts.EditableOptions.MilestoneEdited = true |
| 81 | + } |
| 82 | + |
| 83 | + if !opts.EditableOptions.Dirty() { |
| 84 | + opts.Interactive = true |
| 85 | + } |
| 86 | + |
| 87 | + if opts.Interactive && !opts.IO.CanPrompt() { |
| 88 | + return &cmdutil.FlagError{Err: errors.New("--tile, --body, --assignee, --label, --project, or --milestone required when not running interactively")} |
| 89 | + } |
| 90 | + |
| 91 | + if runF != nil { |
| 92 | + return runF(opts) |
| 93 | + } |
| 94 | + |
| 95 | + return editRun(opts) |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + cmd.Flags().StringVarP(&opts.EditableOptions.Title, "title", "t", "", "Revise the issue title.") |
| 100 | + cmd.Flags().StringVarP(&opts.EditableOptions.Body, "body", "b", "", "Revise the issue body.") |
| 101 | + cmd.Flags().StringSliceVarP(&opts.EditableOptions.Assignees, "assignee", "a", nil, "Set assigned people by their `login`. Use \"@me\" to self-assign.") |
| 102 | + cmd.Flags().StringSliceVarP(&opts.EditableOptions.Labels, "label", "l", nil, "Set the issue labels by `name`") |
| 103 | + cmd.Flags().StringSliceVarP(&opts.EditableOptions.Projects, "project", "p", nil, "Set the projects the issue belongs to by `name`") |
| 104 | + cmd.Flags().StringVarP(&opts.EditableOptions.Milestone, "milestone", "m", "", "Set the milestone the issue belongs to by `name`") |
| 105 | + |
| 106 | + return cmd |
| 107 | +} |
| 108 | + |
| 109 | +func editRun(opts *EditOptions) error { |
| 110 | + httpClient, err := opts.HttpClient() |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + apiClient := api.NewClientFromHTTP(httpClient) |
| 115 | + |
| 116 | + issue, repo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + editOptions := opts.EditableOptions |
| 122 | + editOptions.TitleDefault = issue.Title |
| 123 | + editOptions.BodyDefault = issue.Body |
| 124 | + editOptions.AssigneesDefault = issue.Assignees |
| 125 | + editOptions.LabelsDefault = issue.Labels |
| 126 | + editOptions.ProjectsDefault = issue.ProjectCards |
| 127 | + editOptions.MilestoneDefault = issue.Milestone |
| 128 | + |
| 129 | + if opts.Interactive { |
| 130 | + err = opts.FieldsToEditSurvey(&editOptions) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + opts.IO.StartProgressIndicator() |
| 137 | + err = opts.FetchOptions(apiClient, repo, &editOptions) |
| 138 | + opts.IO.StopProgressIndicator() |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + if opts.Interactive { |
| 144 | + editorCommand, err := opts.DetermineEditor() |
| 145 | + if err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + err = opts.EditableSurvey(editorCommand, &editOptions) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + opts.IO.StartProgressIndicator() |
| 155 | + err = updateIssue(apiClient, repo, issue.ID, editOptions) |
| 156 | + opts.IO.StopProgressIndicator() |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + fmt.Fprintln(opts.IO.Out, issue.URL) |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func updateIssue(client *api.Client, repo ghrepo.Interface, id string, options prShared.EditableOptions) error { |
| 167 | + params := githubv4.UpdateIssueInput{ID: id} |
| 168 | + if options.TitleEdited { |
| 169 | + title := githubv4.String(options.Title) |
| 170 | + params.Title = &title |
| 171 | + } |
| 172 | + if options.BodyEdited { |
| 173 | + body := githubv4.String(options.Body) |
| 174 | + params.Body = &body |
| 175 | + } |
| 176 | + if options.AssigneesEdited { |
| 177 | + meReplacer := prShared.NewMeReplacer(client, repo.RepoHost()) |
| 178 | + assignees, err := meReplacer.ReplaceSlice(options.Assignees) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + ids, err := options.Metadata.MembersToIDs(assignees) |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + assigneeIDs := make([]githubv4.ID, len(ids)) |
| 187 | + for i, v := range ids { |
| 188 | + assigneeIDs[i] = v |
| 189 | + } |
| 190 | + params.AssigneeIDs = &assigneeIDs |
| 191 | + } |
| 192 | + if options.LabelsEdited { |
| 193 | + ids, err := options.Metadata.LabelsToIDs(options.Labels) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + labelIDs := make([]githubv4.ID, len(ids)) |
| 198 | + for i, v := range ids { |
| 199 | + labelIDs[i] = v |
| 200 | + } |
| 201 | + params.LabelIDs = &labelIDs |
| 202 | + } |
| 203 | + if options.ProjectsEdited { |
| 204 | + ids, err := options.Metadata.ProjectsToIDs(options.Projects) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + projectIDs := make([]githubv4.ID, len(ids)) |
| 209 | + for i, v := range ids { |
| 210 | + projectIDs[i] = v |
| 211 | + } |
| 212 | + params.ProjectIDs = &projectIDs |
| 213 | + } |
| 214 | + if options.MilestoneEdited { |
| 215 | + id, err := options.Metadata.MilestoneToID(options.Milestone) |
| 216 | + if err != nil { |
| 217 | + return err |
| 218 | + } |
| 219 | + milestoneID := githubv4.ID(id) |
| 220 | + params.MilestoneID = &milestoneID |
| 221 | + } |
| 222 | + return api.IssueUpdate(client, repo, params) |
| 223 | +} |
0 commit comments