Skip to content

Commit

Permalink
Accept pull requests in issue edit argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Nov 23, 2021
1 parent b75e705 commit c8d5a6b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 150 deletions.
16 changes: 0 additions & 16 deletions api/queries_issue.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package api

import (
"context"
"fmt"
"time"

"github.com/cli/cli/v2/internal/ghrepo"
"github.com/shurcooL/githubv4"
)

type IssuesPayload struct {
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 0 additions & 14 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
70 changes: 5 additions & 65 deletions pkg/cmd/issue/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
}
61 changes: 6 additions & 55 deletions pkg/cmd/pr/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
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 {
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.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
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
122 changes: 122 additions & 0 deletions pkg/cmd/pr/shared/editable_http.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c8d5a6b

Please sign in to comment.