diff --git a/api/GrpcHandler.go b/api/GrpcHandler.go index 43b7bbe7..6e6350c5 100644 --- a/api/GrpcHandler.go +++ b/api/GrpcHandler.go @@ -2,7 +2,7 @@ package api import ( "context" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/devtron-labs/git-sensor/pkg" "github.com/devtron-labs/git-sensor/pkg/git" pb "github.com/devtron-labs/protos/gitSensor" @@ -355,6 +355,7 @@ func (impl *GrpcHandlerImpl) GetCommitMetadataForPipelineMaterial(ctx context.Co return nil, err } + if res == nil { res1 := &pb.GitCommit{} return res1, nil diff --git a/api/GrpcHandler_test.go b/api/GrpcHandler_test.go index ccd33709..b3f80ff0 100644 --- a/api/GrpcHandler_test.go +++ b/api/GrpcHandler_test.go @@ -3,7 +3,7 @@ package api import ( "context" "encoding/json" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/devtron-labs/git-sensor/pkg" "github.com/devtron-labs/git-sensor/pkg/git" "github.com/devtron-labs/git-sensor/pkg/mocks" diff --git a/api/RestHandler.go b/api/RestHandler.go index 2cfccd4f..2c83edf5 100644 --- a/api/RestHandler.go +++ b/api/RestHandler.go @@ -18,7 +18,7 @@ package api import ( "encoding/json" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/devtron-labs/git-sensor/pkg" "github.com/devtron-labs/git-sensor/pkg/git" "github.com/gorilla/mux" diff --git a/App.go b/app/App.go similarity index 98% rename from App.go rename to app/App.go index 2d0cd012..95a0d104 100644 --- a/App.go +++ b/app/App.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package main +package app import ( "context" @@ -24,7 +24,7 @@ import ( pubsub "github.com/devtron-labs/common-lib/pubsub-lib" "github.com/devtron-labs/git-sensor/api" "github.com/devtron-labs/git-sensor/bean" - "github.com/devtron-labs/git-sensor/internal/middleware" + "github.com/devtron-labs/git-sensor/internals/middleware" "github.com/devtron-labs/git-sensor/pkg/git" pb "github.com/devtron-labs/protos/gitSensor" "github.com/go-pg/pg" diff --git a/internal/Configuration.go b/internals/Configuration.go similarity index 87% rename from internal/Configuration.go rename to internals/Configuration.go index 997cb557..ae559c2d 100644 --- a/internal/Configuration.go +++ b/internals/Configuration.go @@ -1,13 +1,12 @@ -package internal +package internals -import ( - "github.com/caarlos0/env" -) +import "github.com/caarlos0/env" type Configuration struct { CommitStatsTimeoutInSec int `env:"COMMIT_STATS_TIMEOUT_IN_SEC" envDefault:"2"` EnableFileStats bool `env:"ENABLE_FILE_STATS" envDefault:"false"` GitHistoryCount int `env:"GIT_HISTORY_COUNT" envDefault:"15"` + CloningMode string `env:"CLONING_MODE" envDefault:"FULL"` MinLimit int `env:"MIN_LIMIT_FOR_PVC" envDefault:"1"` // in MB UseGitCli bool `env:"USE_GIT_CLI" envDefault:"false"` AnalyticsDebug bool `env:"ANALYTICS_DEBUG" envDefault:"false"` diff --git a/internal/Lockutil.go b/internals/Lockutil.go similarity index 99% rename from internal/Lockutil.go rename to internals/Lockutil.go index d5b56b66..e850fd8a 100644 --- a/internal/Lockutil.go +++ b/internals/Lockutil.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal +package internals import ( "go.uber.org/zap" diff --git a/internal/logger/logger.go b/internals/logger/logger.go similarity index 100% rename from internal/logger/logger.go rename to internals/logger/logger.go diff --git a/internal/middleware/delegator.go b/internals/middleware/delegator.go similarity index 100% rename from internal/middleware/delegator.go rename to internals/middleware/delegator.go diff --git a/internal/middleware/instrument.go b/internals/middleware/instrument.go similarity index 100% rename from internal/middleware/instrument.go rename to internals/middleware/instrument.go diff --git a/internal/queueManager/LocalWorker.go b/internals/queueManager/LocalWorker.go similarity index 100% rename from internal/queueManager/LocalWorker.go rename to internals/queueManager/LocalWorker.go diff --git a/internal/sql/CiPipelineMaterial.go b/internals/sql/CiPipelineMaterial.go similarity index 100% rename from internal/sql/CiPipelineMaterial.go rename to internals/sql/CiPipelineMaterial.go diff --git a/internal/sql/GitMaterial.go b/internals/sql/GitMaterial.go similarity index 100% rename from internal/sql/GitMaterial.go rename to internals/sql/GitMaterial.go diff --git a/internal/sql/GitProviderRepository.go b/internals/sql/GitProviderRepository.go similarity index 100% rename from internal/sql/GitProviderRepository.go rename to internals/sql/GitProviderRepository.go diff --git a/internal/sql/WebhookEventDataMappingFilterResultRepository.go b/internals/sql/WebhookEventDataMappingFilterResultRepository.go similarity index 100% rename from internal/sql/WebhookEventDataMappingFilterResultRepository.go rename to internals/sql/WebhookEventDataMappingFilterResultRepository.go diff --git a/internal/sql/WebhookEventDataMappingRepository.go b/internals/sql/WebhookEventDataMappingRepository.go similarity index 100% rename from internal/sql/WebhookEventDataMappingRepository.go rename to internals/sql/WebhookEventDataMappingRepository.go diff --git a/internal/sql/WebhookEventParsedDataRepository.go b/internals/sql/WebhookEventParsedDataRepository.go similarity index 100% rename from internal/sql/WebhookEventParsedDataRepository.go rename to internals/sql/WebhookEventParsedDataRepository.go diff --git a/internal/sql/WebhookEventRepository.go b/internals/sql/WebhookEventRepository.go similarity index 100% rename from internal/sql/WebhookEventRepository.go rename to internals/sql/WebhookEventRepository.go diff --git a/internal/sql/connection.go b/internals/sql/connection.go similarity index 100% rename from internal/sql/connection.go rename to internals/sql/connection.go diff --git a/internal/util/HelperUtil.go b/internals/util/HelperUtil.go similarity index 93% rename from internal/util/HelperUtil.go rename to internals/util/HelperUtil.go index 051391d5..278ae8dc 100644 --- a/internal/util/HelperUtil.go +++ b/internals/util/HelperUtil.go @@ -1,7 +1,7 @@ package util import ( - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "strings" ) diff --git a/pkg/RepoManages.go b/pkg/RepoManages.go index 7a70a702..706403a2 100644 --- a/pkg/RepoManages.go +++ b/pkg/RepoManages.go @@ -20,9 +20,9 @@ import ( "encoding/json" "errors" "fmt" - "github.com/devtron-labs/git-sensor/internal" - "github.com/devtron-labs/git-sensor/internal/sql" - "github.com/devtron-labs/git-sensor/internal/util" + "github.com/devtron-labs/git-sensor/internals" + "github.com/devtron-labs/git-sensor/internals/sql" + "github.com/devtron-labs/git-sensor/internals/util" "github.com/devtron-labs/git-sensor/pkg/git" _ "github.com/robfig/cron/v3" "go.uber.org/zap" @@ -58,15 +58,15 @@ type RepoManagerImpl struct { repositoryManagerAnalytics git.RepositoryManagerAnalytics gitProviderRepository sql.GitProviderRepository ciPipelineMaterialRepository sql.CiPipelineMaterialRepository - locker *internal.RepositoryLocker + locker *internals.RepositoryLocker gitWatcher git.GitWatcher webhookEventRepository sql.WebhookEventRepository webhookEventParsedDataRepository sql.WebhookEventParsedDataRepository webhookEventDataMappingRepository sql.WebhookEventDataMappingRepository webhookEventDataMappingFilterResultRepository sql.WebhookEventDataMappingFilterResultRepository webhookEventBeanConverter git.WebhookEventBeanConverter - configuration *internal.Configuration - gitManager git.GitManagerImpl + configuration *internals.Configuration + gitManager git.GitManager } func NewRepoManagerImpl( @@ -76,14 +76,14 @@ func NewRepoManagerImpl( repositoryManagerAnalytics git.RepositoryManagerAnalytics, gitProviderRepository sql.GitProviderRepository, ciPipelineMaterialRepository sql.CiPipelineMaterialRepository, - locker *internal.RepositoryLocker, + locker *internals.RepositoryLocker, gitWatcher git.GitWatcher, webhookEventRepository sql.WebhookEventRepository, webhookEventParsedDataRepository sql.WebhookEventParsedDataRepository, webhookEventDataMappingRepository sql.WebhookEventDataMappingRepository, webhookEventDataMappingFilterResultRepository sql.WebhookEventDataMappingFilterResultRepository, webhookEventBeanConverter git.WebhookEventBeanConverter, - configuration *internal.Configuration, - gitManager git.GitManagerImpl, + configuration *internals.Configuration, + gitManager git.GitManager, ) *RepoManagerImpl { return &RepoManagerImpl{ logger: logger, @@ -195,6 +195,9 @@ func (impl RepoManagerImpl) updatePipelineMaterialCommit(gitCtx git.GitContext, continue } + gitCtx = gitCtx.WithCredentials(material.GitProvider.UserName, material.GitProvider.Password). + WithCloningMode(impl.configuration.CloningMode) + commits, err := impl.repositoryManager.ChangesSince(gitCtx, material.CheckoutLocation, pipelineMaterial.Value, "", "", impl.configuration.GitHistoryCount) //commits, err := impl.FetchChanges(pipelineMaterial.Id, "", "", 0) if err == nil { @@ -345,15 +348,18 @@ func (impl RepoManagerImpl) checkoutMaterial(gitCtx git.GitContext, material *sq if err != nil { return material, nil } - checkoutPath, err := git.GetLocationForMaterial(material) + + gitCtx = gitCtx.WithCredentials(userName, password). + WithCloningMode(impl.configuration.CloningMode) + + checkoutPath, checkoutLocationForFetching, err := impl.repositoryManager.GetCheckoutPathAndLocation(gitCtx, material, gitProvider.Url) if err != nil { return material, err } - gitCtx = gitCtx.WithCredentials(userName, password) err = impl.repositoryManager.Add(gitCtx, material.GitProviderId, checkoutPath, material.Url, gitProvider.AuthMode, gitProvider.SshPrivateKey) if err == nil { - material.CheckoutLocation = checkoutPath + material.CheckoutLocation = checkoutLocationForFetching material.CheckoutStatus = true } else { material.CheckoutStatus = false @@ -627,7 +633,10 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(gitCtx git.GitContext, pipe }() userName, password, err := git.GetUserNamePassword(gitMaterial.GitProvider) - gitCtx = gitCtx.WithCredentials(userName, password) + + gitCtx = gitCtx.WithCredentials(userName, password). + WithCloningMode(impl.configuration.CloningMode) + updated, repo, err := impl.repositoryManager.Fetch(gitCtx, gitMaterial.Url, gitMaterial.CheckoutLocation) if !updated { impl.logger.Warn("repository is up to date") @@ -659,7 +668,7 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(gitCtx git.GitContext, pipe return nil, err } - commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, branchName, "", "", 1) + commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, branchName, "", "", 1, gitMaterial.CheckoutLocation) if commits == nil { return nil, err @@ -694,6 +703,8 @@ func (impl RepoManagerImpl) GetCommitMetadataForPipelineMaterial(gitCtx git.GitC return nil, err } + gitCtx = gitCtx.WithCredentials(gitMaterial.GitProvider.UserName, gitMaterial.GitProvider.Password). + WithCloningMode(impl.configuration.CloningMode) // validate checkout status of gitMaterial if !gitMaterial.CheckoutStatus { impl.logger.Errorw("checkout not success", "gitMaterialId", gitMaterialId) @@ -707,7 +718,6 @@ func (impl RepoManagerImpl) GetCommitMetadataForPipelineMaterial(gitCtx git.GitC repoLock.Mutex.Unlock() impl.locker.ReturnLocker(gitMaterial.Id) }() - commits, err := impl.repositoryManager.ChangesSince(gitCtx, gitMaterial.CheckoutLocation, branchName, "", gitHash, 1) if err != nil { impl.logger.Errorw("error while fetching commit info", "pipelineMaterialId", pipelineMaterialId, "gitHash", gitHash, "err", err) @@ -747,6 +757,10 @@ func (impl RepoManagerImpl) GetReleaseChanges(gitCtx git.GitContext, request *Re repoLock.Mutex.Unlock() impl.locker.ReturnLocker(gitMaterial.Id) }() + + gitCtx = gitCtx.WithCredentials(gitMaterial.GitProvider.UserName, gitMaterial.GitProvider.Password). + WithCloningMode(impl.configuration.CloningMode) + gitChanges, err := impl.repositoryManagerAnalytics.ChangesSinceByRepositoryForAnalytics(gitCtx, gitMaterial.CheckoutLocation, request.OldCommit, request.NewCommit) if err != nil { impl.logger.Errorw("error in computing changes", "req", request, "err", err) diff --git a/pkg/git/Bean.go b/pkg/git/Bean.go index 9aaea416..be3f19cb 100644 --- a/pkg/git/Bean.go +++ b/pkg/git/Bean.go @@ -17,7 +17,7 @@ package git import ( - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing/object" "io" diff --git a/pkg/git/GitBaseManager.go b/pkg/git/GitBaseManager.go index 59fa1af9..55e49803 100644 --- a/pkg/git/GitBaseManager.go +++ b/pkg/git/GitBaseManager.go @@ -5,8 +5,8 @@ import ( "encoding/json" "errors" "fmt" - "github.com/devtron-labs/git-sensor/internal" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/devtron-labs/git-sensor/util" "go.uber.org/zap" "os" @@ -29,8 +29,6 @@ type GitManager interface { OpenRepoPlain(checkoutPath string) (*GitRepository, error) // Init initializes a git repo Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error - // FetchDiffStatBetweenCommits returns the file stats reponse on executing git action - FetchDiffStatBetweenCommits(gitCtx GitContext, oldHash string, newHash string, rootDir string) (response, errMsg string, err error) } // GitManagerBase Base methods which will be available to all implementation of the parent interface @@ -43,16 +41,19 @@ type GitManagerBase interface { Checkout(gitCtx GitContext, rootDir, branch string) (response, errMsg string, err error) // ConfigureSshCommand configures ssh in git repo ConfigureSshCommand(gitCtx GitContext, rootDir string, sshPrivateKeyPath string) (response, errMsg string, err error) + // FetchDiffStatBetweenCommits returns the file stats reponse on executing git action + FetchDiffStatBetweenCommits(gitCtx GitContext, oldHash string, newHash string, rootDir string) (response, errMsg string, err error) // LogMergeBase get the commit diff between using a merge base strategy LogMergeBase(gitCtx GitContext, rootDir, from string, to string) ([]*Commit, error) + ExecuteCustomCommand(gitContext GitContext, name string, arg ...string) (response, errMsg string, err error) } type GitManagerBaseImpl struct { logger *zap.SugaredLogger - conf *internal.Configuration + conf *internals.Configuration commandTimeoutMap map[string]int } -func NewGitManagerBaseImpl(logger *zap.SugaredLogger, config *internal.Configuration) *GitManagerBaseImpl { +func NewGitManagerBaseImpl(logger *zap.SugaredLogger, config *internals.Configuration) *GitManagerBaseImpl { commandTimeoutMap, err := parseCmdTimeoutJson(config) if err != nil { @@ -62,52 +63,36 @@ func NewGitManagerBaseImpl(logger *zap.SugaredLogger, config *internal.Configura return &GitManagerBaseImpl{logger: logger, conf: config, commandTimeoutMap: commandTimeoutMap} } -func parseCmdTimeoutJson(config *internal.Configuration) (map[string]int, error) { - commandTimeoutMap := make(map[string]int) - var err error - if config.CliCmdTimeoutJson != "" { - err = json.Unmarshal([]byte(config.CliCmdTimeoutJson), &commandTimeoutMap) - } - return commandTimeoutMap, err -} - type GitManagerImpl struct { GitManager } -func NewGitManagerImpl(configuration *internal.Configuration, - cliGitManager GitCliManager, - goGitManager GoGitSDKManager) GitManagerImpl { +func NewGitManagerImpl(logger *zap.SugaredLogger, configuration *internals.Configuration) *GitManagerImpl { + baseImpl := NewGitManagerBaseImpl(logger, configuration) if configuration.UseGitCli { - return GitManagerImpl{cliGitManager} + return &GitManagerImpl{ + GitManager: NewGitCliManagerImpl(baseImpl, logger), + } + + } + return &GitManagerImpl{ + GitManager: NewGoGitSDKManagerImpl(baseImpl, logger), } - return GitManagerImpl{goGitManager} } -func (impl *GitManagerImpl) OpenNewRepo(gitCtx GitContext, location string, url string) (*GitRepository, error) { - - r, err := impl.OpenRepoPlain(location) - if err != nil { - err = os.RemoveAll(location) - if err != nil { - return r, fmt.Errorf("error in cleaning checkout path: %s", err) - } - err = impl.Init(gitCtx, location, url, true) - if err != nil { - return r, fmt.Errorf("err in git init: %s", err) - } - r, err = impl.OpenRepoPlain(location) - if err != nil { - return r, fmt.Errorf("err in git init: %s", err) - } +func parseCmdTimeoutJson(config *internals.Configuration) (map[string]int, error) { + commandTimeoutMap := make(map[string]int) + var err error + if config.CliCmdTimeoutJson != "" { + err = json.Unmarshal([]byte(config.CliCmdTimeoutJson), &commandTimeoutMap) } - return r, nil + return commandTimeoutMap, err } func (impl *GitManagerBaseImpl) Fetch(gitCtx GitContext, rootDir string) (response, errMsg string, err error) { impl.logger.Debugw("git fetch ", "location", rootDir) - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "fetch", "origin", "--tags", "--force") + cmd, cancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "fetch", "origin", "--tags", "--force") defer cancel() output, errMsg, err := impl.runCommandWithCred(cmd, gitCtx.Username, gitCtx.Password) impl.logger.Debugw("fetch output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err) @@ -116,7 +101,7 @@ func (impl *GitManagerBaseImpl) Fetch(gitCtx GitContext, rootDir string) (respon func (impl *GitManagerBaseImpl) Checkout(gitCtx GitContext, rootDir, branch string) (response, errMsg string, err error) { impl.logger.Debugw("git checkout ", "location", rootDir) - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "checkout", branch, "--force") + cmd, cancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "checkout", branch, "--force") defer cancel() output, errMsg, err := impl.runCommand(cmd) impl.logger.Debugw("checkout output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err) @@ -133,7 +118,7 @@ func (impl *GitManagerBaseImpl) LogMergeBase(gitCtx GitContext, rootDir, from st } cmdArgs := []string{"-C", rootDir, "log", from + "..." + toCommitHash, "--date=iso-strict", GITFORMAT} impl.logger.Debugw("git", cmdArgs) - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", cmdArgs...) + cmd, cancel := impl.createCmdWithContext(gitCtx, "git", cmdArgs...) defer cancel() output, errMsg, err := impl.runCommand(cmd) impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err) @@ -181,7 +166,7 @@ func (impl *GitManagerBaseImpl) runCommand(cmd *exec.Cmd) (response, errMsg stri func (impl *GitManagerBaseImpl) ConfigureSshCommand(gitCtx GitContext, rootDir string, sshPrivateKeyPath string) (response, errMsg string, err error) { impl.logger.Debugw("configuring ssh command on ", "location", rootDir) coreSshCommand := fmt.Sprintf("ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no", sshPrivateKeyPath) - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "config", "core.sshCommand", coreSshCommand) + cmd, cancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "config", "core.sshCommand", coreSshCommand) defer cancel() output, errMsg, err := impl.runCommand(cmd) impl.logger.Debugw("configure ssh command output ", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err) @@ -277,7 +262,7 @@ func (impl *GitManagerBaseImpl) FetchDiffStatBetweenCommits(gitCtx GitContext, o newHash = oldHash oldHash = oldHash + "^" } - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "diff", "--numstat", oldHash, newHash) + cmd, cancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "diff", "--numstat", oldHash, newHash) defer cancel() output, errMsg, err := impl.runCommandWithCred(cmd, gitCtx.Username, gitCtx.Password) @@ -285,7 +270,7 @@ func (impl *GitManagerBaseImpl) FetchDiffStatBetweenCommits(gitCtx GitContext, o return output, errMsg, err } -func (impl *GitManagerBaseImpl) CreateCmdWithContext(ctx GitContext, name string, arg ...string) (*exec.Cmd, context.CancelFunc) { +func (impl *GitManagerBaseImpl) createCmdWithContext(ctx GitContext, name string, arg ...string) (*exec.Cmd, context.CancelFunc) { newCtx := ctx cancel := func() {} @@ -308,3 +293,10 @@ func (impl *GitManagerBaseImpl) getCommandTimeout(command string) int { } return timeout } + +func (impl *GitManagerBaseImpl) ExecuteCustomCommand(gitContext GitContext, name string, arg ...string) (response, errMsg string, err error) { + cmd, cancel := impl.createCmdWithContext(gitContext, name, arg...) + defer cancel() + output, errMsg, err := impl.runCommandWithCred(cmd, gitContext.Username, gitContext.Password) + return output, errMsg, err +} diff --git a/pkg/git/GitCliManager.go b/pkg/git/GitCliManager.go index dd5bb922..d4cd89bc 100644 --- a/pkg/git/GitCliManager.go +++ b/pkg/git/GitCliManager.go @@ -1,24 +1,23 @@ package git import ( + "go.uber.org/zap" "gopkg.in/src-d/go-billy.v4/osfs" "os" "path/filepath" "strconv" ) -type GitCliManager interface { - GitManager -} - type GitCliManagerImpl struct { - *GitManagerBaseImpl + GitManagerBase + logger *zap.SugaredLogger } -func NewGitCliManagerImpl(baseManagerImpl *GitManagerBaseImpl) *GitCliManagerImpl { +func NewGitCliManagerImpl(baseManager GitManagerBase, logger *zap.SugaredLogger) *GitCliManagerImpl { return &GitCliManagerImpl{ - GitManagerBaseImpl: baseManagerImpl, + GitManagerBase: baseManager, + logger: logger, } } @@ -87,18 +86,14 @@ func openGitRepo(path string) error { } func (impl *GitCliManagerImpl) GitInit(gitCtx GitContext, rootDir string) error { impl.logger.Debugw("git", "-C", rootDir, "init") - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "init") - defer cancel() - output, errMsg, err := impl.runCommand(cmd) + output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "init") impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err) return err } func (impl *GitCliManagerImpl) GitCreateRemote(gitCtx GitContext, rootDir string, url string) error { impl.logger.Debugw("git", "-C", rootDir, "remote", "add", "origin", url) - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "remote", "add", "origin", url) - defer cancel() - output, errMsg, err := impl.runCommand(cmd) + output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "remote", "add", "origin", url) impl.logger.Debugw("url", url, "opt", output, "errMsg", errMsg, "error", err) return err } @@ -108,11 +103,8 @@ func (impl *GitCliManagerImpl) GetCommits(gitCtx GitContext, branchRef string, b rangeCmdArgs := []string{branchRef} extraCmdArgs := []string{"-n", strconv.Itoa(numCommits), "--date=iso-strict", GITFORMAT} cmdArgs := impl.getCommandForLogRange(branchRef, from, to, rangeCmdArgs, baseCmdArgs, extraCmdArgs) - impl.logger.Debugw("git", cmdArgs) - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", cmdArgs...) - defer cancel() - output, errMsg, err := impl.runCommand(cmd) + output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", cmdArgs...) impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err) if err != nil { return nil, err @@ -137,9 +129,7 @@ func (impl *GitCliManagerImpl) getCommandForLogRange(branchRef string, from stri func (impl *GitCliManagerImpl) GitShow(gitCtx GitContext, rootDir string, hash string) (GitCommit, error) { impl.logger.Debugw("git", "-C", rootDir, "show", hash, "--date=iso-strict", GITFORMAT, "-s") - cmd, cancel := impl.CreateCmdWithContext(gitCtx, "git", "-C", rootDir, "show", hash, "--date=iso-strict", GITFORMAT, "-s") - defer cancel() - output, errMsg, err := impl.runCommand(cmd) + output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "show", hash, "--date=iso-strict", GITFORMAT, "-s") impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err) if err != nil { return nil, err diff --git a/pkg/git/GitCliManager_test.go b/pkg/git/GitCliManager_test.go index 325d3be1..b0a6c803 100644 --- a/pkg/git/GitCliManager_test.go +++ b/pkg/git/GitCliManager_test.go @@ -3,7 +3,7 @@ package git import ( "context" "github.com/devtron-labs/common-lib/utils" - "github.com/devtron-labs/git-sensor/internal" + "github.com/devtron-labs/git-sensor/internals" "testing" ) @@ -12,7 +12,7 @@ func TestGitCliManagerImpl_processGitLogOutput(t *testing.T) { t.Run("tt.name", func(t *testing.T) { logger, _ := utils.NewSugardLogger() impl := &GitCliManagerImpl{ - GitManagerBaseImpl: NewGitManagerBaseImpl(logger, &internal.Configuration{}), + GitManagerBase: NewGitManagerBaseImpl(logger, &internals.Configuration{}), } got, err := impl.GetCommits(BuildGitContext(context.Background()), "main", "", "/Users/subhashish/workspace/lens", 15, "", "") print("err", got, err) @@ -20,15 +20,14 @@ func TestGitCliManagerImpl_processGitLogOutput(t *testing.T) { t.Run("analytics", func(t *testing.T) { logger, _ := utils.NewSugardLogger() - conf := &internal.Configuration{UseGitCli: false, AnalyticsDebug: true, GoGitTimeout: 10, CliCmdTimeoutGlobal: 8, CliCmdTimeoutJson: `{"log":4}`} - impl := &GoGitSDKManagerImpl{ - GitManagerBaseImpl: NewGitManagerBaseImpl(logger, conf), + conf := &internals.Configuration{UseGitCli: false, AnalyticsDebug: true, GoGitTimeout: 10, CliCmdTimeoutGlobal: 8, CliCmdTimeoutJson: `{"log":4}`} + impl := &GitManagerImpl{ + GitManager: NewGitManagerImpl(logger, conf), + } + analyticsImpl := &RepositoryManagerAnalyticsImpl{ + repoManager: NewRepositoryManagerImpl(logger, conf, impl), + gitManager: impl, } - analyticsImpl := &RepositoryManagerAnalyticsImpl{&RepositoryManagerImpl{ - logger: logger, - gitManager: GitManagerImpl{impl}, - configuration: conf, - }} //got, err := impl.GetCommits(GitContext{}, "main", "", "/Users/subhashish/workspace/lens", 15, "", "") got, err := analyticsImpl.ChangesSinceByRepositoryForAnalytics(BuildGitContext(context.Background()), "/Users/subhashish/workspace/lens", "e2d5f17556130e37a9e941a71ffc83a9a2085ec5", "dfb352083ed3131bb2f69cfa2e18c614e5e08700") print("err", got, err) diff --git a/pkg/git/GitContextUtils.go b/pkg/git/GitContextUtils.go index 13b179aa..b1476a6d 100644 --- a/pkg/git/GitContextUtils.go +++ b/pkg/git/GitContextUtils.go @@ -30,6 +30,11 @@ func (gitCtx GitContext) WithTimeout(timeoutSeconds int) (GitContext, context.Ca return gitCtx, cancel } +func (gitCtx GitContext) WithCloningMode(CloningMode string) GitContext { + gitCtx.CloningMode = CloningMode + return gitCtx +} + func RunWithTimeout[T any](ctx context.Context, f func() ([]*T, error)) ([]*T, error) { resultCh := make(chan []*T) errCh := make(chan error) diff --git a/pkg/git/GoGitSDKManager.go b/pkg/git/GoGitSDKManager.go index e1ee433b..48ea05f8 100644 --- a/pkg/git/GoGitSDKManager.go +++ b/pkg/git/GoGitSDKManager.go @@ -2,22 +2,22 @@ package git import ( "fmt" + "go.uber.org/zap" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" "os" ) -type GoGitSDKManager interface { - GitManager -} type GoGitSDKManagerImpl struct { - *GitManagerBaseImpl + GitManagerBase + logger *zap.SugaredLogger } -func NewGoGitSDKManagerImpl(baseManagerImpl *GitManagerBaseImpl) *GoGitSDKManagerImpl { +func NewGoGitSDKManagerImpl(baseManager GitManagerBase, logger *zap.SugaredLogger) *GoGitSDKManagerImpl { return &GoGitSDKManagerImpl{ - GitManagerBaseImpl: baseManagerImpl, + GitManagerBase: baseManager, + logger: logger, } } diff --git a/pkg/git/RepositoryManager.go b/pkg/git/RepositoryManager.go index 266be686..58c3ffa0 100644 --- a/pkg/git/RepositoryManager.go +++ b/pkg/git/RepositoryManager.go @@ -18,16 +18,20 @@ package git import ( "errors" - "github.com/devtron-labs/git-sensor/internal" + "fmt" + "github.com/devtron-labs/git-sensor/internals" "github.com/devtron-labs/git-sensor/util" "golang.org/x/sys/unix" "io" "os" + "path" + "regexp" + "strconv" "strings" "time" - "github.com/devtron-labs/git-sensor/internal/middleware" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/middleware" + "github.com/devtron-labs/git-sensor/internals/sql" "go.uber.org/zap" ) @@ -37,35 +41,40 @@ type RepositoryManager interface { Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, err error) // Add adds and initializes a new git repo , cleans the directory if not empty and fetches latest commits Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error + GetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, error) + FetchRepo(gitCtx GitContext, location string) error + GetLocationForMaterial(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error) + GetCheckoutPathAndLocation(gitCtx GitContext, material *sql.GitMaterial, url string) (string, string, error) + TrimLastGitCommit(gitCommits []*GitCommitBase, count int) []*GitCommitBase // Clean cleans a directory Clean(cloneDir string) error // ChangesSince given the checkput path, retrieves the latest commits for the gt repo existing on the path ChangesSince(gitCtx GitContext, checkoutPath string, branch string, from string, to string, count int) ([]*GitCommitBase, error) // ChangesSinceByRepository returns the latest commits list for the given range and count for an existing repo - ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int) ([]*GitCommitBase, error) + ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string) ([]*GitCommitBase, error) // GetCommitMetadata retrieves the commit metadata for given hash GetCommitMetadata(gitCtx GitContext, checkoutPath, commitHash string) (*GitCommitBase, error) // GetCommitForTag retrieves the commit metadata for given tag GetCommitForTag(gitCtx GitContext, checkoutPath, tag string) (*GitCommitBase, error) // CreateSshFileIfNotExistsAndConfigureSshCommand creates ssh file with creds and configures it at the location - CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) error + CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (string, error) } type RepositoryManagerImpl struct { logger *zap.SugaredLogger - gitManager GitManagerImpl - configuration *internal.Configuration + gitManager GitManager + configuration *internals.Configuration } func NewRepositoryManagerImpl( logger *zap.SugaredLogger, - configuration *internal.Configuration, - gitManager GitManagerImpl, + configuration *internals.Configuration, + gitManager GitManager, ) *RepositoryManagerImpl { return &RepositoryManagerImpl{logger: logger, configuration: configuration, gitManager: gitManager} } -func (impl RepositoryManagerImpl) IsSpaceAvailableOnDisk() bool { +func (impl *RepositoryManagerImpl) IsSpaceAvailableOnDisk() bool { var statFs unix.Statfs_t err := unix.Statfs(GIT_BASE_DIR, &statFs) if err != nil { @@ -75,7 +84,46 @@ func (impl RepositoryManagerImpl) IsSpaceAvailableOnDisk() bool { return availableSpace > int64(impl.configuration.MinLimit)*1024*1024 } -func (impl RepositoryManagerImpl) Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error { +func (impl *RepositoryManagerImpl) GetLocationForMaterial(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error) { + //gitRegex := `/(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/` + httpsRegex := `^https.*` + httpsMatched, err := regexp.MatchString(httpsRegex, material.Url) + if httpsMatched { + locationWithoutProtocol := strings.ReplaceAll(material.Url, "https://", "") + checkoutPath := path.Join(GIT_BASE_DIR, strconv.Itoa(material.Id), locationWithoutProtocol) + return checkoutPath, httpsMatched, false, nil + } + + sshRegex := `^git@.*` + sshMatched, err := regexp.MatchString(sshRegex, material.Url) + if sshMatched { + checkoutPath := path.Join(GIT_BASE_DIR, strconv.Itoa(material.Id), material.Url) + return checkoutPath, httpsMatched, sshMatched, nil + } + + return "", httpsMatched, sshMatched, fmt.Errorf("unsupported format url %s", material.Url) +} + +func (impl *RepositoryManagerImpl) GetCheckoutPathAndLocation(gitCtx GitContext, material *sql.GitMaterial, url string) (string, string, error) { + var checkoutPath string + var checkoutLocationForFetching string + checkoutPath, _, _, err := impl.GetLocationForMaterial(material, gitCtx.CloningMode) + if err != nil { + return checkoutPath, checkoutLocationForFetching, err + } + checkoutLocationForFetching = checkoutPath + return checkoutPath, checkoutLocationForFetching, nil +} + +func (impl *RepositoryManagerImpl) Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error { + _, err := impl.GetSshPrivateKeyPath(gitCtx, gitProviderId, location, url, authMode, sshPrivateKeyContent) + if err != nil { + return err + } + return impl.FetchRepo(gitCtx, location) +} + +func (impl *RepositoryManagerImpl) GetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, error) { var err error start := time.Now() defer func() { @@ -84,36 +132,41 @@ func (impl RepositoryManagerImpl) Add(gitCtx GitContext, gitProviderId int, loca err = os.RemoveAll(location) if err != nil { impl.logger.Errorw("error in cleaning checkout path", "err", err) - return err + return "", err } if !impl.IsSpaceAvailableOnDisk() { err = errors.New("git-sensor PVC - disk full, please increase space") - return err + return "", err } err = impl.gitManager.Init(gitCtx, location, url, true) if err != nil { impl.logger.Errorw("err in git init", "err", err) - return err + return "", err } - + var sshPrivateKeyPath string // check ssh if authMode == sql.AUTH_MODE_SSH { - err = impl.CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx, location, gitProviderId, sshPrivateKeyContent) + sshPrivateKeyPath, err = impl.CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx, location, gitProviderId, sshPrivateKeyContent) if err != nil { - return err + impl.logger.Errorw("error while creating ssh file for shallow clone", "checkoutPath", location, "sshPrivateKeyPath", sshPrivateKeyPath, "err", err) + return "", err } } + return sshPrivateKeyPath, nil +} + +func (impl *RepositoryManagerImpl) FetchRepo(gitCtx GitContext, location string) error { opt, errorMsg, err := impl.gitManager.Fetch(gitCtx, location) if err != nil { - impl.logger.Errorw("error in cloning repo", "errorMsg", errorMsg, "err", err) + impl.logger.Errorw("error in fetching repo", "errorMsg", errorMsg, "err", err) return err } impl.logger.Debugw("opt msg", "opt", opt) return nil } -func (impl RepositoryManagerImpl) Clean(dir string) error { +func (impl *RepositoryManagerImpl) Clean(dir string) error { var err error start := time.Now() defer func() { @@ -123,7 +176,7 @@ func (impl RepositoryManagerImpl) Clean(dir string) error { return err } -func (impl RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, err error) { +func (impl *RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, err error) { start := time.Now() defer func() { util.TriggerGitOperationMetrics("fetch", start, err) @@ -133,7 +186,7 @@ func (impl RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location err = errors.New("git-sensor PVC - disk full, please increase space") return false, nil, err } - r, err := impl.gitManager.OpenNewRepo(gitCtx, location, url) + r, err := impl.openNewRepo(gitCtx, location, url) if err != nil { return false, r, err } @@ -156,7 +209,7 @@ func (impl RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location } -func (impl RepositoryManagerImpl) GetCommitForTag(gitCtx GitContext, checkoutPath, tag string) (*GitCommitBase, error) { +func (impl *RepositoryManagerImpl) GetCommitForTag(gitCtx GitContext, checkoutPath, tag string) (*GitCommitBase, error) { var err error start := time.Now() defer func() { @@ -170,7 +223,7 @@ func (impl RepositoryManagerImpl) GetCommitForTag(gitCtx GitContext, checkoutPat return commit.GetCommit(), nil } -func (impl RepositoryManagerImpl) GetCommitMetadata(gitCtx GitContext, checkoutPath, commitHash string) (*GitCommitBase, error) { +func (impl *RepositoryManagerImpl) GetCommitMetadata(gitCtx GitContext, checkoutPath, commitHash string) (*GitCommitBase, error) { var err error start := time.Now() defer func() { @@ -186,7 +239,7 @@ func (impl RepositoryManagerImpl) GetCommitMetadata(gitCtx GitContext, checkoutP // from -> old commit // to -> new commit -func (impl RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int) ([]*GitCommitBase, error) { +func (impl *RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, repository *GitRepository, branch string, from string, to string, count int, checkoutPath string) ([]*GitCommitBase, error) { // fix for azure devops (manual trigger webhook bases pipeline) : // branch name comes as 'refs/heads/master', we need to extract actual branch name out of it. // https://stackoverflow.com/questions/59956206/how-to-get-a-branch-name-with-a-slash-in-azure-devops @@ -205,6 +258,7 @@ func (impl RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, re ToCommitHash: to, }) if err != nil { + impl.logger.Errorw("error in getting iterator", "branch", branch, "err", err) return nil, err } var gitCommits []*GitCommitBase @@ -255,7 +309,7 @@ func (impl RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, re if impl.configuration.EnableFileStats { defer func() { if err := recover(); err != nil { - impl.logger.Error("file stats function panicked for commit", "err", err, "commit", commit) + impl.logger.Error("file stats function panicked for commit", "err", err, "commit", commit, "count", count) } }() //TODO: implement below Stats() function using git CLI as it panics in some cases, remove defer function after using git CLI @@ -271,7 +325,14 @@ func (impl RepositoryManagerImpl) ChangesSinceByRepository(gitCtx GitContext, re return gitCommits, err } -func (impl RepositoryManagerImpl) ChangesSince(gitCtx GitContext, checkoutPath string, branch string, from string, to string, count int) ([]*GitCommitBase, error) { +func (impl *RepositoryManagerImpl) TrimLastGitCommit(gitCommits []*GitCommitBase, count int) []*GitCommitBase { + if len(gitCommits) > count { + gitCommits = gitCommits[:len(gitCommits)-1] + } + return gitCommits +} + +func (impl *RepositoryManagerImpl) ChangesSince(gitCtx GitContext, checkoutPath string, branch string, from string, to string, count int) ([]*GitCommitBase, error) { var err error start := time.Now() defer func() { @@ -285,30 +346,54 @@ func (impl RepositoryManagerImpl) ChangesSince(gitCtx GitContext, checkoutPath s return nil, err } ///--------------------- - return impl.ChangesSinceByRepository(gitCtx, r, branch, from, to, count) + return impl.ChangesSinceByRepository(gitCtx, r, branch, from, to, count, checkoutPath) ///---------------------- } -func (impl RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) error { +func (impl *RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (string, error) { // add private key var err error + var sshPrivateKeyPath string start := time.Now() defer func() { util.TriggerGitOperationMetrics("createSshFileIfNotExistsAndConfigureSshCommand", start, err) }() - sshPrivateKeyPath, err := GetOrCreateSshPrivateKeyOnDisk(gitProviderId, sshPrivateKeyContent) + sshPrivateKeyPath, err = GetOrCreateSshPrivateKeyOnDisk(gitProviderId, sshPrivateKeyContent) if err != nil { impl.logger.Errorw("error in creating ssh private key", "err", err) - return err + return sshPrivateKeyPath, err } //git config core.sshCommand _, errorMsg, err := impl.gitManager.ConfigureSshCommand(gitCtx, location, sshPrivateKeyPath) if err != nil { impl.logger.Errorw("error in configuring ssh command while adding repo", "errorMsg", errorMsg, "err", err) - return err + return sshPrivateKeyPath, err } - return nil + return sshPrivateKeyPath, nil +} + +func (impl *RepositoryManagerImpl) openNewRepo(gitCtx GitContext, location string, url string) (*GitRepository, error) { + + r, err := impl.gitManager.OpenRepoPlain(location) + if err != nil { + err = os.RemoveAll(location) + if err != nil { + impl.logger.Errorw("error in cleaning checkout path: %s", err) + return r, err + } + err = impl.gitManager.Init(gitCtx, location, url, true) + if err != nil { + impl.logger.Errorw("err in git init: %s", err) + return r, err + } + r, err = impl.gitManager.OpenRepoPlain(location) + if err != nil { + impl.logger.Errorw("err in git init: %s", err) + return r, err + } + } + return r, nil } diff --git a/pkg/git/RepositoryManagerAnalytics.go b/pkg/git/RepositoryManagerAnalytics.go index 70873ce9..6bb08641 100644 --- a/pkg/git/RepositoryManagerAnalytics.go +++ b/pkg/git/RepositoryManagerAnalytics.go @@ -2,7 +2,9 @@ package git import ( "fmt" + "github.com/devtron-labs/git-sensor/internals" "github.com/devtron-labs/git-sensor/util" + "go.uber.org/zap" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" @@ -17,14 +19,20 @@ type RepositoryManagerAnalytics interface { } type RepositoryManagerAnalyticsImpl struct { - *RepositoryManagerImpl + repoManager RepositoryManager + gitManager GitManager + configuration *internals.Configuration + logger *zap.SugaredLogger } -func NewRepositoryManagerAnalyticsImpl( - repositoryManagerImpl *RepositoryManagerImpl, -) *RepositoryManagerAnalyticsImpl { +func NewRepositoryManagerAnalyticsImpl(repoManager RepositoryManager, gitManager GitManager, + configuration *internals.Configuration, logger *zap.SugaredLogger) *RepositoryManagerAnalyticsImpl { return &RepositoryManagerAnalyticsImpl{ - RepositoryManagerImpl: repositoryManagerImpl} + repoManager: repoManager, + gitManager: gitManager, + configuration: configuration, + logger: logger, + } } func computeDiff(r *git.Repository, newHash *plumbing.Hash, oldHash *plumbing.Hash) ([]*object.Commit, error) { diff --git a/pkg/git/RepositoryManagerIT_test.go b/pkg/git/RepositoryManagerIT_test.go index 4312c269..acce2633 100644 --- a/pkg/git/RepositoryManagerIT_test.go +++ b/pkg/git/RepositoryManagerIT_test.go @@ -3,8 +3,8 @@ package git import ( "context" "github.com/devtron-labs/common-lib/utils" - "github.com/devtron-labs/git-sensor/internal" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/stretchr/testify/assert" "os" "reflect" @@ -26,24 +26,24 @@ var password = "" var sshPrivateKey = `` func getRepoManagerAnalyticsImpl(t *testing.T) *RepositoryManagerAnalyticsImpl { - return &RepositoryManagerAnalyticsImpl{RepositoryManagerImpl: getRepoManagerImpl(t)} + return &RepositoryManagerAnalyticsImpl{repoManager: getRepoManagerImpl(t)} } func getRepoManagerImpl(t *testing.T) *RepositoryManagerImpl { logger, err := utils.NewSugardLogger() assert.Nil(t, err) - conf := &internal.Configuration{ + conf := &internals.Configuration{ CommitStatsTimeoutInSec: 0, EnableFileStats: true, GitHistoryCount: 2, - UseGitCli: true, + UseGitCli: false, GoGitTimeout: 10, } base := NewGitManagerBaseImpl(logger, conf) - gitCliImpl := NewGitCliManagerImpl(base) - gogitImpl := NewGoGitSDKManagerImpl(base) + _ = NewGitCliManagerImpl(base, logger) + _ = NewGoGitSDKManagerImpl(base, logger) - gitUtil := NewGitManagerImpl(conf, gitCliImpl, gogitImpl) + gitUtil := NewGitManagerImpl(logger, conf) repositoryManagerImpl := NewRepositoryManagerImpl(logger, conf, gitUtil) return repositoryManagerImpl } @@ -132,7 +132,7 @@ func TestRepositoryManager_Add(t *testing.T) { repositoryManagerImpl := getRepoManagerImpl(t) for _, tt := range tests { if tt.payload.authMode == "SSH" { - err := repositoryManagerImpl.CreateSshFileIfNotExistsAndConfigureSshCommand(BuildGitContext(context.Background()), tt.payload.location, tt.payload.gitProviderId, tt.payload.sshPrivateKeyContent) + _, err := repositoryManagerImpl.CreateSshFileIfNotExistsAndConfigureSshCommand(BuildGitContext(context.Background()), tt.payload.location, tt.payload.gitProviderId, tt.payload.sshPrivateKeyContent) assert.Nil(t, err) } t.Run(tt.name, func(t *testing.T) { @@ -583,7 +583,7 @@ func TestRepositoryManager_ChangesSinceByRepository(t *testing.T) { r, err := repositoryManagerImpl.gitManager.OpenRepoPlain(tt.payload.checkoutPath) assert.Nil(t, err) t.Run(tt.name, func(t *testing.T) { - got, err := repositoryManagerImpl.ChangesSinceByRepository(BuildGitContext(context.Background()), r, tt.payload.branch, tt.payload.from, tt.payload.to, tt.payload.count) + got, err := repositoryManagerImpl.ChangesSinceByRepository(BuildGitContext(context.Background()), r, tt.payload.branch, tt.payload.from, tt.payload.to, tt.payload.count, tt.payload.checkoutPath) if (err != nil) != tt.wantErr { t.Errorf("ChangesSinceByRepository() error in %s, error = %v, wantErr %v", tt.name, err, tt.wantErr) return diff --git a/pkg/git/Util.go b/pkg/git/Util.go index 6f956fa1..334bc7e8 100644 --- a/pkg/git/Util.go +++ b/pkg/git/Util.go @@ -18,11 +18,10 @@ package git import ( "fmt" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "io/ioutil" "os" "path" - "regexp" "strconv" "strings" ) @@ -33,6 +32,10 @@ const ( SSH_PRIVATE_KEY_FILE_NAME = "ssh_pvt_key" CLONE_TIMEOUT_SEC = 600 FETCH_TIMEOUT_SEC = 30 + GITHUB_PROVIDER = "github.com" + GITLAB_PROVIDER = "gitlab.com" + CloningModeShallow = "SHALLOW" + CloningModeFull = "FULL" ) //git@gitlab.com:devtron-client-gitops/wms-user-management.git @@ -41,24 +44,18 @@ const ( //git@bitbucket.org:DelhiveryTech/kafka-consumer-config.git //https://prashant-delhivery@bitbucket.org/DelhiveryTech/kafka-consumer-config.git -func GetLocationForMaterial(material *sql.GitMaterial) (location string, err error) { - //gitRegex := `/(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/` - httpsRegex := `^https.*` - httpsMatched, err := regexp.MatchString(httpsRegex, material.Url) - if httpsMatched { - locationWithoutProtocol := strings.ReplaceAll(material.Url, "https://", "") - checkoutPath := path.Join(GIT_BASE_DIR, strconv.Itoa(material.Id), locationWithoutProtocol) - return checkoutPath, nil - } - - sshRegex := `^git@.*` - sshMatched, err := regexp.MatchString(sshRegex, material.Url) - if sshMatched { - checkoutPath := path.Join(GIT_BASE_DIR, strconv.Itoa(material.Id), material.Url) - return checkoutPath, nil - } - - return "", fmt.Errorf("unsupported format url %s", material.Url) +func GetProjectName(url string) string { + //if url = https://github.com/devtron-labs/git-sensor.git then it will return git-sensor + projName := strings.Split(url, ".")[1] + projectName := projName[strings.LastIndex(projName, "/")+1:] + return projectName +} +func GetCheckoutPath(url string, cloneLocation string) string { + //url= https://github.com/devtron-labs/git-sensor.git cloneLocation= git-base/1/github.com/prakash100198 + //then this function returns git-base/1/github.com/prakash100198/SampleGoLangProject/.git + projectName := GetProjectName(url) + projRootDir := cloneLocation + "/" + projectName + "/.git" + return projRootDir } func GetUserNamePassword(gitProvider *sql.GitProvider) (userName, password string, err error) { diff --git a/pkg/git/Watcher.go b/pkg/git/Watcher.go index 86bcb1db..32511eee 100644 --- a/pkg/git/Watcher.go +++ b/pkg/git/Watcher.go @@ -24,13 +24,14 @@ import ( "github.com/devtron-labs/common-lib/constants" pubsub "github.com/devtron-labs/common-lib/pubsub-lib" "github.com/devtron-labs/common-lib/pubsub-lib/model" - "github.com/devtron-labs/git-sensor/internal" - "github.com/devtron-labs/git-sensor/internal/middleware" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals" + "github.com/devtron-labs/git-sensor/internals/middleware" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/gammazero/workerpool" "github.com/robfig/cron/v3" "go.uber.org/zap" "runtime/debug" + "strings" "time" ) @@ -41,11 +42,11 @@ type GitWatcherImpl struct { logger *zap.SugaredLogger ciPipelineMaterialRepository sql.CiPipelineMaterialRepository pubSubClient *pubsub.PubSubClientServiceImpl - locker *internal.RepositoryLocker + locker *internals.RepositoryLocker pollConfig *PollConfig webhookHandler WebhookHandler - configuration *internal.Configuration - gitManager GitManagerImpl + configuration *internals.Configuration + gitManager GitManager } type GitWatcher interface { @@ -61,9 +62,9 @@ func NewGitWatcherImpl(repositoryManager RepositoryManager, materialRepo sql.MaterialRepository, logger *zap.SugaredLogger, ciPipelineMaterialRepository sql.CiPipelineMaterialRepository, - locker *internal.RepositoryLocker, - pubSubClient *pubsub.PubSubClientServiceImpl, webhookHandler WebhookHandler, configuration *internal.Configuration, - gitmanager GitManagerImpl, + locker *internals.RepositoryLocker, + pubSubClient *pubsub.PubSubClientServiceImpl, webhookHandler WebhookHandler, configuration *internals.Configuration, + gitmanager GitManager, ) (*GitWatcherImpl, error) { cfg := &PollConfig{} @@ -183,22 +184,28 @@ func (impl GitWatcherImpl) pollAndUpdateGitMaterial(materialReq *sql.GitMaterial func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) error { gitProvider := material.GitProvider userName, password, err := GetUserNamePassword(gitProvider) - - gitCtx := BuildGitContext(context.Background()). - WithCredentials(userName, password) - - location, err := GetLocationForMaterial(material) + location := material.CheckoutLocation if err != nil { impl.logger.Errorw("error in determining location", "url", material.Url, "err", err) return err } + gitCtx := BuildGitContext(context.Background()). + WithCredentials(userName, password). + WithCloningMode(impl.configuration.CloningMode) updated, repo, err := impl.FetchAndUpdateMaterial(gitCtx, material, location) if err != nil { impl.logger.Errorw("error in fetching material details ", "repo", material.Url, "err", err) // there might be the case if ssh private key gets flush from disk, so creating and single retrying in this case if gitProvider.AuthMode == sql.AUTH_MODE_SSH { - err = impl.repositoryManager.CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx, location, gitProvider.Id, gitProvider.SshPrivateKey) + if strings.Contains(material.CheckoutLocation, "/.git") { + location, _, _, err = impl.repositoryManager.GetLocationForMaterial(material, gitCtx.CloningMode) + if err != nil { + impl.logger.Errorw("error in getting clone location ", "material", material, "err", err) + return err + } + } + _, err = impl.repositoryManager.CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx, location, gitProvider.Id, gitProvider.SshPrivateKey) if err != nil { impl.logger.Errorw("error in creating/configuring ssh private key on disk ", "repo", material.Url, "gitProviderId", gitProvider.Id, "err", err) return err @@ -225,6 +232,7 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) e var updatedMaterials []*CiPipelineMaterialBean var updatedMaterialsModel []*sql.CiPipelineMaterial var erroredMaterialsModels []*sql.CiPipelineMaterial + checkoutLocation := material.CheckoutLocation for _, material := range materials { if material.Type != sql.SOURCE_TYPE_BRANCH_FIXED { continue @@ -232,7 +240,7 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) e impl.logger.Debugw("Running changesBySinceRepository for material - ", material) impl.logger.Debugw("---------------------------------------------------------- ") // parse env variables here, then search for the count field and pass here. - commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, "", "", impl.configuration.GitHistoryCount) + commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, "", "", impl.configuration.GitHistoryCount, checkoutLocation) if err != nil { material.Errored = true material.ErrorMsg = err.Error() diff --git a/pkg/git/WebhookEventBeanConverter.go b/pkg/git/WebhookEventBeanConverter.go index 283d0a05..e43ef9fa 100644 --- a/pkg/git/WebhookEventBeanConverter.go +++ b/pkg/git/WebhookEventBeanConverter.go @@ -17,7 +17,7 @@ package git -import "github.com/devtron-labs/git-sensor/internal/sql" +import "github.com/devtron-labs/git-sensor/internals/sql" type WebhookEventBeanConverter interface { ConvertFromWebhookParsedDataSqlBean(sqlBean *sql.WebhookEventParsedData) *WebhookData @@ -25,51 +25,48 @@ type WebhookEventBeanConverter interface { } type WebhookEventBeanConverterImpl struct { - } func NewWebhookEventBeanConverterImpl() *WebhookEventBeanConverterImpl { - return &WebhookEventBeanConverterImpl{ - - } + return &WebhookEventBeanConverterImpl{} } func (impl WebhookEventBeanConverterImpl) ConvertFromWebhookParsedDataSqlBean(sqlBean *sql.WebhookEventParsedData) *WebhookData { return &WebhookData{ - Id : sqlBean.Id, + Id: sqlBean.Id, EventActionType: sqlBean.EventActionType, - Data : sqlBean.Data, + Data: sqlBean.Data, } } func (impl WebhookEventBeanConverterImpl) ConvertFromWebhookEventSqlBean(webhookEventFromDb *sql.GitHostWebhookEvent) *WebhookEventConfig { webhookEvent := &WebhookEventConfig{ - Id : webhookEventFromDb.Id, - GitHostId: webhookEventFromDb.GitHostId, - Name: webhookEventFromDb.Name, + Id: webhookEventFromDb.Id, + GitHostId: webhookEventFromDb.GitHostId, + Name: webhookEventFromDb.Name, EventTypesCsv: webhookEventFromDb.EventTypesCsv, - ActionType: webhookEventFromDb.ActionType, - IsActive: webhookEventFromDb.IsActive, - CreatedOn: webhookEventFromDb.CreatedOn, - UpdatedOn: webhookEventFromDb.UpdatedOn, + ActionType: webhookEventFromDb.ActionType, + IsActive: webhookEventFromDb.IsActive, + CreatedOn: webhookEventFromDb.CreatedOn, + UpdatedOn: webhookEventFromDb.UpdatedOn, } // build selectors var webhookEventSelectors []*WebhookEventSelectors for _, selectorFromDb := range webhookEventFromDb.Selectors { selector := &WebhookEventSelectors{ - Id : selectorFromDb.Id, - EventId: selectorFromDb.EventId, - Name: selectorFromDb.Name, - Selector: selectorFromDb.Selector, - ToShow: selectorFromDb.ToShow, + Id: selectorFromDb.Id, + EventId: selectorFromDb.EventId, + Name: selectorFromDb.Name, + Selector: selectorFromDb.Selector, + ToShow: selectorFromDb.ToShow, ToShowInCiFilter: selectorFromDb.ToShowInCiFilter, - FixValue: selectorFromDb.FixValue, - PossibleValues: selectorFromDb.PossibleValues, - IsActive: selectorFromDb.IsActive, - CreatedOn: selectorFromDb.CreatedOn, - UpdatedOn: selectorFromDb.UpdatedOn, + FixValue: selectorFromDb.FixValue, + PossibleValues: selectorFromDb.PossibleValues, + IsActive: selectorFromDb.IsActive, + CreatedOn: selectorFromDb.CreatedOn, + UpdatedOn: selectorFromDb.UpdatedOn, } webhookEventSelectors = append(webhookEventSelectors, selector) } @@ -78,4 +75,4 @@ func (impl WebhookEventBeanConverterImpl) ConvertFromWebhookEventSqlBean(webhook return webhookEvent -} \ No newline at end of file +} diff --git a/pkg/git/WebhookEventParser.go b/pkg/git/WebhookEventParser.go index 596e7d55..0b9c3c08 100644 --- a/pkg/git/WebhookEventParser.go +++ b/pkg/git/WebhookEventParser.go @@ -18,7 +18,7 @@ package git import ( - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/tidwall/gjson" "go.uber.org/zap" "time" diff --git a/pkg/git/WebhookEventService.go b/pkg/git/WebhookEventService.go index e1452cbb..bad9b8f2 100644 --- a/pkg/git/WebhookEventService.go +++ b/pkg/git/WebhookEventService.go @@ -20,8 +20,8 @@ import ( "encoding/json" "fmt" pubsub "github.com/devtron-labs/common-lib/pubsub-lib" - "github.com/devtron-labs/git-sensor/internal/sql" - "github.com/devtron-labs/git-sensor/internal/util" + "github.com/devtron-labs/git-sensor/internals/sql" + "github.com/devtron-labs/git-sensor/internals/util" _ "github.com/robfig/cron/v3" "go.uber.org/zap" "regexp" diff --git a/pkg/mocks/RepoManager.go b/pkg/mocks/RepoManager.go index 453d9451..3b73c688 100644 --- a/pkg/mocks/RepoManager.go +++ b/pkg/mocks/RepoManager.go @@ -8,7 +8,7 @@ import ( pkg "github.com/devtron-labs/git-sensor/pkg" - sql "github.com/devtron-labs/git-sensor/internal/sql" + sql "github.com/devtron-labs/git-sensor/internals/sql" ) // RepoManager is an autogenerated mock type for the RepoManager type diff --git a/util/BasicUtil.go b/util/BasicUtil.go index 4c6109ab..e3761ab3 100644 --- a/util/BasicUtil.go +++ b/util/BasicUtil.go @@ -17,7 +17,7 @@ package util import ( - "github.com/devtron-labs/git-sensor/internal/middleware" + "github.com/devtron-labs/git-sensor/internals/middleware" "math/rand" "strings" "time" diff --git a/wire.go b/wire.go index afb34140..073d344b 100644 --- a/wire.go +++ b/wire.go @@ -20,69 +20,18 @@ package main import ( - "github.com/devtron-labs/common-lib/monitoring" - pubsub "github.com/devtron-labs/common-lib/pubsub-lib" - "github.com/devtron-labs/git-sensor/api" - "github.com/devtron-labs/git-sensor/internal" - "github.com/devtron-labs/git-sensor/internal/logger" - "github.com/devtron-labs/git-sensor/internal/sql" - "github.com/devtron-labs/git-sensor/pkg" + "github.com/devtron-labs/git-sensor/app" "github.com/devtron-labs/git-sensor/pkg/git" + "github.com/devtron-labs/git-sensor/wireset" "github.com/google/wire" ) -func InitializeApp() (*App, error) { +func InitializeApp() (*app.App, error) { wire.Build( - NewApp, - api.NewMuxRouter, - internal.ParseConfiguration, - logger.NewSugaredLogger, - api.NewRestHandlerImpl, - wire.Bind(new(api.RestHandler), new(*api.RestHandlerImpl)), - api.NewGrpcHandlerImpl, - pkg.NewRepoManagerImpl, - wire.Bind(new(pkg.RepoManager), new(*pkg.RepoManagerImpl)), - sql.NewMaterialRepositoryImpl, - wire.Bind(new(sql.MaterialRepository), new(*sql.MaterialRepositoryImpl)), - sql.NewDbConnection, - sql.GetConfig, - sql.NewCiPipelineMaterialRepositoryImpl, - wire.Bind(new(sql.CiPipelineMaterialRepository), new(*sql.CiPipelineMaterialRepositoryImpl)), - sql.NewGitProviderRepositoryImpl, - wire.Bind(new(sql.GitProviderRepository), new(*sql.GitProviderRepositoryImpl)), + wireset.CommonWireSet, git.NewRepositoryManagerImpl, wire.Bind(new(git.RepositoryManager), new(*git.RepositoryManagerImpl)), - git.NewRepositoryManagerAnalyticsImpl, - wire.Bind(new(git.RepositoryManagerAnalytics), new(*git.RepositoryManagerAnalyticsImpl)), - git.NewGoGitSDKManagerImpl, - wire.Bind(new(git.GoGitSDKManager), new(*git.GoGitSDKManagerImpl)), - git.NewGitCliManagerImpl, - wire.Bind(new(git.GitCliManager), new(*git.GitCliManagerImpl)), - git.NewGitManagerImpl, - git.NewGitManagerBaseImpl, - git.NewGitWatcherImpl, - wire.Bind(new(git.GitWatcher), new(*git.GitWatcherImpl)), - internal.NewRepositoryLocker, - //internal.NewNatsConnection, - pubsub.NewPubSubClientServiceImpl, - sql.NewWebhookEventRepositoryImpl, - wire.Bind(new(sql.WebhookEventRepository), new(*sql.WebhookEventRepositoryImpl)), - sql.NewWebhookEventParsedDataRepositoryImpl, - wire.Bind(new(sql.WebhookEventParsedDataRepository), new(*sql.WebhookEventParsedDataRepositoryImpl)), - sql.NewWebhookEventDataMappingRepositoryImpl, - wire.Bind(new(sql.WebhookEventDataMappingRepository), new(*sql.WebhookEventDataMappingRepositoryImpl)), - sql.NewWebhookEventDataMappingFilterResultRepositoryImpl, - wire.Bind(new(sql.WebhookEventDataMappingFilterResultRepository), new(*sql.WebhookEventDataMappingFilterResultRepositoryImpl)), - git.NewWebhookEventBeanConverterImpl, - wire.Bind(new(git.WebhookEventBeanConverter), new(*git.WebhookEventBeanConverterImpl)), - git.NewWebhookEventServiceImpl, - wire.Bind(new(git.WebhookEventService), new(*git.WebhookEventServiceImpl)), - git.NewWebhookEventParserImpl, - wire.Bind(new(git.WebhookEventParser), new(*git.WebhookEventParserImpl)), - git.NewWebhookHandlerImpl, - wire.Bind(new(git.WebhookHandler), new(*git.WebhookHandlerImpl)), - monitoring.NewMonitoringRouter, ) - return &App{}, nil + return &app.App{}, nil } diff --git a/wire_gen.go b/wire_gen.go index 3886ce60..c7b7edf5 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -10,16 +10,17 @@ import ( "github.com/devtron-labs/common-lib/monitoring" "github.com/devtron-labs/common-lib/pubsub-lib" "github.com/devtron-labs/git-sensor/api" - "github.com/devtron-labs/git-sensor/internal" - "github.com/devtron-labs/git-sensor/internal/logger" - "github.com/devtron-labs/git-sensor/internal/sql" + "github.com/devtron-labs/git-sensor/app" + "github.com/devtron-labs/git-sensor/internals" + "github.com/devtron-labs/git-sensor/internals/logger" + "github.com/devtron-labs/git-sensor/internals/sql" "github.com/devtron-labs/git-sensor/pkg" "github.com/devtron-labs/git-sensor/pkg/git" ) // Injectors from wire.go: -func InitializeApp() (*App, error) { +func InitializeApp() (*app.App, error) { sugaredLogger := logger.NewSugaredLogger() config, err := sql.GetConfig() if err != nil { @@ -30,19 +31,16 @@ func InitializeApp() (*App, error) { return nil, err } materialRepositoryImpl := sql.NewMaterialRepositoryImpl(db) - configuration, err := internal.ParseConfiguration() + configuration, err := internals.ParseConfiguration() if err != nil { return nil, err } - gitManagerBaseImpl := git.NewGitManagerBaseImpl(sugaredLogger, configuration) - gitCliManagerImpl := git.NewGitCliManagerImpl(gitManagerBaseImpl) - goGitSDKManagerImpl := git.NewGoGitSDKManagerImpl(gitManagerBaseImpl) - gitManagerImpl := git.NewGitManagerImpl(configuration, gitCliManagerImpl, goGitSDKManagerImpl) + gitManagerImpl := git.NewGitManagerImpl(sugaredLogger, configuration) repositoryManagerImpl := git.NewRepositoryManagerImpl(sugaredLogger, configuration, gitManagerImpl) - repositoryManagerAnalyticsImpl := git.NewRepositoryManagerAnalyticsImpl(repositoryManagerImpl) + repositoryManagerAnalyticsImpl := git.NewRepositoryManagerAnalyticsImpl(repositoryManagerImpl, gitManagerImpl, configuration, sugaredLogger) gitProviderRepositoryImpl := sql.NewGitProviderRepositoryImpl(db) ciPipelineMaterialRepositoryImpl := sql.NewCiPipelineMaterialRepositoryImpl(db, sugaredLogger) - repositoryLocker := internal.NewRepositoryLocker(sugaredLogger) + repositoryLocker := internals.NewRepositoryLocker(sugaredLogger) pubSubClientServiceImpl := pubsub_lib.NewPubSubClientServiceImpl(sugaredLogger) webhookEventRepositoryImpl := sql.NewWebhookEventRepositoryImpl(db) webhookEventParsedDataRepositoryImpl := sql.NewWebhookEventParsedDataRepositoryImpl(db) @@ -61,6 +59,6 @@ func InitializeApp() (*App, error) { monitoringRouter := monitoring.NewMonitoringRouter(sugaredLogger) muxRouter := api.NewMuxRouter(sugaredLogger, restHandlerImpl, monitoringRouter) grpcHandlerImpl := api.NewGrpcHandlerImpl(repoManagerImpl, sugaredLogger) - app := NewApp(muxRouter, sugaredLogger, gitWatcherImpl, db, pubSubClientServiceImpl, grpcHandlerImpl) - return app, nil + appApp := app.NewApp(muxRouter, sugaredLogger, gitWatcherImpl, db, pubSubClientServiceImpl, grpcHandlerImpl) + return appApp, nil } diff --git a/wireset/commonWireset.go b/wireset/commonWireset.go new file mode 100644 index 00000000..57e27a80 --- /dev/null +++ b/wireset/commonWireset.go @@ -0,0 +1,59 @@ +package wireset + +import ( + "github.com/devtron-labs/common-lib/monitoring" + pubsub "github.com/devtron-labs/common-lib/pubsub-lib" + "github.com/devtron-labs/git-sensor/api" + "github.com/devtron-labs/git-sensor/app" + "github.com/devtron-labs/git-sensor/internals" + "github.com/devtron-labs/git-sensor/internals/logger" + "github.com/devtron-labs/git-sensor/internals/sql" + "github.com/devtron-labs/git-sensor/pkg" + "github.com/devtron-labs/git-sensor/pkg/git" + "github.com/google/wire" +) + +var CommonWireSet = wire.NewSet(app.NewApp, + api.NewMuxRouter, + internals.ParseConfiguration, + logger.NewSugaredLogger, + api.NewRestHandlerImpl, + wire.Bind(new(api.RestHandler), new(*api.RestHandlerImpl)), + api.NewGrpcHandlerImpl, + sql.NewMaterialRepositoryImpl, + wire.Bind(new(sql.MaterialRepository), new(*sql.MaterialRepositoryImpl)), + sql.NewDbConnection, + sql.GetConfig, + sql.NewCiPipelineMaterialRepositoryImpl, + wire.Bind(new(sql.CiPipelineMaterialRepository), new(*sql.CiPipelineMaterialRepositoryImpl)), + sql.NewGitProviderRepositoryImpl, + wire.Bind(new(sql.GitProviderRepository), new(*sql.GitProviderRepositoryImpl)), + git.NewGitManagerImpl, + wire.Bind(new(git.GitManager), new(*git.GitManagerImpl)), + git.NewRepositoryManagerAnalyticsImpl, + wire.Bind(new(git.RepositoryManagerAnalytics), new(*git.RepositoryManagerAnalyticsImpl)), + pkg.NewRepoManagerImpl, + wire.Bind(new(pkg.RepoManager), new(*pkg.RepoManagerImpl)), + git.NewGitWatcherImpl, + wire.Bind(new(git.GitWatcher), new(*git.GitWatcherImpl)), + internals.NewRepositoryLocker, + //internal.NewNatsConnection, + pubsub.NewPubSubClientServiceImpl, + sql.NewWebhookEventRepositoryImpl, + wire.Bind(new(sql.WebhookEventRepository), new(*sql.WebhookEventRepositoryImpl)), + sql.NewWebhookEventParsedDataRepositoryImpl, + wire.Bind(new(sql.WebhookEventParsedDataRepository), new(*sql.WebhookEventParsedDataRepositoryImpl)), + sql.NewWebhookEventDataMappingRepositoryImpl, + wire.Bind(new(sql.WebhookEventDataMappingRepository), new(*sql.WebhookEventDataMappingRepositoryImpl)), + sql.NewWebhookEventDataMappingFilterResultRepositoryImpl, + wire.Bind(new(sql.WebhookEventDataMappingFilterResultRepository), new(*sql.WebhookEventDataMappingFilterResultRepositoryImpl)), + git.NewWebhookEventBeanConverterImpl, + wire.Bind(new(git.WebhookEventBeanConverter), new(*git.WebhookEventBeanConverterImpl)), + git.NewWebhookEventServiceImpl, + wire.Bind(new(git.WebhookEventService), new(*git.WebhookEventServiceImpl)), + git.NewWebhookEventParserImpl, + wire.Bind(new(git.WebhookEventParser), new(*git.WebhookEventParserImpl)), + git.NewWebhookHandlerImpl, + wire.Bind(new(git.WebhookHandler), new(*git.WebhookHandlerImpl)), + monitoring.NewMonitoringRouter, +)