- 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
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
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
|
|
}
|