From 89e1ee321746e77ac768621bbdbf495ce89518df Mon Sep 17 00:00:00 2001 From: Matthew Gleich Date: Thu, 17 Sep 2020 16:27:23 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20gist=20list=20t?= =?UTF-8?q?o=20use=20graphQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthew Gleich --- pkg/cmd/gist/list/http.go | 87 +++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 17 deletions(-) diff --git a/pkg/cmd/gist/list/http.go b/pkg/cmd/gist/list/http.go index aa75eef25..2474dd5fd 100644 --- a/pkg/cmd/gist/list/http.go +++ b/pkg/cmd/gist/list/http.go @@ -1,41 +1,94 @@ package list import ( - "fmt" "net/http" - "net/url" "sort" + "strings" + "time" "github.com/cli/cli/api" "github.com/cli/cli/pkg/cmd/gist/shared" ) func listGists(client *http.Client, hostname string, limit int, visibility string) ([]shared.Gist, error) { - result := []shared.Gist{} - - query := url.Values{} - if visibility == "all" { - query.Add("per_page", fmt.Sprintf("%d", limit)) - } else { - query.Add("per_page", "100") + type response struct { + Viewer struct { + Gists struct { + Nodes []struct { + Description string + Files []struct { + Name string + Language struct { + Name string + } + Extension string + } + IsPublic bool + Name string + UpdatedAt time.Time + } + } + } + } + + query := ` + query ListGists($visibility: GistPrivacy!, $per_page: Int = 10) { + viewer { + gists(first: $per_page, privacy: $visibility) { + nodes { + name + files { + name + language { + name + } + extension + } + description + updatedAt + isPublic + } + } + } + }` + + if visibility != "all" { + limit = 100 + } + variables := map[string]interface{}{ + "per_page": limit, + "visibility": strings.ToUpper(visibility), } - // TODO switch to graphql apiClient := api.NewClientFromHTTP(client) - err := apiClient.REST(hostname, "GET", "gists?"+query.Encode(), nil, &result) + var result response + err := apiClient.GraphQL(hostname, query, variables, &result) if err != nil { return nil, err } gists := []shared.Gist{} + for _, gist := range result.Viewer.Gists.Nodes { - for _, gist := range result { - if len(gists) == limit { - break - } - if visibility == "all" || (visibility == "secret" && !gist.Public) || (visibility == "public" && gist.Public) { - gists = append(gists, gist) + files := map[string]*shared.GistFile{} + for _, file := range gist.Files { + files[file.Name] = &shared.GistFile{ + Filename: file.Name, + Type: file.Extension, + Language: file.Language.Name, + } } + + gists = append( + gists, + shared.Gist{ + ID: gist.Name, + Description: gist.Description, + Files: files, + UpdatedAt: gist.UpdatedAt, + Public: gist.IsPublic, + }, + ) } sort.SliceStable(gists, func(i, j int) bool {