Add commit diff stat to list commits (#1062)

eb/code-1016-2
Darko Draskovic 2024-02-23 11:27:06 +00:00 committed by Harness
parent f8f1453de1
commit 8150be7a3b
6 changed files with 39 additions and 28 deletions

View File

@ -160,6 +160,5 @@ func (c *Controller) CommitFiles(ctx context.Context,
return types.CommitFilesResponse{
CommitID: commit.CommitID,
RuleViolations: violations,
Stats: commit.Stats,
}, nil, nil
}

View File

@ -101,6 +101,11 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
Message: c.Message,
Author: *author,
Committer: *committer,
DiffStats: types.CommitDiffStats{
Additions: c.DiffStats.Additions,
Deletions: c.DiffStats.Deletions,
Total: c.DiffStats.Additions + c.DiffStats.Deletions,
},
}, nil
}

View File

@ -21,6 +21,8 @@ import (
"github.com/harness/gitness/errors"
"github.com/harness/gitness/git/types"
"github.com/rs/zerolog/log"
)
type GetCommitParams struct {
@ -37,6 +39,7 @@ type Commit struct {
Author Signature `json:"author"`
Committer Signature `json:"committer"`
FileStats CommitFileStats `json:"file_stats,omitempty"`
DiffStats CommitDiffStats `json:"diff_stats,omitempty"`
}
type GetCommitOutput struct {
@ -179,6 +182,20 @@ func (s *Service) ListCommits(ctx context.Context, params *ListCommitsParams) (*
if err != nil {
return nil, fmt.Errorf("failed to map rpc commit: %w", err)
}
stat, err := s.CommitShortStat(ctx, &CommitShortStatParams{
Path: repoPath,
Ref: commit.SHA,
})
if err != nil {
log.Warn().Msgf("failed to get diff stats: %s", err)
}
commit.DiffStats = CommitDiffStats{
Additions: stat.Additions,
Deletions: stat.Deletions,
Total: stat.Additions + stat.Deletions,
}
commits[i] = *commit
}

View File

@ -84,14 +84,13 @@ func (p *CommitFilesParams) Validate() error {
return p.WriteParams.Validate()
}
type CommitFileStat struct {
type CommitDiffStats struct {
Total int
Additions int
Deletions int
}
type CommitFilesResponse struct {
Stats CommitFileStat
CommitID string
}
@ -239,14 +238,6 @@ func (s *Service) CommitFiles(ctx context.Context, params *CommitFilesParams) (C
log.Debug().Msg("update ref")
stats, err := s.CommitShortStat(ctx, &CommitShortStatParams{
Path: repo.Path,
Ref: newCommitSHA,
})
if err != nil {
return CommitFilesResponse{}, fmt.Errorf("failed to get diff stats: %w", err)
}
branchRef := adapter.GetReferenceFromBranchName(params.Branch)
if params.Branch != params.NewBranch {
// we are creating a new branch, rather than updating the existing one
@ -276,11 +267,6 @@ func (s *Service) CommitFiles(ctx context.Context, params *CommitFilesParams) (C
return CommitFilesResponse{
CommitID: commit.ID.String(),
Stats: CommitFileStat{
Total: stats.Additions + stats.Deletions,
Additions: stats.Additions,
Deletions: stats.Deletions,
},
}, nil
}

View File

@ -14,12 +14,9 @@
package types
import "github.com/harness/gitness/git"
// CommitFilesResponse holds commit id.
type CommitFilesResponse struct {
DryRunRules bool `json:"dry_run_rules,omitempty"`
CommitID string `json:"commit_id"`
RuleViolations []RuleViolations `json:"rule_violations,omitempty"`
Stats git.CommitFileStat `json:"stats,omitempty"`
DryRunRules bool `json:"dry_run_rules,omitempty"`
CommitID string `json:"commit_id"`
RuleViolations []RuleViolations `json:"rule_violations,omitempty"`
}

View File

@ -56,13 +56,20 @@ type TagFilter struct {
Size int `json:"size"`
}
type CommitDiffStats struct {
Total int `json:"total,omitempty"`
Additions int `json:"additions,omitempty"`
Deletions int `json:"deletions,omitempty"`
}
type Commit struct {
SHA string `json:"sha"`
ParentSHAs []string `json:"parent_shas,omitempty"`
Title string `json:"title"`
Message string `json:"message"`
Author Signature `json:"author"`
Committer Signature `json:"committer"`
SHA string `json:"sha"`
ParentSHAs []string `json:"parent_shas,omitempty"`
Title string `json:"title"`
Message string `json:"message"`
Author Signature `json:"author"`
Committer Signature `json:"committer"`
DiffStats CommitDiffStats `json:"diff_stats"`
}
type Signature struct {