Skip to content

fix: Git cli analytics develop #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions config.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# GITSENSOR CONFIGMAP

| Key | Value | Description |
|-----------------------------------|------------------------------------|-------------------------------|
| PG_ADDR | postgresql-postgresql.devtroncd | PostgreSQL Server Address |
| PG_USER | postgres | PostgreSQL User |
| PG_DATABASE | git_sensor | PostgreSQL Database Name |
| SENTRY_ENV | prod | Sentry Environment |
| SENTRY_ENABLED | "false" | Sentry Enabled (boolean) |
| POLL_DURATION | "1" | Polling Duration (in seconds) |
| POLL_WORKER | "2" | Number of Polling Workers |
| PG_LOG_QUERY | "false" | PostgreSQL Query Logging (boolean) |
| COMMIT_STATS_TIMEOUT_IN_SEC | "2" | Commit Stats Timeout (in seconds) |
| ENABLE_FILE_STATS | "false" | Enable File Stats (boolean) |
| GIT_HISTORY_COUNT | "15" | Git History Count |
| CLONING_MODE | FULL | Cloning Mode (Possible values: SHALLOW, FULL) |
| Key | Value | Description |
|-----------------------------|---------------------------------|---------------------------------------------------------------------|
| PG_ADDR | postgresql-postgresql.devtroncd | PostgreSQL Server Address |
| PG_USER | postgres | PostgreSQL User |
| PG_DATABASE | git_sensor | PostgreSQL Database Name |
| SENTRY_ENV | prod | Sentry Environment |
| SENTRY_ENABLED | "false" | Sentry Enabled (boolean) |
| POLL_DURATION | "1" | Polling Duration (in seconds) |
| POLL_WORKER | "2" | Number of Polling Workers |
| PG_LOG_QUERY | "false" | PostgreSQL Query Logging (boolean) |
| COMMIT_STATS_TIMEOUT_IN_SEC | "2" | Commit Stats Timeout (in seconds) |
| ENABLE_FILE_STATS | "false" | Enable File Stats (boolean) |
| GIT_HISTORY_COUNT | "15" | Git History Count |
| CLONING_MODE | FULL | Cloning Mode (Possible values: SHALLOW, FULL) |
| USE_GIT_CLI | "false" | Use git cli commands directly for all git operations |
| USE_GIT_CLI_ANALYTICS | "false" | Use git cli commands directly for getting commit data for analytics |
3 changes: 2 additions & 1 deletion internals/Configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ type Configuration struct {
GitHistoryCount int `env:"GIT_HISTORY_COUNT" envDefault:"15"`
MinLimit int `env:"MIN_LIMIT_FOR_PVC" envDefault:"1"` // in MB
UseGitCli bool `env:"USE_GIT_CLI" envDefault:"false"`
UseGitCliAnalytics bool `env:"USE_GIT_CLI_ANALYTICS" envDefault:"false"` // This flag is used to compute commitDiff using git-cli only for analytics
AnalyticsDebug bool `env:"ANALYTICS_DEBUG" envDefault:"false"`
CliCmdTimeoutGlobal int `env:"CLI_CMD_TIMEOUT_GLOBAL_SECONDS" envDefault:"0"`
CliCmdTimeoutJson string `env:"CLI_CMD_TIMEOUT_JSON" envDefault:""`
GoGitTimeout int `env:"GOGIT_TIMEOUT_SECONDS" envDefault:"10"`
GoGitTimeout int `env:"GOGIT_TIMEOUT_SECONDS" envDefault:"10" `
}

func ParseConfiguration() (*Configuration, error) {
Expand Down
12 changes: 8 additions & 4 deletions pkg/git/RepositoryManagerAnalytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func transform(src *object.Commit, tag *object.Tag) (dst *Commit) {
func (impl RepositoryManagerAnalyticsImpl) ChangesSinceByRepositoryForAnalytics(gitCtx GitContext, checkoutPath string, Old string, New string) (*GitChanges, error) {
var err error
start := time.Now()
useGitCli := impl.configuration.UseGitCli || impl.configuration.UseGitCliAnalytics
defer func() {
util.TriggerGitOperationMetrics("changesSinceByRepositoryForAnalytics", start, err)
}()
Expand All @@ -214,7 +215,7 @@ func (impl RepositoryManagerAnalyticsImpl) ChangesSinceByRepositoryForAnalytics(
oldHash := plumbing.NewHash(Old)

var fileStats FileStats
if strings.Contains(checkoutPath, "/.git") || impl.configuration.UseGitCli {
if strings.Contains(checkoutPath, "/.git") || useGitCli {
oldHashString := oldHash.String()
newHashString := newHash.String()
fileStats, err = impl.gitManager.FetchDiffStatBetweenCommitsWithNumstat(gitCtx, oldHashString, newHashString, checkoutPath)
Expand Down Expand Up @@ -243,14 +244,17 @@ func (impl RepositoryManagerAnalyticsImpl) ChangesSinceByRepositoryForAnalytics(
func (impl RepositoryManagerAnalyticsImpl) computeCommitDiff(gitCtx GitContext, checkoutPath string, oldHash plumbing.Hash, newHash plumbing.Hash, repository *GitRepository) ([]*Commit, error) {
var commitsCli, commitsGoGit []*Commit
var err error
if impl.configuration.UseGitCli || impl.configuration.AnalyticsDebug {
useGitCli := impl.configuration.UseGitCli || impl.configuration.UseGitCliAnalytics
if useGitCli || impl.configuration.AnalyticsDebug {
impl.logger.Infow("Computing commit diff using cli ", "checkoutPath", checkoutPath)
commitsCli, err = impl.gitManager.LogMergeBase(gitCtx, checkoutPath, oldHash.String(), newHash.String())
if err != nil {
impl.logger.Errorw("error in fetching commits for analytics through CLI: ", "err", err)
return nil, err
}
}
if !impl.configuration.UseGitCli || impl.configuration.AnalyticsDebug {
if !useGitCli || impl.configuration.AnalyticsDebug {
impl.logger.Infow("Computing commit diff using go-git ", "checkoutPath", checkoutPath)
ctx, cancel := gitCtx.WithTimeout(impl.configuration.GoGitTimeout)
defer cancel()
commitsGoGit, err = RunWithTimeout(ctx, func() ([]*Commit, error) {
Expand All @@ -265,7 +269,7 @@ func (impl RepositoryManagerAnalyticsImpl) computeCommitDiff(gitCtx GitContext,
impl.logOldestCommitComparison(commitsGoGit, commitsCli, checkoutPath, oldHash.String(), newHash.String())
}

if !impl.configuration.UseGitCli {
if !useGitCli {
return commitsGoGit, nil
}
return commitsCli, nil
Expand Down
Loading