From b7c2865d0f021f581ce5d14b18d3e81dd711af30 Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Thu, 18 Feb 2021 17:34:00 -0300 Subject: [PATCH] Remove archived filter from repo list --- pkg/cmd/repo/list/http.go | 116 ++++++++++---------------------------- pkg/cmd/repo/list/list.go | 18 ++---- 2 files changed, 35 insertions(+), 99 deletions(-) diff --git a/pkg/cmd/repo/list/http.go b/pkg/cmd/repo/list/http.go index 3409990c8..4669e72a3 100644 --- a/pkg/cmd/repo/list/http.go +++ b/pkg/cmd/repo/list/http.go @@ -1,7 +1,6 @@ package list import ( - "fmt" "strings" "github.com/cli/cli/api" @@ -14,35 +13,21 @@ type RepositoryList struct { } func listRepos(client *api.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) { - type reposBlock struct { - TotalCount int - RepositoryCount int - Nodes []Repository - PageInfo struct { - HasNextPage bool - EndCursor string - } - } - type response struct { RepositoryOwner struct { - Repositories reposBlock + Repositories struct { + TotalCount int + RepositoryCount int + Nodes []Repository + PageInfo struct { + HasNextPage bool + EndCursor string + } + } } - Search reposBlock } - fragment := ` - fragment repo on Repository { - nameWithOwner - description - isFork - isPrivate - isArchived - updatedAt - }` - - // If `--archived` wasn't specified, use `repositoryOwner.repositores` - query := fragment + ` + query := ` query RepoList($owner: String!, $per_page: Int!, $endCursor: String, $fork: Boolean, $privacy: RepositoryPrivacy) { repositoryOwner(login: $owner) { repositories( @@ -54,7 +39,12 @@ func listRepos(client *api.Client, hostname string, limit int, owner string, fil orderBy: { field: UPDATED_AT, direction: DESC }) { totalCount nodes { - ...repo + nameWithOwner + description + isFork + isPrivate + isArchived + updatedAt } pageInfo { hasNextPage @@ -70,59 +60,23 @@ func listRepos(client *api.Client, hostname string, limit int, owner string, fil } variables := map[string]interface{}{ + "owner": githubv4.String(owner), "per_page": githubv4.Int(perPage), "endCursor": (*githubv4.String)(nil), } - hasArchivedFilter := filter.Archived - - if hasArchivedFilter { - // If `--archived` was specified, use the `search` API rather than - // `repositoryOwner.repositories` - query = fragment + ` - query RepoList($per_page: Int!, $endCursor: String, $query: String!) { - search(first: $per_page, after:$endCursor, type: REPOSITORY, query: $query) { - repositoryCount - nodes { - ... on Repository { - ...repo - } - } - pageInfo { - hasNextPage - endCursor - } - } - }` - - search := []string{fmt.Sprintf("user:%s archived:true fork:true sort:updated-desc", owner)} - - switch filter.Visibility { - case "private": - search = append(search, "is:private") - case "public": - search = append(search, "is:public") - default: - search = append(search, "is:all") - } - - variables["query"] = strings.Join(search, " ") + if filter.Visibility != "" { + variables["privacy"] = githubv4.RepositoryPrivacy(strings.ToUpper(filter.Visibility)) } else { - variables["owner"] = githubv4.String(owner) + variables["privacy"] = (*githubv4.RepositoryPrivacy)(nil) + } - if filter.Visibility != "" { - variables["privacy"] = githubv4.RepositoryPrivacy(strings.ToUpper(filter.Visibility)) - } else { - variables["privacy"] = (*githubv4.RepositoryPrivacy)(nil) - } - - if filter.Fork { - variables["fork"] = githubv4.Boolean(true) - } else if filter.Source { - variables["fork"] = githubv4.Boolean(false) - } else { - variables["fork"] = (*githubv4.Boolean)(nil) - } + if filter.Fork { + variables["fork"] = githubv4.Boolean(true) + } else if filter.Source { + variables["fork"] = githubv4.Boolean(false) + } else { + variables["fork"] = (*githubv4.Boolean)(nil) } var repos []Repository @@ -137,11 +91,7 @@ pagination: return nil, err } - if hasArchivedFilter { - repos = append(repos, result.Search.Nodes...) - } else { - repos = append(repos, result.RepositoryOwner.Repositories.Nodes...) - } + repos = append(repos, result.RepositoryOwner.Repositories.Nodes...) if len(repos) >= limit { if len(repos) > limit { @@ -151,21 +101,13 @@ pagination: } if !result.RepositoryOwner.Repositories.PageInfo.HasNextPage { - if !result.Search.PageInfo.HasNextPage { - break - } + break } variables["endCursor"] = githubv4.String(result.RepositoryOwner.Repositories.PageInfo.EndCursor) - if hasArchivedFilter { - variables["endCursor"] = githubv4.String(result.Search.PageInfo.EndCursor) - } } totalCount = result.RepositoryOwner.Repositories.TotalCount - if hasArchivedFilter { - totalCount = result.Search.RepositoryCount - } listResult := &RepositoryList{ Repositories: repos, diff --git a/pkg/cmd/repo/list/list.go b/pkg/cmd/repo/list/list.go index b066764b3..55a713962 100644 --- a/pkg/cmd/repo/list/list.go +++ b/pkg/cmd/repo/list/list.go @@ -20,7 +20,6 @@ type FilterOptions struct { Visibility string // private, public Fork bool Source bool - Archived bool } type ListOptions struct { @@ -33,7 +32,6 @@ type ListOptions struct { Visibility string Fork bool Source bool - Archived bool } func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { @@ -43,11 +41,10 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman } var ( - flagPublic bool - flagPrivate bool - flagSource bool - flagFork bool - flagArchived bool + flagPublic bool + flagPrivate bool + flagSource bool + flagFork bool ) cmd := &cobra.Command{ @@ -72,7 +69,6 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman opts.Visibility = "public" } - opts.Archived = flagArchived opts.Fork = flagFork opts.Source = flagSource @@ -91,7 +87,6 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().BoolVar(&flagPrivate, "private", false, "Show only private repositories") cmd.Flags().BoolVar(&flagPublic, "public", false, "Show only public repositories") cmd.Flags().BoolVar(&flagSource, "source", false, "Show only source repositories") - cmd.Flags().BoolVar(&flagArchived, "archived", false, "Show only archived repositories") cmd.Flags().BoolVar(&flagFork, "fork", false, "Show only forks") return cmd @@ -119,7 +114,6 @@ func listRun(opts *ListOptions) error { Visibility: opts.Visibility, Fork: opts.Fork, Source: opts.Source, - Archived: opts.Archived, } listResult, err := listRepos(apiClient, ghinstance.OverridableDefault(), opts.Limit, owner, filter) @@ -131,7 +125,7 @@ func listRun(opts *ListOptions) error { tp := utils.NewTablePrinter(opts.IO) - notArchived := (filter.Fork || filter.Source) && !filter.Archived + notArchived := filter.Fork || filter.Source matchCount := len(listResult.Repositories) now := time.Now() @@ -172,7 +166,7 @@ func listRun(opts *ListOptions) error { } if isTerminal { - hasFilters := filter.Visibility != "" || filter.Fork || filter.Source || filter.Archived + hasFilters := filter.Visibility != "" || filter.Fork || filter.Source title := listHeader(owner, matchCount, listResult.TotalCount, hasFilters) fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title) }