Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit c59895a

Browse files
authored
GraphQL repositories: allow TotalCount for cloned/uncloned/cloneStatus (#41370)
1 parent b6ae296 commit c59895a

File tree

2 files changed

+96
-73
lines changed

2 files changed

+96
-73
lines changed

cmd/frontend/graphqlbackend/repositories.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type repositoryArgs struct {
3838
After *string
3939
}
4040

41-
func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnectionResolver, error) {
41+
func (args *repositoryArgs) toReposListOptions() (database.ReposListOptions, error) {
4242
opt := database.ReposListOptions{
4343
OrderBy: database.RepoListOrderBy{{
4444
Field: ToDBRepoListColumn(args.OrderBy),
@@ -54,7 +54,7 @@ func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnecti
5454
if args.After != nil {
5555
cursor, err := UnmarshalRepositoryCursor(args.After)
5656
if err != nil {
57-
return nil, err
57+
return opt, err
5858
}
5959
opt.Cursors = append(opt.Cursors, cursor)
6060
} else {
@@ -70,6 +70,7 @@ func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnecti
7070

7171
opt.Cursors = append(opt.Cursors, &cursor)
7272
}
73+
args.Set(&opt.LimitOffset)
7374

7475
if args.CloneStatus != nil {
7576
opt.CloneStatus = types.ParseCloneStatusFromGraphQL(*args.CloneStatus)
@@ -86,7 +87,15 @@ func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnecti
8687
opt.OnlyCloned = true
8788
}
8889

89-
args.ConnectionArgs.Set(&opt.LimitOffset)
90+
return opt, nil
91+
}
92+
93+
func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnectionResolver, error) {
94+
opt, err := args.toReposListOptions()
95+
96+
if err != nil {
97+
return nil, err
98+
}
9099

91100
return &repositoryConnectionResolver{
92101
db: r.db,
@@ -239,14 +248,6 @@ func (r *repositoryConnectionResolver) TotalCount(ctx context.Context, args *Tot
239248
}
240249
}
241250

242-
i32ptr := func(v int32) *int32 {
243-
return &v
244-
}
245-
246-
if r.opt.NoCloned || r.opt.OnlyCloned || r.opt.CloneStatus != types.CloneStatusUnknown {
247-
// Don't support counting if filtering by clone status.
248-
return nil, nil
249-
}
250251
if !r.indexed || !r.notIndexed {
251252
// Don't support counting if filtering by index status.
252253
return nil, nil
@@ -269,6 +270,7 @@ func (r *repositoryConnectionResolver) TotalCount(ctx context.Context, args *Tot
269270
}()
270271
}
271272

273+
i32ptr := func(v int32) *int32 { return &v }
272274
count, err := r.db.Repos().Count(ctx, r.opt)
273275
return i32ptr(int32(count)), err
274276
}

cmd/frontend/graphqlbackend/repositories_test.go

+83-62
Original file line numberDiff line numberDiff line change
@@ -568,70 +568,91 @@ func TestRepositories_Integration(t *testing.T) {
568568
}
569569
ctx = actor.WithActor(ctx, actor.FromUser(admin.ID))
570570

571-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
572-
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
573-
wantTotalCount: 6,
574-
})
575-
576-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
577-
// cloned only says whether to "Include cloned repositories.", it doesn't exclude non-cloned.
578-
args: "cloned: true",
579-
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
580-
wantTotalCount: 6,
581-
})
582-
583-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
584-
args: "cloned: false",
585-
wantRepos: []string{"repo1", "repo2", "repo3", "repo4"},
586-
// Right now we don't produce a totalCount if "cloned: false" is used
587-
wantNoTotalCount: true,
588-
})
589-
590-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
591-
args: "notCloned: true",
592-
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
593-
wantTotalCount: 6,
594-
})
595-
596-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
597-
args: "notCloned: false",
598-
wantRepos: []string{"repo5", "repo6"},
599-
// Right now we don't produce a totalCount if "notCloned" is used
600-
wantNoTotalCount: true,
601-
})
602-
603-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
604-
args: "failedFetch: true",
605-
wantRepos: []string{"repo2", "repo4", "repo6"},
606-
wantTotalCount: 3,
607-
})
608-
609-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
610-
args: "failedFetch: false",
611-
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
612-
wantTotalCount: 6,
613-
})
614-
615-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
616-
args: "cloneStatus:NOT_CLONED",
617-
wantRepos: []string{"repo1", "repo2"},
618-
// Right now we don't produce a totalCount if "cloneStatus" is used
619-
wantNoTotalCount: true,
620-
})
571+
tests := []repositoriesQueryTest{
572+
// no args
573+
{
574+
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
575+
wantTotalCount: 6,
576+
},
577+
// first
578+
{
579+
args: "first: 2",
580+
wantRepos: []string{"repo1", "repo2"},
581+
wantTotalCount: 6,
582+
},
583+
// cloned
584+
{
585+
// cloned only says whether to "Include cloned repositories.", it doesn't exclude non-cloned.
586+
args: "cloned: true",
587+
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
588+
wantTotalCount: 6,
589+
},
590+
{
591+
args: "cloned: false",
592+
wantRepos: []string{"repo1", "repo2", "repo3", "repo4"},
593+
wantTotalCount: 4,
594+
},
595+
{
596+
args: "cloned: false, first: 2",
597+
wantRepos: []string{"repo1", "repo2"},
598+
wantTotalCount: 4,
599+
},
600+
// notCloned
601+
{
602+
args: "notCloned: true",
603+
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
604+
wantTotalCount: 6,
605+
},
606+
{
607+
args: "notCloned: false",
608+
wantRepos: []string{"repo5", "repo6"},
609+
wantTotalCount: 2,
610+
},
611+
// failedFetch
612+
{
613+
args: "failedFetch: true",
614+
wantRepos: []string{"repo2", "repo4", "repo6"},
615+
wantTotalCount: 3,
616+
},
617+
{
618+
args: "failedFetch: true, first: 2",
619+
wantRepos: []string{"repo2", "repo4"},
620+
wantTotalCount: 3,
621+
},
622+
{
623+
args: "failedFetch: false",
624+
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
625+
wantTotalCount: 6,
626+
},
627+
// cloneStatus
628+
{
629+
args: "cloneStatus:NOT_CLONED",
630+
wantRepos: []string{"repo1", "repo2"},
631+
wantTotalCount: 2,
632+
},
633+
{
634+
args: "cloneStatus:CLONING",
635+
wantRepos: []string{"repo3", "repo4"},
636+
wantTotalCount: 2,
637+
},
638+
{
639+
args: "cloneStatus:CLONED",
640+
wantRepos: []string{"repo5", "repo6"},
641+
wantTotalCount: 2,
642+
},
643+
{
644+
args: "cloneStatus:NOT_CLONED, first: 1",
645+
wantRepos: []string{"repo1"},
646+
wantTotalCount: 2,
647+
},
648+
}
621649

622-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
623-
args: "cloneStatus:CLONING",
624-
wantRepos: []string{"repo3", "repo4"},
625-
// Right now we don't produce a totalCount if "cloneStatus" is used
626-
wantNoTotalCount: true,
627-
})
650+
for _, tt := range tests {
651+
t.Run(tt.args, func(t *testing.T) {
652+
runRepositoriesQuery(t, ctx, schema, tt)
653+
})
654+
}
628655

629-
runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
630-
args: "cloneStatus:CLONED",
631-
wantRepos: []string{"repo5", "repo6"},
632-
// Right now we don't produce a totalCount if "cloneStatus" is used
633-
wantNoTotalCount: true,
634-
})
635656
}
636657

637658
type repositoriesQueryTest struct {

0 commit comments

Comments
 (0)