forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
88 lines (77 loc) · 1.88 KB
/
http.go
File metadata and controls
88 lines (77 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package list
import (
"context"
"net/http"
"strings"
"time"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/pkg/cmd/gist/shared"
"github.com/shurcooL/githubv4"
"github.com/shurcooL/graphql"
)
func listGists(client *http.Client, hostname string, limit int, visibility string) ([]shared.Gist, error) {
type response struct {
Viewer struct {
Gists struct {
Nodes []struct {
Description string
Files []struct {
Name string
}
IsPublic bool
Name string
UpdatedAt time.Time
}
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"gists(first: $per_page, after: $endCursor, privacy: $visibility, orderBy: {field: CREATED_AT, direction: DESC})"`
}
}
perPage := limit
if perPage > 100 {
perPage = 100
}
variables := map[string]interface{}{
"per_page": githubv4.Int(perPage),
"endCursor": (*githubv4.String)(nil),
"visibility": githubv4.GistPrivacy(strings.ToUpper(visibility)),
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(hostname), client)
gists := []shared.Gist{}
pagination:
for {
var result response
err := gql.QueryNamed(context.Background(), "GistList", &result, variables)
if err != nil {
return nil, err
}
for _, gist := range result.Viewer.Gists.Nodes {
files := map[string]*shared.GistFile{}
for _, file := range gist.Files {
files[file.Name] = &shared.GistFile{
Filename: file.Name,
}
}
gists = append(
gists,
shared.Gist{
ID: gist.Name,
Description: gist.Description,
Files: files,
UpdatedAt: gist.UpdatedAt,
Public: gist.IsPublic,
},
)
if len(gists) == limit {
break pagination
}
}
if !result.Viewer.Gists.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(result.Viewer.Gists.PageInfo.EndCursor)
}
return gists, nil
}