mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 14:51:44 +00:00
frontend: Return error early when user added repos exceed limit (#19920)
This commit is contained in:
parent
bfa26c3fa3
commit
32f10a208b
@ -9,6 +9,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
|
||||
"github.com/sourcegraph/sourcegraph/internal/conf"
|
||||
"github.com/sourcegraph/sourcegraph/internal/database"
|
||||
"github.com/sourcegraph/sourcegraph/internal/timeutil"
|
||||
)
|
||||
@ -23,7 +24,8 @@ func (r *schemaResolver) SetExternalServiceRepos(ctx context.Context, args struc
|
||||
return nil, err
|
||||
}
|
||||
|
||||
es, err := database.GlobalExternalServices.GetByID(ctx, id)
|
||||
extsvcStore := database.ExternalServices(r.db)
|
||||
es, err := extsvcStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -46,6 +48,12 @@ func (r *schemaResolver) SetExternalServiceRepos(ctx context.Context, args struc
|
||||
var repos []string
|
||||
if args.Repos != nil {
|
||||
repos = *args.Repos
|
||||
// If we know the number of repos up front we can ensure that they don't exceed
|
||||
// their limit before hitting the code host
|
||||
maxAllowed := conf.UserReposMaxPerUser()
|
||||
if es.NamespaceUserID != 0 && len(repos) > maxAllowed {
|
||||
return nil, errors.Errorf("Too many repositories, %d. Sourcegraph supports adding a maximum of %d repositories.", len(repos), maxAllowed)
|
||||
}
|
||||
}
|
||||
err = ra.SetRepos(args.AllRepos, repos)
|
||||
if err != nil {
|
||||
@ -62,7 +70,7 @@ func (r *schemaResolver) SetExternalServiceRepos(ctx context.Context, args struc
|
||||
es.NextSyncAt = time.Time{}
|
||||
es.UpdatedAt = timeutil.Now()
|
||||
|
||||
err = database.GlobalExternalServices.Upsert(ctx, es)
|
||||
err = extsvcStore.Upsert(ctx, es)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -383,3 +383,19 @@ func GitMaxCodehostRequestsPerSecond() int {
|
||||
}
|
||||
return *val
|
||||
}
|
||||
|
||||
func UserReposMaxPerUser() int {
|
||||
v := Get().UserReposMaxPerUser
|
||||
if v == 0 {
|
||||
return 2000
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func UserReposMaxPerSite() int {
|
||||
v := Get().UserReposMaxPerSite
|
||||
if v == 0 {
|
||||
return 200000
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@ -21,19 +21,3 @@ func ConfRepoConcurrentExternalServiceSyncers() int {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func ConfUserReposMaxPerUser() int {
|
||||
v := conf.Get().UserReposMaxPerUser
|
||||
if v == 0 {
|
||||
return 2000
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func ConfUserReposMaxPerSite() int {
|
||||
v := conf.Get().UserReposMaxPerSite
|
||||
if v == 0 {
|
||||
return 200000
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@ -174,7 +174,7 @@ func (s *Syncer) SyncExternalService(ctx context.Context, tx *Store, externalSer
|
||||
// If we are over our limit for user added repos we abort the sync
|
||||
totalAllowed := uint64(s.UserReposMaxPerSite)
|
||||
if totalAllowed == 0 {
|
||||
totalAllowed = uint64(ConfUserReposMaxPerSite())
|
||||
totalAllowed = uint64(conf.UserReposMaxPerSite())
|
||||
}
|
||||
userAdded, err := tx.CountUserAddedRepos(ctx)
|
||||
if err != nil {
|
||||
@ -189,7 +189,7 @@ func (s *Syncer) SyncExternalService(ctx context.Context, tx *Store, externalSer
|
||||
var sourcedRepoCount int64
|
||||
maxAllowed := s.UserReposMaxPerUser
|
||||
if maxAllowed == 0 {
|
||||
maxAllowed = ConfUserReposMaxPerUser()
|
||||
maxAllowed = conf.UserReposMaxPerUser()
|
||||
}
|
||||
onSourced = func(r *types.Repo) error {
|
||||
newCount := atomic.AddInt64(&sourcedRepoCount, 1)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user