11package list
22
33import (
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
1315func 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}
0 commit comments