-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accept pull requests in
issue edit
argument
- Loading branch information
Showing
5 changed files
with
133 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |