Skip to content

Commit c8d5a6b

Browse files
committed
Accept pull requests in issue edit argument
1 parent b75e705 commit c8d5a6b

File tree

5 files changed

+133
-150
lines changed

5 files changed

+133
-150
lines changed

api/queries_issue.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package api
22

33
import (
4-
"context"
54
"fmt"
65
"time"
76

87
"github.com/cli/cli/v2/internal/ghrepo"
9-
"github.com/shurcooL/githubv4"
108
)
119

1210
type IssuesPayload struct {
@@ -342,20 +340,6 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
342340
return &resp.Repository.Issue, nil
343341
}
344342

345-
func IssueUpdate(client *Client, repo ghrepo.Interface, params githubv4.UpdateIssueInput) error {
346-
var mutation struct {
347-
UpdateIssue struct {
348-
Issue struct {
349-
ID string
350-
}
351-
} `graphql:"updateIssue(input: $input)"`
352-
}
353-
variables := map[string]interface{}{"input": params}
354-
gql := graphQLClient(client.http, repo.RepoHost())
355-
err := gql.MutateNamed(context.Background(), "IssueUpdate", &mutation, variables)
356-
return err
357-
}
358-
359343
func (i Issue) Link() string {
360344
return i.URL
361345
}

api/queries_pr.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -621,20 +621,6 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
621621
return pr, nil
622622
}
623623

624-
func UpdatePullRequest(client *Client, repo ghrepo.Interface, params githubv4.UpdatePullRequestInput) error {
625-
var mutation struct {
626-
UpdatePullRequest struct {
627-
PullRequest struct {
628-
ID string
629-
}
630-
} `graphql:"updatePullRequest(input: $input)"`
631-
}
632-
variables := map[string]interface{}{"input": params}
633-
gql := graphQLClient(client.http, repo.RepoHost())
634-
err := gql.MutateNamed(context.Background(), "PullRequestUpdate", &mutation, variables)
635-
return err
636-
}
637-
638624
func UpdatePullRequestReviews(client *Client, repo ghrepo.Interface, params githubv4.RequestReviewsInput) error {
639625
var mutation struct {
640626
RequestReviews struct {

pkg/cmd/issue/edit/edit.go

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
1212
"github.com/cli/cli/v2/pkg/cmdutil"
1313
"github.com/cli/cli/v2/pkg/iostreams"
14-
"github.com/shurcooL/githubv4"
1514
"github.com/spf13/cobra"
1615
)
1716

@@ -130,14 +129,15 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
130129
return cmd
131130
}
132131

132+
var lookupFields = []string{"id", "number", "title", "body", "assignees", "labels", "projectCards", "milestone", "url"}
133+
133134
func editRun(opts *EditOptions) error {
134135
httpClient, err := opts.HttpClient()
135136
if err != nil {
136137
return err
137138
}
138-
apiClient := api.NewClientFromHTTP(httpClient)
139139

140-
issue, repo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg)
140+
issue, repo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.SelectorArg, lookupFields)
141141
if err != nil {
142142
return err
143143
}
@@ -159,6 +159,7 @@ func editRun(opts *EditOptions) error {
159159
}
160160
}
161161

162+
apiClient := api.NewClientFromHTTP(httpClient)
162163
opts.IO.StartProgressIndicator()
163164
err = opts.FetchOptions(apiClient, repo, &editable)
164165
opts.IO.StopProgressIndicator()
@@ -178,7 +179,7 @@ func editRun(opts *EditOptions) error {
178179
}
179180

