diff --git a/app/api/controller/repo/check.go b/app/api/controller/repo/check.go new file mode 100644 index 000000000..144168b38 --- /dev/null +++ b/app/api/controller/repo/check.go @@ -0,0 +1,27 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "context" + + "github.com/harness/gitness/app/auth" +) + +// Check defines the interface for adding extra checks during repository operations. +type Check interface { + // Create allows adding extra check during create repo operations + Create(ctx context.Context, session *auth.Session, in *CreateInput) error +} diff --git a/app/api/controller/repo/controller.go b/app/api/controller/repo/controller.go index 5bbb5feaf..a7ea23160 100644 --- a/app/api/controller/repo/controller.go +++ b/app/api/controller/repo/controller.go @@ -66,6 +66,7 @@ type Controller struct { resourceLimiter limiter.ResourceLimiter mtxManager lock.MutexManager identifierCheck check.RepoIdentifier + repoCheck Check } func NewController( @@ -88,6 +89,7 @@ func NewController( limiter limiter.ResourceLimiter, mtxManager lock.MutexManager, identifierCheck check.RepoIdentifier, + repoCheck Check, ) *Controller { return &Controller{ defaultBranch: config.Git.DefaultBranch, @@ -110,6 +112,7 @@ func NewController( resourceLimiter: limiter, mtxManager: mtxManager, identifierCheck: identifierCheck, + repoCheck: repoCheck, } } diff --git a/app/api/controller/repo/create.go b/app/api/controller/repo/create.go index d7e06e2ee..e44aff627 100644 --- a/app/api/controller/repo/create.go +++ b/app/api/controller/repo/create.go @@ -70,6 +70,11 @@ func (c *Controller) Create(ctx context.Context, session *auth.Session, in *Crea return nil, err } + err = c.repoCheck.Create(ctx, session, in) + if err != nil { + return nil, err + } + var repo *types.Repository err = c.tx.WithTx(ctx, func(ctx context.Context) error { if err := c.resourceLimiter.RepoCount(ctx, parentSpace.ID, 1); err != nil { diff --git a/app/api/controller/repo/no_op_checks.go b/app/api/controller/repo/no_op_checks.go new file mode 100644 index 000000000..e37c1b931 --- /dev/null +++ b/app/api/controller/repo/no_op_checks.go @@ -0,0 +1,34 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "context" + + "github.com/harness/gitness/app/auth" +) + +var _ Check = (*NoOpRepoChecks)(nil) + +type NoOpRepoChecks struct { +} + +func NewNoOpRepoChecks() *NoOpRepoChecks { + return &NoOpRepoChecks{} +} + +func (c *NoOpRepoChecks) Create(_ context.Context, _ *auth.Session, _ *CreateInput) error { + return nil +} diff --git a/app/api/controller/repo/wire.go b/app/api/controller/repo/wire.go index 0a63ffae0..508d2ce63 100644 --- a/app/api/controller/repo/wire.go +++ b/app/api/controller/repo/wire.go @@ -58,10 +58,15 @@ func ProvideController( limiter limiter.ResourceLimiter, mtxManager lock.MutexManager, identifierCheck check.RepoIdentifier, + repoChecks Check, ) *Controller { return NewController(config, tx, urlProvider, authorizer, repoStore, spaceStore, pipelineStore, principalStore, ruleStore, principalInfoCache, protectionManager, - rpcClient, importer, codeOwners, reporeporter, indexer, limiter, mtxManager, identifierCheck) + rpcClient, importer, codeOwners, reporeporter, indexer, limiter, mtxManager, identifierCheck, repoChecks) +} + +func ProvideRepoCheck() Check { + return NewNoOpRepoChecks() } diff --git a/cmd/gitness/wire.go b/cmd/gitness/wire.go index e659bfcef..a18e7668b 100644 --- a/cmd/gitness/wire.go +++ b/cmd/gitness/wire.go @@ -183,6 +183,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e controllerkeywordsearch.WireSet, usergroup.WireSet, openapi.WireSet, + repo.ProvideRepoCheck, ) return &cliserver.System{}, nil } diff --git a/cmd/gitness/wire_gen.go b/cmd/gitness/wire_gen.go index f0093ae9e..a839f61c8 100644 --- a/cmd/gitness/wire_gen.go +++ b/cmd/gitness/wire_gen.go @@ -189,7 +189,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro return nil, err } repoIdentifier := check.ProvideRepoIdentifierCheck() - repoController := repo.ProvideController(config, transactor, provider, authorizer, repoStore, spaceStore, pipelineStore, principalStore, ruleStore, principalInfoCache, protectionManager, gitInterface, repository, codeownersService, reporter, indexer, resourceLimiter, mutexManager, repoIdentifier) + repoCheck := repo.ProvideRepoCheck() + repoController := repo.ProvideController(config, transactor, provider, authorizer, repoStore, spaceStore, pipelineStore, principalStore, ruleStore, principalInfoCache, protectionManager, gitInterface, repository, codeownersService, reporter, indexer, resourceLimiter, mutexManager, repoIdentifier, repoCheck) executionStore := database.ProvideExecutionStore(db) checkStore := database.ProvideCheckStore(db, principalInfoCache) stageStore := database.ProvideStageStore(db)