feat: add repochecks (#1132)

This commit is contained in:
Abhinav Singh 2024-03-26 22:22:30 +00:00 committed by Harness
parent cecfecdb06
commit 96f35b6e01
7 changed files with 78 additions and 2 deletions

View File

@ -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
}

View File

@ -66,6 +66,7 @@ type Controller struct {
resourceLimiter limiter.ResourceLimiter resourceLimiter limiter.ResourceLimiter
mtxManager lock.MutexManager mtxManager lock.MutexManager
identifierCheck check.RepoIdentifier identifierCheck check.RepoIdentifier
repoCheck Check
} }
func NewController( func NewController(
@ -88,6 +89,7 @@ func NewController(
limiter limiter.ResourceLimiter, limiter limiter.ResourceLimiter,
mtxManager lock.MutexManager, mtxManager lock.MutexManager,
identifierCheck check.RepoIdentifier, identifierCheck check.RepoIdentifier,
repoCheck Check,
) *Controller { ) *Controller {
return &Controller{ return &Controller{
defaultBranch: config.Git.DefaultBranch, defaultBranch: config.Git.DefaultBranch,
@ -110,6 +112,7 @@ func NewController(
resourceLimiter: limiter, resourceLimiter: limiter,
mtxManager: mtxManager, mtxManager: mtxManager,
identifierCheck: identifierCheck, identifierCheck: identifierCheck,
repoCheck: repoCheck,
} }
} }

View File

@ -70,6 +70,11 @@ func (c *Controller) Create(ctx context.Context, session *auth.Session, in *Crea
return nil, err return nil, err
} }
err = c.repoCheck.Create(ctx, session, in)
if err != nil {
return nil, err
}
var repo *types.Repository var repo *types.Repository
err = c.tx.WithTx(ctx, func(ctx context.Context) error { err = c.tx.WithTx(ctx, func(ctx context.Context) error {
if err := c.resourceLimiter.RepoCount(ctx, parentSpace.ID, 1); err != nil { if err := c.resourceLimiter.RepoCount(ctx, parentSpace.ID, 1); err != nil {

View File

@ -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
}

View File

@ -58,10 +58,15 @@ func ProvideController(
limiter limiter.ResourceLimiter, limiter limiter.ResourceLimiter,
mtxManager lock.MutexManager, mtxManager lock.MutexManager,
identifierCheck check.RepoIdentifier, identifierCheck check.RepoIdentifier,
repoChecks Check,
) *Controller { ) *Controller {
return NewController(config, tx, urlProvider, return NewController(config, tx, urlProvider,
authorizer, repoStore, authorizer, repoStore,
spaceStore, pipelineStore, spaceStore, pipelineStore,
principalStore, ruleStore, principalInfoCache, protectionManager, 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()
} }

View File

@ -183,6 +183,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
controllerkeywordsearch.WireSet, controllerkeywordsearch.WireSet,
usergroup.WireSet, usergroup.WireSet,
openapi.WireSet, openapi.WireSet,
repo.ProvideRepoCheck,
) )
return &cliserver.System{}, nil return &cliserver.System{}, nil
} }

View File

@ -189,7 +189,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
return nil, err return nil, err
} }
repoIdentifier := check.ProvideRepoIdentifierCheck() 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) executionStore := database.ProvideExecutionStore(db)
checkStore := database.ProvideCheckStore(db, principalInfoCache) checkStore := database.ProvideCheckStore(db, principalInfoCache)
stageStore := database.ProvideStageStore(db) stageStore := database.ProvideStageStore(db)