From c8d5a6be020423cd9bd91026021e9be6fb6712db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 23 Nov 2021 20:39:19 +0100 Subject: [PATCH] Accept pull requests in `issue edit` argument --- api/queries_issue.go | 16 ---- api/queries_pr.go | 14 ---- pkg/cmd/issue/edit/edit.go | 70 ++--------------- pkg/cmd/pr/edit/edit.go | 61 ++------------- pkg/cmd/pr/shared/editable_http.go | 122 +++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 150 deletions(-) create mode 100644 pkg/cmd/pr/shared/editable_http.go diff --git a/api/queries_issue.go b/api/queries_issue.go index 262334251..813212276 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -1,12 +1,10 @@ package api import ( - "context" "fmt" "time" "github.com/cli/cli/v2/internal/ghrepo" - "github.com/shurcooL/githubv4" ) type IssuesPayload struct { @@ -342,20 +340,6 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e return &resp.Repository.Issue, nil } -func IssueUpdate(client *Client, repo ghrepo.Interface, params githubv4.UpdateIssueInput) error { - var mutation struct { - UpdateIssue struct { - Issue struct { - ID string - } - } `graphql:"updateIssue(input: $input)"` - } - variables := map[string]interface{}{"input": params} - gql := graphQLClient(client.http, repo.RepoHost()) - err := gql.MutateNamed(context.Background(), "IssueUpdate", &mutation, variables) - return err -} - func (i Issue) Link() string { return i.URL } diff --git a/api/queries_pr.go b/api/queries_pr.go index 4635bc1b0..300846c23 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -621,20 +621,6 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter return pr, nil } -func UpdatePullRequest(client *Client, repo ghrepo.Interface, params githubv4.UpdatePullRequestInput) error { - var mutation struct { - UpdatePullRequest struct { - PullRequest struct { - ID string - } - } `graphql:"updatePullRequest(input: $input)"` - } - variables := map[string]interface{}{"input": params} - gql := graphQLClient(client.http, repo.RepoHost()) - err := gql.MutateNamed(context.Background(), "PullRequestUpdate", &mutation, variables) - return err -} - func UpdatePullRequestReviews(client *Client, repo ghrepo.Interface, params githubv4.RequestReviewsInput) error { var mutation struct { RequestReviews struct { diff --git a/pkg/cmd/issue/edit/edit.go b/pkg/cmd/issue/edit/edit.go index 433141583..4f6b07c4f 100644 --- a/pkg/cmd/issue/edit/edit.go +++ b/pkg/cmd/issue/edit/edit.go @@ -11,7 +11,6 @@ import ( prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/shurcooL/githubv4" "github.com/spf13/cobra" ) @@ -130,14 +129,15 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman return cmd } +var lookupFields = []string{"id", "number", "title", "body", "assignees", "labels", "projectCards", "milestone", "url"} + func editRun(opts *EditOptions) error { httpClient, err := opts.HttpClient() if err != nil { return err } - apiClient := api.NewClientFromHTTP(httpClient) - issue, repo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg) + issue, repo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.SelectorArg, lookupFields) if err != nil { return err } @@ -159,6 +159,7 @@ func editRun(opts *EditOptions) error { } } + apiClient := api.NewClientFromHTTP(httpClient) opts.IO.StartProgressIndicator() err = opts.FetchOptions(apiClient, repo, &editable) opts.IO.StopProgressIndicator() @@ -178,7 +179,7 @@ func editRun(opts *EditOptions) error { } opts.IO.StartProgressIndicator() - err = updateIssue(apiClient, repo, issue.ID, editable) + err = prShared.UpdateIssue(httpClient, repo, issue.ID, issue.IsPullRequest(), editable) opts.IO.StopProgressIndicator() if err != nil { return err @@ -188,64 +189,3 @@ func editRun(opts *EditOptions) error { return nil } - -func updateIssue(client *api.Client, repo ghrepo.Interface, id string, options prShared.Editable) error { - var err error - params := githubv4.UpdateIssueInput{ - ID: id, - Title: ghString(options.TitleValue()), - Body: ghString(options.BodyValue()), - } - assigneeIds, err := options.AssigneeIds(client, repo) - if err != nil { - return err - } - params.AssigneeIDs = ghIds(assigneeIds) - labelIds, err := options.LabelIds() - if err != nil { - return err - } - params.LabelIDs = ghIds(labelIds) - projectIds, err := options.ProjectIds() - if err != nil { - return err - } - params.ProjectIDs = ghIds(projectIds) - milestoneId, err := options.MilestoneId() - if err != nil { - return err - } - params.MilestoneID = ghId(milestoneId) - return api.IssueUpdate(client, repo, params) -} - -func ghIds(s *[]string) *[]githubv4.ID { - if s == nil { - return nil - } - ids := make([]githubv4.ID, len(*s)) - for i, v := range *s { - ids[i] = v - } - return &ids -} - -func ghId(s *string) *githubv4.ID { - if s == nil { - return nil - } - if *s == "" { - r := githubv4.ID(nil) - return &r - } - r := githubv4.ID(*s) - return &r -} - -func ghString(s *string) *githubv4.String { - if s == nil { - return nil - } - r := githubv4.String(*s) - return &r -} diff --git a/pkg/cmd/pr/edit/edit.go b/pkg/cmd/pr/edit/edit.go index 17f35a2ee..f69a70e79 100644 --- a/pkg/cmd/pr/edit/edit.go +++ b/pkg/cmd/pr/edit/edit.go @@ -202,7 +202,7 @@ func editRun(opts *EditOptions) error { } opts.IO.StartProgressIndicator() - err = updatePullRequest(apiClient, repo, pr.ID, editable) + err = updatePullRequest(httpClient, repo, pr.ID, editable) opts.IO.StopProgressIndicator() if err != nil { return err @@ -213,44 +213,14 @@ func editRun(opts *EditOptions) error { return nil } -func updatePullRequest(client *api.Client, repo ghrepo.Interface, id string, editable shared.Editable) error { - var err error - params := githubv4.UpdatePullRequestInput{ - PullRequestID: id, - Title: ghString(editable.TitleValue()), - Body: ghString(editable.BodyValue()), - } - assigneeIds, err := editable.AssigneeIds(client, repo) - if err != nil { +func updatePullRequest(httpClient *http.Client, repo ghrepo.Interface, id string, editable shared.Editable) error { + if err := shared.UpdateIssue(httpClient, repo, id, true, editable); err != nil { return err } - params.AssigneeIDs = ghIds(assigneeIds) - labelIds, err := editable.LabelIds() - if err != nil { - return err - } - params.LabelIDs = ghIds(labelIds) - projectIds, err := editable.ProjectIds() - if err != nil { - return err - } - params.ProjectIDs = ghIds(projectIds) - milestoneId, err := editable.MilestoneId() - if err != nil { - return err - } - params.MilestoneID = ghId(milestoneId) - if editable.Base.Edited { - params.BaseRefName = ghString(&editable.Base.Value) - } - err = api.UpdatePullRequest(client, repo, params) - if err != nil { - return err - } - return updatePullRequestReviews(client, repo, id, editable) + return updatePullRequestReviews(httpClient, repo, id, editable) } -func updatePullRequestReviews(client *api.Client, repo ghrepo.Interface, id string, editable shared.Editable) error { +func updatePullRequestReviews(httpClient *http.Client, repo ghrepo.Interface, id string, editable shared.Editable) error { if !editable.Reviewers.Edited { return nil } @@ -265,6 +235,7 @@ func updatePullRequestReviews(client *api.Client, repo ghrepo.Interface, id stri UserIDs: ghIds(userIds), TeamIDs: ghIds(teamIds), } + client := api.NewClientFromHTTP(httpClient) return api.UpdatePullRequestReviews(client, repo, reviewsRequestParams) } @@ -315,23 +286,3 @@ func ghIds(s *[]string) *[]githubv4.ID { } return &ids } - -func ghId(s *string) *githubv4.ID { - if s == nil { - return nil - } - if *s == "" { - r := githubv4.ID(nil) - return &r - } - r := githubv4.ID(*s) - return &r -} - -func ghString(s *string) *githubv4.String { - if s == nil { - return nil - } - r := githubv4.String(*s) - return &r -} diff --git a/pkg/cmd/pr/shared/editable_http.go b/pkg/cmd/pr/shared/editable_http.go new file mode 100644 index 000000000..8af23c6c3 --- /dev/null +++ b/pkg/cmd/pr/shared/editable_http.go @@ -0,0 +1,122 @@ +package shared + +import ( + "context" + "net/http" + + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/ghinstance" + "github.com/cli/cli/v2/internal/ghrepo" + graphql "github.com/cli/shurcooL-graphql" + "github.com/shurcooL/githubv4" +) + +func UpdateIssue(httpClient *http.Client, repo ghrepo.Interface, id string, isPR bool, options Editable) error { + title := ghString(options.TitleValue()) + body := ghString(options.BodyValue()) + + apiClient := api.NewClientFromHTTP(httpClient) + assigneeIds, err := options.AssigneeIds(apiClient, repo) + if err != nil { + return err + } + + labelIds, err := options.LabelIds() + if err != nil { + return err + } + + projectIds, err := options.ProjectIds() + if err != nil { + return err + } + + milestoneId, err := options.MilestoneId() + if err != nil { + return err + } + + if isPR { + params := githubv4.UpdatePullRequestInput{ + PullRequestID: id, + Title: title, + Body: body, + AssigneeIDs: ghIds(assigneeIds), + LabelIDs: ghIds(labelIds), + ProjectIDs: ghIds(projectIds), + MilestoneID: ghId(milestoneId), + } + if options.Base.Edited { + params.BaseRefName = ghString(&options.Base.Value) + } + return updatePullRequest(httpClient, repo, params) + } + + return updateIssue(httpClient, repo, githubv4.UpdateIssueInput{ + ID: id, + Title: title, + Body: body, + AssigneeIDs: ghIds(assigneeIds), + LabelIDs: ghIds(labelIds), + ProjectIDs: ghIds(projectIds), + MilestoneID: ghId(milestoneId), + }) +} + +func updateIssue(httpClient *http.Client, repo ghrepo.Interface, params githubv4.UpdateIssueInput) error { + var mutation struct { + UpdateIssue struct { + Issue struct { + ID string + } + } `graphql:"updateIssue(input: $input)"` + } + variables := map[string]interface{}{"input": params} + gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient) + return gql.MutateNamed(context.Background(), "IssueUpdate", &mutation, variables) +} + +func updatePullRequest(httpClient *http.Client, repo ghrepo.Interface, params githubv4.UpdatePullRequestInput) error { + var mutation struct { + UpdatePullRequest struct { + PullRequest struct { + ID string + } + } `graphql:"updatePullRequest(input: $input)"` + } + variables := map[string]interface{}{"input": params} + gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient) + err := gql.MutateNamed(context.Background(), "PullRequestUpdate", &mutation, variables) + return err +} + +func ghIds(s *[]string) *[]githubv4.ID { + if s == nil { + return nil + } + ids := make([]githubv4.ID, len(*s)) + for i, v := range *s { + ids[i] = v + } + return &ids +} + +func ghId(s *string) *githubv4.ID { + if s == nil { + return nil + } + if *s == "" { + r := githubv4.ID(nil) + return &r + } + r := githubv4.ID(*s) + return &r +} + +func ghString(s *string) *githubv4.String { + if s == nil { + return nil + } + r := githubv4.String(*s) + return &r +}