180181
opts.IO.StartProgressIndicator()
181-
err = updateIssue(apiClient, repo, issue.ID, editable)
182+
err = prShared.UpdateIssue(httpClient, repo, issue.ID, issue.IsPullRequest(), editable)
182183
opts.IO.StopProgressIndicator()
183184
if err != nil {
184185
return err
@@ -188,64 +189,3 @@ func editRun(opts *EditOptions) error {
188189

189190
return nil
190191
}
191-
192-
func updateIssue(client *api.Client, repo ghrepo.Interface, id string, options prShared.Editable) error {
193-
var err error
194-
params := githubv4.UpdateIssueInput{
195-
ID: id,
196-
Title: ghString(options.TitleValue()),
197-
Body: ghString(options.BodyValue()),
198-
}
199-
assigneeIds, err := options.AssigneeIds(client, repo)
200-
if err != nil {
201-
return err
202-
}
203-
params.AssigneeIDs = ghIds(assigneeIds)
204-
labelIds, err := options.LabelIds()
205-
if err != nil {
206-
return err
207-
}
208-
params.LabelIDs = ghIds(labelIds)
209-
projectIds, err := options.ProjectIds()
210-
if err != nil {
211-
return err
212-
}
213-
params.ProjectIDs = ghIds(projectIds)
214-
milestoneId, err := options.MilestoneId()
215-
if err != nil {
216-
return err
217-
}
218-
params.MilestoneID = ghId(milestoneId)
219-
return api.IssueUpdate(client, repo, params)
220-
}
221-
222-
func ghIds(s *[]string) *[]githubv4.ID {
223-
if s == nil {
224-
return nil
225-
}
226-
ids := make([]githubv4.ID, len(*s))
227-
for i, v := range *s {
228-
ids[i] = v
229-
}
230-
return &ids
231-
}
232-
233-
func ghId(s *string) *githubv4.ID {
234-
if s == nil {
235-
return nil
236-
}
237-
if *s == "" {
238-
r := githubv4.ID(nil)
239-
return &r
240-
}
241-
r := githubv4.ID(*s)
242-
return &r
243-
}
244-
245-
func ghString(s *string) *githubv4.String {
246-
if s == nil {
247-
return nil
248-
}
249-
r := githubv4.String(*s)
250-
return &r
251-
}

pkg/cmd/pr/edit/edit.go

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func editRun(opts *EditOptions) error {
202202
}
203203

204204
opts.IO.StartProgressIndicator()
205-
err = updatePullRequest(apiClient, repo, pr.ID, editable)
205+
err = updatePullRequest(httpClient, repo, pr.ID, editable)
206206
opts.IO.StopProgressIndicator()
207207
if err != nil {
208208
return err
@@ -213,44 +213,14 @@ func editRun(opts *EditOptions) error {
213213
return nil
214214
}
215215

216-
func updatePullRequest(client *api.Client, repo ghrepo.Interface, id string, editable shared.Editable) error {
217-
var err error
218-
params := githubv4.UpdatePullRequestInput{
219-
PullRequestID: id,
220-
Title: ghString(editable.TitleValue()),
221-
Body: ghString(editable.BodyValue()),
222-
}
223-
assigneeIds, err := editable.AssigneeIds(client, repo)
224-
if err != nil {
225-
return err
226-
}
227-
params.AssigneeIDs = ghIds(assigneeIds)
228-
labelIds, err := editable.LabelIds()
229-
if err != nil {
230-
return err
231-
}
232-
params.LabelIDs = ghIds(labelIds)
233-
projectIds, err := editable.ProjectIds()
234-
if err != nil {
216+
func updatePullRequest(httpClient *http.Client, repo ghrepo.Interface, id string, editable shared.Editable) error {
217+
if err := shared.UpdateIssue(httpClient, repo, id, true, editable); err != nil {
235218
return err
236219
}
237-
params.ProjectIDs = ghIds(projectIds)
238-
milestoneId, err := editable.MilestoneId()
239-
if err != nil {
240-
return err
241-
}
242-
params.MilestoneID = ghId(milestoneId)
243-
if editable.Base.Edited {
244-
params.BaseRefName = ghString(&editable.Base.Value)
245-
}
246-
err = api.UpdatePullRequest(client, repo, params)
247-
if err != nil {
248-
return err
249-
}
250-
return updatePullRequestReviews(client, repo, id, editable)
220+
return updatePullRequestReviews(httpClient, repo, id, editable)
251221
}
252222

253-
func updatePullRequestReviews(client *api.Client, repo ghrepo.Interface, id string, editable shared.Editable) error {
223+
func updatePullRequestReviews(httpClient *http.Client, repo ghrepo.Interface, id string, editable shared.Editable) error {
254224
if !editable.Reviewers.Edited {
255225
return nil
256226
}
@@ -265,6 +235,7 @@ func updatePullRequestReviews(client *api.Client, repo ghrepo.Interface, id stri
265235
UserIDs: ghIds(userIds),
266236
TeamIDs: ghIds(teamIds),
267237
}
238+
client := api.NewClientFromHTTP(httpClient)
268239
return api.UpdatePullRequestReviews(client, repo, reviewsRequestParams)
269240
}
270241

@@ -315,23 +286,3 @@ func ghIds(s *[]string) *[]githubv4.ID {
315286
}
316287
return &ids
317288
}
318-
319-
func ghId(s *string) *githubv4.ID {
320-
if s == nil {
321-
return nil
322-
}
323-
if *s == "" {
324-
r := githubv4.ID(nil)
325-
return &r
326-
}
327-
r := githubv4.ID(*s)
328-
return &r
329-
}
330-
331-
func ghString(s *string) *githubv4.String {
332-
if s == nil {
333-
return nil
334-
}
335-
r := githubv4.String(*s)
336-
return &r
337-
}

pkg/cmd/pr/shared/editable_http.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package shared
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/cli/cli/v2/api"
8+
"github.com/cli/cli/v2/internal/ghinstance"
9+
"github.com/cli/cli/v2/internal/ghrepo"
10+
graphql "github.com/cli/shurcooL-graphql"
11+
"github.com/shurcooL/githubv4"
12+
)
13+
14+
func UpdateIssue(httpClient *http.Client, repo ghrepo.Interface, id string, isPR bool, options Editable) error {
15+
title := ghString(options.TitleValue())
16+
body := ghString(options.BodyValue())
17+
18+
apiClient := api.NewClientFromHTTP(httpClient)
19+
assigneeIds, err := options.AssigneeIds(apiClient, repo)
20+
if err != nil {
21+
return err
22+
}
23+
24+
labelIds, err := options.LabelIds()
25+
if err != nil {
26+
return err
27+
}
28+
29+
projectIds, err := options.ProjectIds()
30+
if err != nil {
31+
return err
32+
}
33+
34+
milestoneId, err := options.MilestoneId()
35+
if err != nil {
36+
return err
37+
}
38+
39+
if isPR {
40+
params := githubv4.UpdatePullRequestInput{
41+
PullRequestID: id,
42+
Title: title,
43+
Body: body,
44+
AssigneeIDs: ghIds(assigneeIds),
45+
LabelIDs: ghIds(labelIds),
46+
ProjectIDs: ghIds(projectIds),
47+
MilestoneID: ghId(milestoneId),
48+
}
49+
if options.Base.Edited {
50+
params.BaseRefName = ghString(&options.Base.Value)
51+
}
52+
return updatePullRequest(httpClient, repo, params)
53+
}
54+
55+
return updateIssue(httpClient, repo, githubv4.UpdateIssueInput{
56+
ID: id,
57+
Title: title,
58+
Body: body,
59+
AssigneeIDs: ghIds(assigneeIds),
60+
LabelIDs: ghIds(labelIds),
61+
ProjectIDs: ghIds(projectIds),
62+
MilestoneID: ghId(milestoneId),
63+
})
64+
}
65+
66+
func updateIssue(httpClient *http.Client, repo ghrepo.Interface, params githubv4.UpdateIssueInput) error {
67+
var mutation struct {
68+
UpdateIssue struct {
69+
Issue struct {
70+
ID string
71+
}
72+
} `graphql:"updateIssue(input: $input)"`
73+
}
74+
variables := map[string]interface{}{"input": params}
75+
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
76+
return gql.MutateNamed(context.Background(), "IssueUpdate", &mutation, variables)
77+
}
78+
79+
func updatePullRequest(httpClient *http.Client, repo ghrepo.Interface, params githubv4.UpdatePullRequestInput) error {
80+
var mutation struct {
81+
UpdatePullRequest struct {
82+
PullRequest struct {
83+
ID string
84+
}
85+
} `graphql:"updatePullRequest(input: $input)"`
86+
}
87+
variables := map[string]interface{}{"input": params}
88+
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
89+
err := gql.MutateNamed(context.Background(), "PullRequestUpdate", &mutation, variables)
90+
return err
91+
}
92+
93+
func ghIds(s *[]string) *[]githubv4.ID {
94+
if s == nil {
95+
return nil
96+
}
97+
ids := make([]githubv4.ID, len(*s))
98+
for i, v := range *s {
99+
ids[i] = v
100+
}
101+
return &ids
102+
}
103+
104+
func ghId(s *string) *githubv4.ID {
105+
if s == nil {
106+
return nil
107+
}
108+
if *s == "" {
109+
r := githubv4.ID(nil)
110+
return &r
111+
}
112+
r := githubv4.ID(*s)
113+
return &r
114+
}
115+
116+
func ghString(s *string) *githubv4.String {
117+
if s == nil {
118+
return nil
119+
}
120+
r := githubv4.String(*s)
121+
return &r
122+
}

0 commit comments

Comments
 (0)