Skip to content

Commit 4e5aa91

Browse files
authored
Merge pull request cli#2949 from cli/edit-improvements
Change behavior of slice flags for issue edit and pr edit commands
2 parents a84145e + 4fdf28d commit 4e5aa91

File tree

9 files changed

+651
-407
lines changed

9 files changed

+651
-407
lines changed

api/queries_issue.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,29 @@ type Assignees struct {
4949
TotalCount int
5050
}
5151

52+
func (a Assignees) Logins() []string {
53+
logins := make([]string, len(a.Nodes))
54+
for i, a := range a.Nodes {
55+
logins[i] = a.Login
56+
}
57+
return logins
58+
}
59+
5260
type Labels struct {
5361
Nodes []struct {
5462
Name string
5563
}
5664
TotalCount int
5765
}
5866

67+
func (l Labels) Names() []string {
68+
names := make([]string, len(l.Nodes))
69+
for i, l := range l.Nodes {
70+
names[i] = l.Name
71+
}
72+
return names
73+
}
74+
5975
type ProjectCards struct {
6076
Nodes []struct {
6177
Project struct {
@@ -68,6 +84,14 @@ type ProjectCards struct {
6884
TotalCount int
6985
}
7086

87+
func (p ProjectCards) ProjectNames() []string {
88+
names := make([]string, len(p.Nodes))
89+
for i, c := range p.Nodes {
90+
names[i] = c.Project.Name
91+
}
92+
return names
93+
}
94+
7195
type Milestone struct {
7296
Title string
7397
}

api/queries_pr.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,10 @@ type PullRequest struct {
8080
}
8181
}
8282
}
83-
Assignees struct {
84-
Nodes []struct {
85-
Login string
86-
}
87-
TotalCount int
88-
}
89-
Labels struct {
90-
Nodes []struct {
91-
Name string
92-
}
93-
TotalCount int
94-
}
95-
ProjectCards struct {
96-
Nodes []struct {
97-
Project struct {
98-
Name string
99-
}
100-
Column struct {
101-
Name string
102-
}
103-
}
104-
TotalCount int
105-
}
106-
Milestone struct {
107-
Title string
108-
}
83+
Assignees Assignees
84+
Labels Labels
85+
ProjectCards ProjectCards
86+
Milestone Milestone
10987
Comments Comments
11088
ReactionGroups ReactionGroups
11189
Reviews PullRequestReviews
@@ -123,6 +101,14 @@ type ReviewRequests struct {
123101
TotalCount int
124102
}
125103

104+
func (r ReviewRequests) Logins() []string {
105+
logins := make([]string, len(r.Nodes))
106+
for i, a := range r.Nodes {
107+
logins[i] = a.RequestedReviewer.Login
108+
}
109+
return logins
110+
}
111+
126112
type NotFoundError struct {
127113
error
128114
}
@@ -816,6 +802,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
816802
reviewParams["teamIds"] = ids
817803
}
818804

