mirror of
https://github.com/harness/drone.git
synced 2025-05-05 23:42:57 +00:00
Add omitempty to stats and run get change info for changes and types in parallel (#2105)
This commit is contained in:
parent
329181ed1a
commit
fe7b1941ef
@ -94,13 +94,6 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
|
|||||||
return nil, fmt.Errorf("failed to map committer: %w", err)
|
return nil, fmt.Errorf("failed to map committer: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var insertions int64
|
|
||||||
var deletions int64
|
|
||||||
for _, stat := range c.FileStats {
|
|
||||||
insertions += stat.Insertions
|
|
||||||
deletions += stat.Deletions
|
|
||||||
}
|
|
||||||
|
|
||||||
parentSHAs := make([]string, len(c.ParentSHAs))
|
parentSHAs := make([]string, len(c.ParentSHAs))
|
||||||
for i, sha := range c.ParentSHAs {
|
for i, sha := range c.ParentSHAs {
|
||||||
parentSHAs[i] = sha.String()
|
parentSHAs[i] = sha.String()
|
||||||
@ -113,15 +106,31 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
|
|||||||
Message: c.Message,
|
Message: c.Message,
|
||||||
Author: *author,
|
Author: *author,
|
||||||
Committer: *committer,
|
Committer: *committer,
|
||||||
Stats: types.CommitStats{
|
Stats: mapStats(c),
|
||||||
|
},
|
||||||
|
nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapStats(c *git.Commit) *types.CommitStats {
|
||||||
|
if len(c.FileStats) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var insertions int64
|
||||||
|
var deletions int64
|
||||||
|
for _, stat := range c.FileStats {
|
||||||
|
insertions += stat.Insertions
|
||||||
|
deletions += stat.Deletions
|
||||||
|
}
|
||||||
|
|
||||||
|
return &types.CommitStats{
|
||||||
Total: types.ChangeStats{
|
Total: types.ChangeStats{
|
||||||
Insertions: insertions,
|
Insertions: insertions,
|
||||||
Deletions: deletions,
|
Deletions: deletions,
|
||||||
Changes: insertions + deletions,
|
Changes: insertions + deletions,
|
||||||
},
|
},
|
||||||
Files: mapFileStats(c),
|
Files: mapFileStats(c),
|
||||||
},
|
}
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapFileStats(c *git.Commit) []types.CommitFileStats {
|
func mapFileStats(c *git.Commit) []types.CommitFileStats {
|
||||||
|
@ -31,6 +31,7 @@ import (
|
|||||||
"github.com/harness/gitness/git/sha"
|
"github.com/harness/gitness/git/sha"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CommitGPGSignature represents a git commit signature part.
|
// CommitGPGSignature represents a git commit signature part.
|
||||||
@ -255,15 +256,28 @@ func getCommitFileStats(
|
|||||||
repoPath string,
|
repoPath string,
|
||||||
sha sha.SHA,
|
sha sha.SHA,
|
||||||
) ([]CommitFileStats, error) {
|
) ([]CommitFileStats, error) {
|
||||||
|
g, ctx := errgroup.WithContext(ctx)
|
||||||
|
var changeInfoChanges map[string]changeInfoChange
|
||||||
var changeInfoTypes map[string]changeInfoType
|
var changeInfoTypes map[string]changeInfoType
|
||||||
changeInfoTypes, err := getChangeInfoTypes(ctx, repoPath, sha)
|
|
||||||
if err != nil {
|
g.Go(func() error {
|
||||||
|
var err error
|
||||||
|
changeInfoChanges, err = getChangeInfoChanges(ctx, repoPath, sha)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
g.Go(func() error {
|
||||||
|
var err error
|
||||||
|
changeInfoTypes, err = getChangeInfoTypes(ctx, repoPath, sha)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := g.Wait(); err != nil {
|
||||||
return nil, fmt.Errorf("failed to get change infos: %w", err)
|
return nil, fmt.Errorf("failed to get change infos: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
changeInfoChanges, err := getChangeInfoChanges(ctx, repoPath, sha)
|
if len(changeInfoTypes) == 0 {
|
||||||
if err != nil {
|
return []CommitFileStats{}, nil
|
||||||
return []CommitFileStats{}, fmt.Errorf("failed to get change infos: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileStats := make([]CommitFileStats, len(changeInfoChanges))
|
fileStats := make([]CommitFileStats, len(changeInfoChanges))
|
||||||
|
@ -83,7 +83,7 @@ type Commit struct {
|
|||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Author Signature `json:"author"`
|
Author Signature `json:"author"`
|
||||||
Committer Signature `json:"committer"`
|
Committer Signature `json:"committer"`
|
||||||
Stats CommitStats `json:"stats,omitempty"`
|
Stats *CommitStats `json:"stats,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Signature struct {
|
type Signature struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user