X Tutup
Skip to content

Commit 53ff384

Browse files
authored
Merge pull request cli#1335 from cli/query-names
Add names to all our GraphQL queries
2 parents 1ddb4d7 + 7a28986 commit 53ff384

17 files changed

+481
-402
lines changed

api/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/henvic/httpretty"
15+
"github.com/shurcooL/graphql"
1516
)
1617

1718
// ClientOption represents an argument to NewClient
@@ -235,6 +236,10 @@ func (c Client) GraphQL(query string, variables map[string]interface{}, data int
235236
return handleResponse(resp, data)
236237
}
237238

239+
func graphQLClient(h *http.Client) *graphql.Client {
240+
return graphql.NewClient("https://api.github.com/graphql", h)
241+
}
242+
238243
// REST performs a REST request and parses the response.
239244
func (c Client) REST(method string, p string, body io.Reader, data interface{}) error {
240245
url := "https://api.github.com/" + p

api/queries_issue.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const fragments = `
8888
// IssueCreate creates an issue in a GitHub repository
8989
func IssueCreate(client *Client, repo *Repository, params map[string]interface{}) (*Issue, error) {
9090
query := `
91-
mutation CreateIssue($input: CreateIssueInput!) {
91+
mutation IssueCreate($input: CreateIssueInput!) {
9292
createIssue(input: $input) {
9393
issue {
9494
url
@@ -140,7 +140,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
140140
}
141141

142142
query := fragments + `
143-
query($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) {
143+
query IssueStatus($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) {
144144
repository(owner: $owner, name: $repo) {
145145
hasIssuesEnabled
146146
assigned: issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) {
@@ -212,7 +212,7 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
212212
}
213213

214214
query := fragments + `
215-
query($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String, $author: String, $mention: String, $milestone: String) {
215+
query IssueList($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String, $author: String, $mention: String, $milestone: String) {
216216
repository(owner: $owner, name: $repo) {
217217
hasIssuesEnabled
218218
issues(first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee, createdBy: $author, mentioned: $mention, milestone: $milestone}) {
@@ -306,7 +306,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
306306
}
307307

308308
query := `
309-
query($owner: String!, $repo: String!, $issue_number: Int!) {
309+
query IssueByNumber($owner: String!, $repo: String!, $issue_number: Int!) {
310310
repository(owner: $owner, name: $repo) {
311311
hasIssuesEnabled
312312
issue(number: $issue_number) {
@@ -383,12 +383,14 @@ func IssueClose(client *Client, repo ghrepo.Interface, issue Issue) error {
383383
} `graphql:"closeIssue(input: $input)"`
384384
}
385385

386-
input := githubv4.CloseIssueInput{
387-
IssueID: issue.ID,
386+
variables := map[string]interface{}{
387+
"input": githubv4.CloseIssueInput{
388+
IssueID: issue.ID,
389+
},
388390
}
389391

390-
v4 := githubv4.NewClient(client.http)
391-
err := v4.Mutate(context.Background(), &mutation, input, nil)
392+
gql := graphQLClient(client.http)
393+
err := gql.MutateNamed(context.Background(), "IssueClose", &mutation, variables)
392394

393395
if err != nil {
394396
return err
@@ -406,12 +408,14 @@ func IssueReopen(client *Client, repo ghrepo.Interface, issue Issue) error {
406408
} `graphql:"reopenIssue(input: $input)"`
407409
}
408410

409-
input := githubv4.ReopenIssueInput{
410-
IssueID: issue.ID,
411+
variables := map[string]interface{}{
412+
"input": githubv4.ReopenIssueInput{
413+
IssueID: issue.ID,
414+
},
411415
}
412416

413-
v4 := githubv4.NewClient(client.http)
414-
err := v4.Mutate(context.Background(), &mutation, input, nil)
417+
gql := graphQLClient(client.http)
418+
err := gql.MutateNamed(context.Background(), "IssueReopen", &mutation, variables)
415419

416420
return err
417421
}

api/queries_org.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func OrganizationProjects(client *Client, owner string) ([]RepoProject, error) {
4747
"endCursor": (*githubv4.String)(nil),
4848
}
4949

50-
v4 := githubv4.NewClient(client.http)
50+
gql := graphQLClient(client.http)
5151

5252
var projects []RepoProject
5353
for {
54-
err := v4.Query(context.Background(), &query, variables)
54+
err := gql.QueryNamed(context.Background(), "OrganizationProjectList", &query, variables)
5555
if err != nil {
5656
return nil, err
5757
}
@@ -90,11 +90,11 @@ func OrganizationTeams(client *Client, owner string) ([]OrgTeam, error) {
9090
"endCursor": (*githubv4.String)(nil),
9191
}
9292

93-
v4 := githubv4.NewClient(client.http)
93+
gql := graphQLClient(client.http)
9494

9595
var teams []OrgTeam
9696
for {
97-
err := v4.Query(context.Background(), &query, variables)
97+
err := gql.QueryNamed(context.Background(), "OrganizationTeamList", &query, variables)
9898
if err != nil {
9999
return nil, err
100100
}

api/queries_pr.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
296296
`
297297

298298
queryPrefix := `
299-
query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
299+
query PullRequestStatus($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
300300
repository(owner: $owner, name: $repo) {
301301
defaultBranchRef { name }
302302
pullRequests(headRefName: $headRefName, first: $per_page, orderBy: { field: CREATED_AT, direction: DESC }) {
@@ -311,7 +311,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
311311
`
312312
if currentPRNumber > 0 {
313313
queryPrefix = `
314-
query($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
314+
query PullRequestStatus($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
315315
repository(owner: $owner, name: $repo) {
316316
defaultBranchRef { name }
317317
pullRequest(number: $number) {
@@ -408,7 +408,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
408408
}
409409

410410
query := `
411-
query($owner: String!, $repo: String!, $pr_number: Int!) {
411+
query PullRequestByNumber($owner: String!, $repo: String!, $pr_number: Int!) {
412412
repository(owner: $owner, name: $repo) {
413413
pullRequest(number: $pr_number) {
414414
id
@@ -518,7 +518,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
518518
}
519519

520520
query := `
521-
query($owner: String!, $repo: String!, $headRefName: String!) {
521+
query PullRequestForBranch($owner: String!, $repo: String!, $headRefName: String!) {
522522
repository(owner: $owner, name: $repo) {
523523
pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
524524
nodes {
@@ -634,7 +634,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
634634
// CreatePullRequest creates a pull request in a GitHub repository
635635
func CreatePullRequest(client *Client, repo *Repository, params map[string]interface{}) (*PullRequest, error) {
636636
query := `
637-
mutation CreatePullRequest($input: CreatePullRequestInput!) {
637+
mutation PullRequestCreate($input: CreatePullRequestInput!) {
638638
createPullRequest(input: $input) {
639639
pullRequest {
640640
id
@@ -681,7 +681,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
681681
}
682682
if len(updateParams) > 0 {
683683
updateQuery := `
684-
mutation UpdatePullRequest($input: UpdatePullRequestInput!) {
684+
mutation PullRequestCreateMetadata($input: UpdatePullRequestInput!) {
685685
updatePullRequest(input: $input) { clientMutationId }
686686
}`
687687
updateParams["pullRequestId"] = pr.ID
@@ -705,7 +705,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
705705

706706
if len(reviewParams) > 0 {
707707
reviewQuery := `
708-
mutation RequestReviews($input: RequestReviewsInput!) {
708+
mutation PullRequestCreateRequestReviews($input: RequestReviewsInput!) {
709709
requestReviews(input: $input) { clientMutationId }
710710
}`
711711
reviewParams["pullRequestId"] = pr.ID
@@ -749,16 +749,16 @@ func AddReview(client *Client, pr *PullRequest, input *PullRequestReviewInput) e
749749
}
750750

751751
body := githubv4.String(input.Body)
752-
753-
gqlInput := githubv4.AddPullRequestReviewInput{
754-
PullRequestID: pr.ID,
755-
Event: &state,
756-
Body: &body,
752+
variables := map[string]interface{}{
753+
"input": githubv4.AddPullRequestReviewInput{
754+
PullRequestID: pr.ID,
755+
Event: &state,
756+
Body: &body,
757+
},
757758
}
758759

759-
v4 := githubv4.NewClient(client.http)
760-
761-
return v4.Mutate(context.Background(), &mutation, gqlInput, nil)
760+
gql := graphQLClient(client.http)
761+
return gql.MutateNamed(context.Background(), "PullRequestReviewAdd", &mutation, variables)
762762
}
763763

764764
func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
@@ -798,7 +798,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P
798798
// If assignee wasn't specified, use `Repository.pullRequest` for ability to
799799
// query by multiple labels
800800
query := fragment + `
801-
query(
801+
query PullRequestList(
802802
$owner: String!,
803803
$repo: String!,
804804
$limit: Int!,
@@ -840,7 +840,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P
840840
// `Repository.pullRequests`, but this mode doesn't support multiple labels
841841
if assignee, ok := vars["assignee"].(string); ok {
842842
query = fragment + `
843-
query(
843+
query PullRequestList(
844844
$q: String!,
845845
$limit: Int!,
846846
$endCursor: String,
@@ -938,12 +938,14 @@ func PullRequestClose(client *Client, repo ghrepo.Interface, pr *PullRequest) er
938938
} `graphql:"closePullRequest(input: $input)"`
939939
}
940940

941-
input := githubv4.ClosePullRequestInput{
942-
PullRequestID: pr.ID,
941+
variables := map[string]interface{}{
942+
"input": githubv4.ClosePullRequestInput{
943+
PullRequestID: pr.ID,
944+
},
943945
}
944946

945-
v4 := githubv4.NewClient(client.http)
946-
err := v4.Mutate(context.Background(), &mutation, input, nil)
947+
gql := graphQLClient(client.http)
948+
err := gql.MutateNamed(context.Background(), "PullRequestClose", &mutation, variables)
947949

948950
return err
949951
}
@@ -957,12 +959,14 @@ func PullRequestReopen(client *Client, repo ghrepo.Interface, pr *PullRequest) e
957959
} `graphql:"reopenPullRequest(input: $input)"`
958960
}
959961

960-
input := githubv4.ReopenPullRequestInput{
961-
PullRequestID: pr.ID,
962+
variables := map[string]interface{}{
963+
"input": githubv4.ReopenPullRequestInput{
964+
PullRequestID: pr.ID,
965+
},
962966
}
963967

964-
v4 := githubv4.NewClient(client.http)
965-
err := v4.Mutate(context.Background(), &mutation, input, nil)
968+
gql := graphQLClient(client.http)
969+
err := gql.MutateNamed(context.Background(), "PullRequestReopen", &mutation, variables)
966970

967971
return err
968972
}
@@ -984,13 +988,15 @@ func PullRequestMerge(client *Client, repo ghrepo.Interface, pr *PullRequest, m
984988
} `graphql:"mergePullRequest(input: $input)"`
985989
}
986990

987-
input := githubv4.MergePullRequestInput{
988-
PullRequestID: pr.ID,
989-
MergeMethod: &mergeMethod,
991+
variables := map[string]interface{}{
992+
"input": githubv4.MergePullRequestInput{
993+
PullRequestID: pr.ID,
994+
MergeMethod: &mergeMethod,
995+
},
990996
}
991997

992-
v4 := githubv4.NewClient(client.http)
993-
err := v4.Mutate(context.Background(), &mutation, input, nil)
998+
gql := graphQLClient(client.http)
999+
err := gql.MutateNamed(context.Background(), "PullRequestMerge", &mutation, variables)
9941000

9951001
return err
9961002
}
@@ -1004,10 +1010,14 @@ func PullRequestReady(client *Client, repo ghrepo.Interface, pr *PullRequest) er
10041010
} `graphql:"markPullRequestReadyForReview(input: $input)"`
10051011
}
10061012

1007-
input := githubv4.MarkPullRequestReadyForReviewInput{PullRequestID: pr.ID}
1013+
variables := map[string]interface{}{
1014+
"input": githubv4.MarkPullRequestReadyForReviewInput{
1015+
PullRequestID: pr.ID,
1016+
},
1017+
}
10081018

1009-
v4 := githubv4.NewClient(client.http)
1010-
return v4.Mutate(context.Background(), &mutation, input, nil)
1019+
gql := graphQLClient(client.http)
1020+
return gql.MutateNamed(context.Background(), "PullRequestReadyForReview", &mutation, variables)
10111021
}
10121022

10131023
func BranchDeleteRemote(client *Client, repo ghrepo.Interface, branch string) error {

api/queries_pr_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func TestBranchDeleteRemote(t *testing.T) {
3131
for _, tt := range tests {
3232
t.Run(tt.name, func(t *testing.T) {
3333
http := &httpmock.Registry{}
34-
http.Register(httpmock.MatchAny, httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody))
34+
http.Register(
35+
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/branch"),
36+
httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody))
3537

3638
client := NewClient(ReplaceTripper(http))
3739
repo, _ := ghrepo.FromFullName("OWNER/REPO")

0 commit comments

Comments
 (0)
X Tutup