805+
//TODO: How much work to extract this into own method and use for create and edit?
819806
if len(reviewParams) > 0 {
820807
reviewQuery := `
821808
mutation PullRequestCreateRequestReviews($input: RequestReviewsInput!) {

api/queries_repo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"net/http"
109
"sort"
@@ -497,7 +496,7 @@ func (m *RepoMetadataResult) MilestoneToID(title string) (string, error) {
497496
return m.ID, nil
498497
}
499498
}
500-
return "", errors.New("not found")
499+
return "", fmt.Errorf("'%s' not found", title)
501500
}
502501

503502
func (m *RepoMetadataResult) Merge(m2 *RepoMetadataResult) {

pkg/cmd/issue/edit/edit.go

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
4747
Short: "Edit an issue",
4848
Example: heredoc.Doc(`
4949
$ 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"
50+
$ gh issue edit 23 --add-label "bug,help wanted" --remove-label "core"
51+
$ gh issue edit 23 --add-assignee @me --remove-assignee monalisa,hubot
52+
$ gh issue edit 23 --add-project "Roadmap" --remove-project v1,v2
53+
$ gh issue edit 23 --milestone "Version 1"
5554
`),
5655
Args: cobra.ExactArgs(1),
5756
RunE: func(cmd *cobra.Command, args []string) error {
@@ -62,30 +61,30 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
6261

6362
flags := cmd.Flags()
6463
if flags.Changed("title") {
65-
opts.Editable.TitleEdited = true
64+
opts.Editable.Title.Edited = true
6665
}
6766
if flags.Changed("body") {
68-
opts.Editable.BodyEdited = true
67+
opts.Editable.Body.Edited = true
6968
}
70-
if flags.Changed("assignee") {
71-
opts.Editable.AssigneesEdited = true
69+
if flags.Changed("add-assignee") || flags.Changed("remove-assignee") {
70+
opts.Editable.Assignees.Edited = true
7271
}
73-
if flags.Changed("label") {
74-
opts.Editable.LabelsEdited = true
72+
if flags.Changed("add-label") || flags.Changed("remove-label") {
73+
opts.Editable.Labels.Edited = true
7574
}
76-
if flags.Changed("project") {
77-
opts.Editable.ProjectsEdited = true
75+
if flags.Changed("add-project") || flags.Changed("remove-project") {
76+
opts.Editable.Projects.Edited = true
7877
}
7978
if flags.Changed("milestone") {
80-
opts.Editable.MilestoneEdited = true
79+
opts.Editable.Milestone.Edited = true
8180
}
8281

8382
if !opts.Editable.Dirty() {
8483
opts.Interactive = true
8584
}
8685

8786
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")}
87+
return &cmdutil.FlagError{Err: errors.New("field to edit flag required when not running interactively")}
8988
}
9089

9190
if runF != nil {
@@ -96,12 +95,15 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
9695
},
9796
}
9897

99-
cmd.Flags().StringVarP(&opts.Editable.Title, "title", "t", "", "Revise the issue title.")
100-
cmd.Flags().StringVarP(&opts.Editable.Body, "body", "b", "", "Revise the issue body.")
101-
cmd.Flags().StringSliceVarP(&opts.Editable.Assignees, "assignee", "a", nil, "Set assigned people by their `login`. Use \"@me\" to self-assign.")
102-
cmd.Flags().StringSliceVarP(&opts.Editable.Labels, "label", "l", nil, "Set the issue labels by `name`")
103-
cmd.Flags().StringSliceVarP(&opts.Editable.Projects, "project", "p", nil, "Set the projects the issue belongs to by `name`")
104-
cmd.Flags().StringVarP(&opts.Editable.Milestone, "milestone", "m", "", "Set the milestone the issue belongs to by `name`")
98+
cmd.Flags().StringVarP(&opts.Editable.Title.Value, "title", "t", "", "Set the new title.")
99+
cmd.Flags().StringVarP(&opts.Editable.Body.Value, "body", "b", "", "Set the new body.")
100+
cmd.Flags().StringSliceVar(&opts.Editable.Assignees.Add, "add-assignee", nil, "Add assigned users by their `login`. Use \"@me\" to assign yourself.")
101+
cmd.Flags().StringSliceVar(&opts.Editable.Assignees.Remove, "remove-assignee", nil, "Remove assigned users by their `login`. Use \"@me\" to unassign yourself.")
102+
cmd.Flags().StringSliceVar(&opts.Editable.Labels.Add, "add-label", nil, "Add labels by `name`")
103+
cmd.Flags().StringSliceVar(&opts.Editable.Labels.Remove, "remove-label", nil, "Remove labels by `name`")
104+
cmd.Flags().StringSliceVar(&opts.Editable.Projects.Add, "add-project", nil, "Add the issue to projects by `name`")
105+
cmd.Flags().StringSliceVar(&opts.Editable.Projects.Remove, "remove-project", nil, "Remove the issue from projects by `name`")
106+
cmd.Flags().StringVarP(&opts.Editable.Milestone.Value, "milestone", "m", "", "Edit the milestone the issue belongs to by `name`")
105107

106108
return cmd
107109
}
@@ -119,12 +121,12 @@ func editRun(opts *EditOptions) error {
119121
}
120122

121123
editable := opts.Editable
122-
editable.TitleDefault = issue.Title
123-
editable.BodyDefault = issue.Body
124-
editable.AssigneesDefault = issue.Assignees
125-
editable.LabelsDefault = issue.Labels
126-
editable.ProjectsDefault = issue.ProjectCards
127-
editable.MilestoneDefault = issue.Milestone
124+
editable.Title.Default = issue.Title
125+
editable.Body.Default = issue.Body
126+
editable.Assignees.Default = issue.Assignees.Logins()
127+
editable.Labels.Default = issue.Labels.Names()
128+
editable.Projects.Default = issue.ProjectCards.ProjectNames()
129+
editable.Milestone.Default = issue.Milestone.Title
128130

129131
if opts.Interactive {
130132
err = opts.FieldsToEditSurvey(&editable)
@@ -167,24 +169,59 @@ func updateIssue(client *api.Client, repo ghrepo.Interface, id string, options p
167169
var err error
168170
params := githubv4.UpdateIssueInput{
169171
ID: id,
170-
Title: options.TitleParam(),
171-
Body: options.BodyParam(),
172+
Title: ghString(options.TitleValue()),
173+
Body: ghString(options.BodyValue()),
172174
}
173-
params.AssigneeIDs, err = options.AssigneesParam(client, repo)
175+
assigneeIds, err := options.AssigneeIds(client, repo)
174176
if err != nil {
175177
return err
176178
}
177-
params.LabelIDs, err = options.LabelsParam()
179+
params.AssigneeIDs = ghIds(assigneeIds)
180+
labelIds, err := options.LabelIds()
178181
if err != nil {
179182
return err
180183
}
181-
params.ProjectIDs, err = options.ProjectsParam()
184+
params.LabelIDs = ghIds(labelIds)
185+
projectIds, err := options.ProjectIds()
182186
if err != nil {
183187
return err
184188
}
185-
params.MilestoneID, err = options.MilestoneParam()
189+
params.ProjectIDs = ghIds(projectIds)
190+
milestoneId, err := options.MilestoneId()
186191
if err != nil {
187192
return err
188193
}
194+
params.MilestoneID = ghId(milestoneId)
189195
return api.IssueUpdate(client, repo, params)
190196
}
197+
198+
func ghIds(s *[]string) *[]githubv4.ID {
199+
if s == nil {
200+
return nil
201+
}
202+
ids := make([]githubv4.ID, len(*s))
203+
for i, v := range *s {
204+
ids[i] = v
205+
}
206+
return &ids
207+
}
208+
209+
func ghId(s *string) *githubv4.ID {
210+
if s == nil {
211+
return nil
212+
}
213+
if *s == "" {
214+
r := githubv4.ID(nil)
215+
return &r
216+
}
217+
r := githubv4.ID(*s)
218+
return &r
219+
}
220+
221+
func ghString(s *string) *githubv4.String {
222+
if s == nil {
223+
return nil
224+
}
225+
r := githubv4.String(*s)
226+
return &r
227+
}

0 commit comments

Comments
 (0)