Skip to content

Commit

Permalink
GraphQL repositories: allow TotalCount for cloned/uncloned/cloneStatus (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnugget authored Sep 7, 2022
1 parent b6ae296 commit c59895a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 73 deletions.
24 changes: 13 additions & 11 deletions cmd/frontend/graphqlbackend/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type repositoryArgs struct {
After *string
}

func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnectionResolver, error) {
func (args *repositoryArgs) toReposListOptions() (database.ReposListOptions, error) {
opt := database.ReposListOptions{
OrderBy: database.RepoListOrderBy{{
Field: ToDBRepoListColumn(args.OrderBy),
Expand All @@ -54,7 +54,7 @@ func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnecti
if args.After != nil {
cursor, err := UnmarshalRepositoryCursor(args.After)
if err != nil {
return nil, err
return opt, err
}
opt.Cursors = append(opt.Cursors, cursor)
} else {
Expand All @@ -70,6 +70,7 @@ func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnecti

opt.Cursors = append(opt.Cursors, &cursor)
}
args.Set(&opt.LimitOffset)

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

args.ConnectionArgs.Set(&opt.LimitOffset)
return opt, nil
}

func (r *schemaResolver) Repositories(args *repositoryArgs) (*repositoryConnectionResolver, error) {
opt, err := args.toReposListOptions()

if err != nil {
return nil, err
}

return &repositoryConnectionResolver{
db: r.db,
Expand Down Expand Up @@ -239,14 +248,6 @@ func (r *repositoryConnectionResolver) TotalCount(ctx context.Context, args *Tot
}
}

i32ptr := func(v int32) *int32 {
return &v
}

if r.opt.NoCloned || r.opt.OnlyCloned || r.opt.CloneStatus != types.CloneStatusUnknown {
// Don't support counting if filtering by clone status.
return nil, nil
}
if !r.indexed || !r.notIndexed {
// Don't support counting if filtering by index status.
return nil, nil
Expand All @@ -269,6 +270,7 @@ func (r *repositoryConnectionResolver) TotalCount(ctx context.Context, args *Tot
}()
}

i32ptr := func(v int32) *int32 { return &v }
count, err := r.db.Repos().Count(ctx, r.opt)
return i32ptr(int32(count)), err
}
Expand Down
145 changes: 83 additions & 62 deletions cmd/frontend/graphqlbackend/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,70 +568,91 @@ func TestRepositories_Integration(t *testing.T) {
}
ctx = actor.WithActor(ctx, actor.FromUser(admin.ID))

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
// cloned only says whether to "Include cloned repositories.", it doesn't exclude non-cloned.
args: "cloned: true",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "cloned: false",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4"},
// Right now we don't produce a totalCount if "cloned: false" is used
wantNoTotalCount: true,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "notCloned: true",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "notCloned: false",
wantRepos: []string{"repo5", "repo6"},
// Right now we don't produce a totalCount if "notCloned" is used
wantNoTotalCount: true,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "failedFetch: true",
wantRepos: []string{"repo2", "repo4", "repo6"},
wantTotalCount: 3,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "failedFetch: false",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
})

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "cloneStatus:NOT_CLONED",
wantRepos: []string{"repo1", "repo2"},
// Right now we don't produce a totalCount if "cloneStatus" is used
wantNoTotalCount: true,
})
tests := []repositoriesQueryTest{
// no args
{
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
},
// first
{
args: "first: 2",
wantRepos: []string{"repo1", "repo2"},
wantTotalCount: 6,
},
// cloned
{
// cloned only says whether to "Include cloned repositories.", it doesn't exclude non-cloned.
args: "cloned: true",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
},
{
args: "cloned: false",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4"},
wantTotalCount: 4,
},
{
args: "cloned: false, first: 2",
wantRepos: []string{"repo1", "repo2"},
wantTotalCount: 4,
},
// notCloned
{
args: "notCloned: true",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
},
{
args: "notCloned: false",
wantRepos: []string{"repo5", "repo6"},
wantTotalCount: 2,
},
// failedFetch
{
args: "failedFetch: true",
wantRepos: []string{"repo2", "repo4", "repo6"},
wantTotalCount: 3,
},
{
args: "failedFetch: true, first: 2",
wantRepos: []string{"repo2", "repo4"},
wantTotalCount: 3,
},
{
args: "failedFetch: false",
wantRepos: []string{"repo1", "repo2", "repo3", "repo4", "repo5", "repo6"},
wantTotalCount: 6,
},
// cloneStatus
{
args: "cloneStatus:NOT_CLONED",
wantRepos: []string{"repo1", "repo2"},
wantTotalCount: 2,
},
{
args: "cloneStatus:CLONING",
wantRepos: []string{"repo3", "repo4"},
wantTotalCount: 2,
},
{
args: "cloneStatus:CLONED",
wantRepos: []string{"repo5", "repo6"},
wantTotalCount: 2,
},
{
args: "cloneStatus:NOT_CLONED, first: 1",
wantRepos: []string{"repo1"},
wantTotalCount: 2,
},
}

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "cloneStatus:CLONING",
wantRepos: []string{"repo3", "repo4"},
// Right now we don't produce a totalCount if "cloneStatus" is used
wantNoTotalCount: true,
})
for _, tt := range tests {
t.Run(tt.args, func(t *testing.T) {
runRepositoriesQuery(t, ctx, schema, tt)
})
}

runRepositoriesQuery(t, ctx, schema, repositoriesQueryTest{
args: "cloneStatus:CLONED",
wantRepos: []string{"repo5", "repo6"},
// Right now we don't produce a totalCount if "cloneStatus" is used
wantNoTotalCount: true,
})
}

type repositoriesQueryTest struct {
Expand Down

0 comments on commit c59895a

Please sign in to comment.