X Tutup
Skip to content

Commit e27a77f

Browse files
committed
Add ability to filter by archived in repo list
Like `--language`, archived filters also use the Search API.
1 parent 5da8301 commit e27a77f

File tree

4 files changed

+164
-64
lines changed

4 files changed

+164
-64
lines changed

pkg/cmd/repo/list/http.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,20 @@ type RepositoryList struct {
4444
Owner string
4545
Repositories []Repository
4646
TotalCount int
47+
FromSearch bool
4748
}
4849

4950
type FilterOptions struct {
50-
Visibility string // private, public
51-
Fork bool
52-
Source bool
53-
Language string
51+
Visibility string // private, public
52+
Fork bool
53+
Source bool
54+
Language string
55+
Archived bool
56+
NonArchived bool
5457
}
5558

5659
func listRepos(client *http.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) {
57-
if filter.Language != "" {
60+
if filter.Language != "" || filter.Archived || filter.NonArchived {
5861
return searchRepos(client, hostname, limit, owner, filter)
5962
}
6063

@@ -165,7 +168,7 @@ func searchRepos(client *http.Client, hostname string, limit int, owner string,
165168
}
166169

167170
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(hostname), client)
168-
listResult := RepositoryList{}
171+
listResult := RepositoryList{FromSearch: true}
169172
pagination:
170173
for {
171174
var result query
@@ -222,5 +225,11 @@ func searchQuery(owner string, filter FilterOptions) string {
222225
queryParts = append(queryParts, "is:private")
223226
}
224227

228+
if filter.Archived {
229+
queryParts = append(queryParts, "archived:true")
230+
} else if filter.NonArchived {
231+
queryParts = append(queryParts, "archived:false")
232+
}
233+
225234
return strings.Join(queryParts, " ")
226235
}

pkg/cmd/repo/list/http_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func Test_listReposWithLanguage(t *testing.T) {
5252
require.NoError(t, err)
5353

5454
assert.Equal(t, 3, res.TotalCount)
55+
assert.Equal(t, true, res.FromSearch)
5556
assert.Equal(t, "octocat", res.Owner)
5657
assert.Equal(t, "octocat/hello-world", res.Repositories[0].NameWithOwner)
5758

@@ -130,6 +131,26 @@ func Test_searchQuery(t *testing.T) {
130131
},
131132
want: "sort:updated-desc user:@me fork:true language:\"ruby\"",
132133
},
134+
{
135+
name: "only archived",
136+
args: args{
137+
owner: "",
138+
filter: FilterOptions{
139+
Archived: true,
140+
},
141+
},
142+
want: "sort:updated-desc user:@me fork:true archived:true",
143+
},
144+
{
145+
name: "only non-archived",
146+
args: args{
147+
owner: "",
148+
filter: FilterOptions{
149+
NonArchived: true,
150+
},
151+
},
152+
want: "sort:updated-desc user:@me fork:true archived:false",
153+
},
133154
}
134155
for _, tt := range tests {
135156
t.Run(tt.name, func(t *testing.T) {

pkg/cmd/repo/list/list.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ type ListOptions struct {
2020
Limit int
2121
Owner string
2222

23-
Visibility string
24-
Fork bool
25-
Source bool
26-
Language string
23+
Visibility string
24+
Fork bool
25+
Source bool
26+
Language string
27+
Archived bool
28+
NonArchived bool
2729

2830
Now func() time.Time
2931
}
@@ -55,6 +57,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
5557
if opts.Source && opts.Fork {
5658
return &cmdutil.FlagError{Err: fmt.Errorf("specify only one of `--source` or `--fork`")}
5759
}
60+
if opts.Archived && opts.NonArchived {
61+
return &cmdutil.FlagError{Err: fmt.Errorf("specify only one of `--archived` or `--no-archived`")}
62+
}
5863

5964
if flagPrivate {
6065
opts.Visibility = "private"
@@ -79,6 +84,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
7984
cmd.Flags().BoolVar(&opts.Source, "source", false, "Show only non-forks")
8085
cmd.Flags().BoolVar(&opts.Fork, "fork", false, "Show only forks")
8186
cmd.Flags().StringVarP(&opts.Language, "language", "l", "", "Filter by primary coding language")
87+
cmd.Flags().BoolVar(&opts.Archived, "archived", false, "Show only archived repositories")
88+
cmd.Flags().BoolVar(&opts.NonArchived, "no-archived", false, "Omit archived repositories")
8289

8390
return cmd
8491
}
@@ -90,10 +97,12 @@ func listRun(opts *ListOptions) error {
9097
}
9198

9299
filter := FilterOptions{
93-
Visibility: opts.Visibility,
94-
Fork: opts.Fork,
95-
Source: opts.Source,
96-
Language: opts.Language,
100+
Visibility: opts.Visibility,
101+
Fork: opts.Fork,
102+
Source: opts.Source,
103+
Language: opts.Language,
104+
Archived: opts.Archived,
105+
NonArchived: opts.NonArchived,
97106
}
98107

99108
listResult, err := listRepos(httpClient, ghinstance.OverridableDefault(), opts.Limit, opts.Owner, filter)
@@ -117,13 +126,18 @@ func listRun(opts *ListOptions) error {
117126
infoColor = cs.Yellow
118127
}
119128

129+
t := repo.PushedAt
130+
// if listResult.FromSearch {
131+
// t = repo.UpdatedAt
132+
// }
133+
120134
tp.AddField(repo.NameWithOwner, nil, cs.Bold)
121135
tp.AddField(text.ReplaceExcessiveWhitespace(repo.Description), nil, nil)
122136
tp.AddField(info, nil, infoColor)
123137
if tp.IsTTY() {
124-
tp.AddField(utils.FuzzyAgoAbbr(now, repo.PushedAt), nil, cs.Gray)
138+
tp.AddField(utils.FuzzyAgoAbbr(now, t), nil, cs.Gray)
125139
} else {
126-
tp.AddField(repo.PushedAt.Format(time.RFC3339), nil, nil)
140+
tp.AddField(t.Format(time.RFC3339), nil, nil)
127141
}
128142
tp.EndRow()
129143
}

pkg/cmd/repo/list/list_test.go

Lines changed: 104 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,96 +28,140 @@ func TestNewCmdList(t *testing.T) {
2828
name: "no arguments",
2929
cli: "",
3030
wants: ListOptions{
31-
Limit: 30,
32-
Owner: "",
33-
Visibility: "",
34-
Fork: false,
35-
Source: false,
36-
Language: "",
31+
Limit: 30,
32+
Owner: "",
33+
Visibility: "",
34+
Fork: false,
35+
Source: false,
36+
Language: "",
37+
Archived: false,
38+
NonArchived: false,
3739
},
3840
},
3941
{
4042
name: "with owner",
4143
cli: "monalisa",
4244
wants: ListOptions{
43-
Limit: 30,
44-
Owner: "monalisa",
45-
Visibility: "",
46-
Fork: false,
47-
Source: false,
48-
Language: "",
45+
Limit: 30,
46+
Owner: "monalisa",
47+
Visibility: "",
48+
Fork: false,
49+
Source: false,
50+
Language: "",
51+
Archived: false,
52+
NonArchived: false,
4953
},
5054
},
5155
{
5256
name: "with limit",
5357
cli: "-L 101",
5458
wants: ListOptions{
55-
Limit: 101,
56-
Owner: "",
57-
Visibility: "",
58-
Fork: false,
59-
Source: false,
60-
Language: "",
59+
Limit: 101,
60+
Owner: "",
61+
Visibility: "",
62+
Fork: false,
63+
Source: false,
64+
Language: "",
65+
Archived: false,
66+
NonArchived: false,
6167
},
6268
},
6369
{
6470
name: "only public",
6571
cli: "--public",
6672
wants: ListOptions{
67-
Limit: 30,
68-
Owner: "",
69-
Visibility: "public",
70-
Fork: false,
71-
Source: false,
72-
Language: "",
73+
Limit: 30,
74+
Owner: "",
75+
Visibility: "public",
76+
Fork: false,
77+
Source: false,
78+
Language: "",
79+
Archived: false,
80+
NonArchived: false,
7381
},
7482
},
7583
{
7684
name: "only private",
7785
cli: "--private",
7886
wants: ListOptions{
79-
Limit: 30,
80-
Owner: "",
81-
Visibility: "private",
82-
Fork: false,
83-
Source: false,
84-
Language: "",
87+
Limit: 30,
88+
Owner: "",
89+
Visibility: "private",
90+
Fork: false,
91+
Source: false,
92+
Language: "",
93+
Archived: false,
94+
NonArchived: false,
8595
},
8696
},
8797
{
8898
name: "only forks",
8999
cli: "--fork",
90100
wants: ListOptions{
91-
Limit: 30,
92-
Owner: "",
93-
Visibility: "",
94-
Fork: true,
95-
Source: false,
96-
Language: "",
101+
Limit: 30,
102+
Owner: "",
103+
Visibility: "",
104+
Fork: true,
105+
Source: false,
106+
Language: "",
107+
Archived: false,
108+
NonArchived: false,
97109
},
98110
},
99111
{
100112
name: "only sources",
101113
cli: "--source",
102114
wants: ListOptions{
103-
Limit: 30,
104-
Owner: "",
105-
Visibility: "",
106-
Fork: false,
107-
Source: true,
108-
Language: "",
115+
Limit: 30,
116+
Owner: "",
117+
Visibility: "",
118+
Fork: false,
119+
Source: true,
120+
Language: "",
121+
Archived: false,
122+
NonArchived: false,
109123
},
110124
},
111125
{
112126
name: "with language",
113127
cli: "-l go",
114128
wants: ListOptions{
115-
Limit: 30,
116-
Owner: "",
117-
Visibility: "",
118-
Fork: false,
119-
Source: false,
120-
Language: "go",
129+
Limit: 30,
130+
Owner: "",
131+
Visibility: "",
132+
Fork: false,
133+
Source: false,
134+
Language: "go",
135+
Archived: false,
136+
NonArchived: false,
137+
},
138+
},
139+
{
140+
name: "only archived",
141+
cli: "--archived",
142+
wants: ListOptions{
143+
Limit: 30,
144+
Owner: "",
145+
Visibility: "",
146+
Fork: false,
147+
Source: false,
148+
Language: "",
149+
Archived: true,
150+
NonArchived: false,
151+
},
152+
},
153+
{
154+
name: "only non-archived",
155+
cli: "--no-archived",
156+
wants: ListOptions{
157+
Limit: 30,
158+
Owner: "",
159+
Visibility: "",
160+
Fork: false,
161+
Source: false,
162+
Language: "",
163+
Archived: false,
164+
NonArchived: true,
121165
},
122166
},
123167
{
@@ -130,11 +174,21 @@ func TestNewCmdList(t *testing.T) {
130174
cli: "--fork --source",
131175
wantsErr: "specify only one of `--source` or `--fork`",
132176
},
177+
{
178+
name: "conflicting archived",
179+
cli: "--archived --no-archived",
180+
wantsErr: "specify only one of `--archived` or `--no-archived`",
181+
},
133182
{
134183
name: "too many arguments",
135184
cli: "monalisa hubot",
136185
wantsErr: "accepts at most 1 arg(s), received 2",
137186
},
187+
{
188+
name: "invalid limit",
189+
cli: "-L 0",
190+
wantsErr: "invalid limit: 0",
191+
},
138192
}
139193

140194
for _, tt := range tests {
@@ -166,6 +220,8 @@ func TestNewCmdList(t *testing.T) {
166220
assert.Equal(t, tt.wants.Visibility, gotOpts.Visibility)
167221
assert.Equal(t, tt.wants.Fork, gotOpts.Fork)
168222
assert.Equal(t, tt.wants.Source, gotOpts.Source)
223+
assert.Equal(t, tt.wants.Archived, gotOpts.Archived)
224+
assert.Equal(t, tt.wants.NonArchived, gotOpts.NonArchived)
169225
})
170226
}
171227
}

0 commit comments

Comments
 (0)
X Tutup