X Tutup
Skip to content

Commit 89e1ee3

Browse files
committed
♻️ Refactor gist list to use graphQL
Signed-off-by: Matthew Gleich <matthewgleich@gmail.com>
1 parent 458aace commit 89e1ee3

File tree

1 file changed

+69
-16
lines changed

1 file changed

+69
-16
lines changed

pkg/cmd/gist/list/http.go

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,94 @@
11
package list
22

33
import (
4-
"fmt"
54
"net/http"
6-
"net/url"
75
"sort"
6+
"strings"
7+
"time"
88

99
"github.com/cli/cli/api"
1010
"github.com/cli/cli/pkg/cmd/gist/shared"
1111
)
1212

1313
func listGists(client *http.Client, hostname string, limit int, visibility string) ([]shared.Gist, error) {
14-
result := []shared.Gist{}
14+
type response struct {
15+
Viewer struct {
16+
Gists struct {
17+
Nodes []struct {
18+
Description string
19+
Files []struct {
20+
Name string
21+
Language struct {
22+
Name string
23+
}
24+
Extension string
25+
}
26+
IsPublic bool
27+
Name string
28+
UpdatedAt time.Time
29+
}
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
50+
}
51+
}
52+
}
53+
}`
1554

16-
query := url.Values{}
17-
if visibility == "all" {
18-
query.Add("per_page", fmt.Sprintf("%d", limit))
19-
} else {
20-
query.Add("per_page", "100")
55+
if visibility != "all" {
56+
limit = 100
57+
}
58+
variables := map[string]interface{}{
59+
"per_page": limit,
60+
"visibility": strings.ToUpper(visibility),
2161
}
2262

23-
// TODO switch to graphql
2463
apiClient := api.NewClientFromHTTP(client)
25-
err := apiClient.REST(hostname, "GET", "gists?"+query.Encode(), nil, &result)
64+
var result response
65+
err := apiClient.GraphQL(hostname, query, variables, &result)
2666
if err != nil {
2767
return nil, err
2868
}
2969

3070
gists := []shared.Gist{}
71+
for _, gist := range result.Viewer.Gists.Nodes {
3172

32-
for _, gist := range result {
33-
if len(gists) == limit {
34-
break
35-
}
36-
if visibility == "all" || (visibility == "secret" && !gist.Public) || (visibility == "public" && gist.Public) {
37-
gists = append(gists, gist)
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,
79+
}
3880
}
81+
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+
)
3992
}
4093

4194
sort.SliceStable(gists, func(i, j int) bool {

0 commit comments

Comments
 (0)
X Tutup