Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd2adfe

Browse files
committed
Port listing gists to githubv4 GraphQL client
- Fetching more than 100 gists is now supported - The GraphQL query name is now `GistList` instead of `ListGists` for consistency with other queries - Avoid fetching unnecessary Files fields - Gists are now rendered in the order that the API returned them in - The gist timestamp for machine-readable output is now rendered in RFC3339 format instead of in `time.Time.String()` format which is only meant for debugging and is not considered stable - Ensure newlines in gist description are rendered as spaces
1 parent 1859728 commit cd2adfe

File tree

3 files changed

+122
-128
lines changed

3 files changed

+122
-128
lines changed

pkg/cmd/gist/list/http.go

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package list
22

33
import (
4+
"context"
45
"net/http"
5-
"sort"
66
"strings"
77
"time"
88

9-
"github.com/cli/cli/api"
9+
"github.com/cli/cli/internal/ghinstance"
1010
"github.com/cli/cli/pkg/cmd/gist/shared"
11+
"github.com/shurcooL/githubv4"
12+
"github.com/shurcooL/graphql"
1113
)
1214

1315
func listGists(client *http.Client, hostname string, limit int, visibility string) ([]shared.Gist, error) {
@@ -17,83 +19,70 @@ func listGists(client *http.Client, hostname string, limit int, visibility strin
1719
Nodes []struct {
1820
Description string
1921
Files []struct {
20-
Name string
21-
Language struct {
22-
Name string
23-
}
24-
Extension string
22+
Name string
2523
}
2624
IsPublic bool
2725
Name string
2826
UpdatedAt time.Time
2927
}
30-
}
31-
}
32-
}
33-
34-
query := `
35-
query ListGists($visibility: GistPrivacy!, $per_page: Int = 10) {
36-
viewer {
37-
gists(first: $per_page, privacy: $visibility) {
38-
nodes {
39-
name
40-
files {
41-
name
42-
language {
43-
name
44-
}
45-
extension
46-
}
47-
description
48-
updatedAt
49-
isPublic
28+
PageInfo struct {
29+
HasNextPage bool
30+
EndCursor string
5031
}
51-
}
32+
} `graphql:"gists(first: $per_page, after: $endCursor, privacy: $visibility, orderBy: {field: CREATED_AT, direction: DESC})"`
5233
}
53-
}`
34+
}
5435

55-
if visibility != "all" {
56-
limit = 100
36+
perPage := limit
37+
if perPage > 100 {
38+
perPage = 100
5739
}
40+
5841
variables := map[string]interface{}{
59-
"per_page": limit,
60-
"visibility": strings.ToUpper(visibility),
42+
"per_page": githubv4.Int(perPage),
43+
"endCursor": (*githubv4.String)(nil),
44+
"visibility": githubv4.GistPrivacy(strings.ToUpper(visibility)),
6145
}
6246

63-
apiClient := api.NewClientFromHTTP(client)
64-
var result response
65-
err := apiClient.GraphQL(hostname, query, variables, &result)
66-
if err != nil {
67-
return nil, err
68-
}
47+
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(hostname), client)
6948

7049
gists := []shared.Gist{}
71-
for _, gist := range result.Viewer.Gists.Nodes {
50+
pagination:
51+
for {
52+
var result response
53+
err := gql.QueryNamed(context.Background(), "GistList", &result, variables)
54+
if err != nil {
55+
return nil, err
56+
}
7257

73-
files := map[string]*shared.GistFile{}
74-
for _, file := range gist.Files {
75-
files[file.Name] = &shared.GistFile{
76-
Filename: file.Name,
77-
Type: file.Extension,
78-
Language: file.Language.Name,
58+
for _, gist := range result.Viewer.Gists.Nodes {
59+
files := map[string]*shared.GistFile{}
60+
for _, file := range gist.Files {
61+
files[file.Name] = &shared.GistFile{
62+
Filename: file.Name,
63+
}
64+
}
65+
66+
gists = append(
67+
gists,
68+
shared.Gist{
69+
ID: gist.Name,
70+
Description: gist.Description,
71+
Files: files,
72+
UpdatedAt: gist.UpdatedAt,
73+
Public: gist.IsPublic,
74+
},
75+
)
76+
if len(gists) == limit {
77+
break pagination
7978
}
8079
}
8180

82-
gists = append(
83-
gists,
84-
shared.Gist{
85-
ID: gist.Name,
86-
Description: gist.Description,
87-
Files: files,
88-
UpdatedAt: gist.UpdatedAt,
89-
Public: gist.IsPublic,
90-
},
91-
)
81+
if !result.Viewer.Gists.PageInfo.HasNextPage {
82+
break
83+
}
84+
variables["endCursor"] = githubv4.String(result.Viewer.Gists.PageInfo.EndCursor)
9285
}
9386

94-
sort.SliceStable(gists, func(i, j int) bool {
95-
return gists[i].UpdatedAt.After(gists[j].UpdatedAt)
96-
})
97-
9887
return gists, nil
9988
}

pkg/cmd/gist/list/list.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/cli/cli/internal/ghinstance"
1010
"github.com/cli/cli/pkg/cmdutil"
1111
"github.com/cli/cli/pkg/iostreams"
12+
"github.com/cli/cli/pkg/text"
1213
"github.com/cli/cli/utils"
1314
"github.com/spf13/cobra"
1415
)
@@ -76,10 +77,7 @@ func listRun(opts *ListOptions) error {
7677
tp := utils.NewTablePrinter(opts.IO)
7778

7879
for _, gist := range gists {
79-
fileCount := 0
80-
for range gist.Files {
81-
fileCount++
82-
}
80+
fileCount := len(gist.Files)
8381

8482
visibility := "public"
8583
visColor := cs.Green
@@ -98,16 +96,16 @@ func listRun(opts *ListOptions) error {
9896
}
9997
}
10098

99+
gistTime := gist.UpdatedAt.Format(time.RFC3339)
100+
if tp.IsTTY() {
101+
gistTime = utils.FuzzyAgo(time.Since(gist.UpdatedAt))
102+
}
103+
101104
tp.AddField(gist.ID, nil, nil)
102-
tp.AddField(description, nil, cs.Bold)
105+
tp.AddField(text.ReplaceExcessiveWhitespace(description), nil, cs.Bold)
103106
tp.AddField(utils.Pluralize(fileCount, "file"), nil, nil)
104107
tp.AddField(visibility, nil, visColor)
105-
if tp.IsTTY() {
106-
updatedAt := utils.FuzzyAgo(time.Since(gist.UpdatedAt))
107-
tp.AddField(updatedAt, nil, cs.Gray)
108-
} else {
109-
tp.AddField(gist.UpdatedAt.String(), nil, nil)
110-
}
108+
tp.AddField(gistTime, nil, cs.Gray)
111109
tp.EndRow()
112110
}
113111

0 commit comments

Comments
 (0)