store PR commit count in the DB (#842)

This commit is contained in:
Marko Gacesa 2023-11-30 15:01:18 +00:00 committed by Harness
parent 5418dd77ef
commit 03b33aca77
11 changed files with 60 additions and 21 deletions

View File

@ -232,6 +232,7 @@ func (c *Controller) Merge(
pr.MergeSHA = &mergeOutput.MergeSHA
pr.MergeConflicts = nil
}
pr.Stats.DiffStats = types.NewDiffStats(mergeOutput.CommitCount, mergeOutput.ChangedFileCount)
return nil
})
if err != nil {
@ -291,6 +292,7 @@ func (c *Controller) Merge(
pr.MergeTargetSHA = &mergeOutput.BaseSHA
pr.MergeSHA = nil
pr.MergeConflicts = mergeOutput.ConflictFiles
pr.Stats.DiffStats = types.NewDiffStats(mergeOutput.CommitCount, mergeOutput.ChangedFileCount)
return nil
})
if err != nil {
@ -318,6 +320,7 @@ func (c *Controller) Merge(
pr.MergeBaseSHA = mergeOutput.MergeBaseSHA
pr.MergeSHA = &mergeOutput.MergeSHA
pr.MergeConflicts = nil
pr.Stats.DiffStats = types.NewDiffStats(mergeOutput.CommitCount, mergeOutput.ChangedFileCount)
// update sequence for PR activities
pr.ActivitySeq++

View File

@ -49,17 +49,18 @@ func (c *Controller) Find(
headRef := pr.SourceSHA
baseRef := pr.MergeBaseSHA
output, err := c.git.DiffStats(ctx, &git.DiffParams{
ReadParams: git.CreateReadParams(repo),
BaseRef: baseRef,
HeadRef: headRef,
})
if err != nil {
return nil, err
}
if pr.Stats.DiffStats.Commits == nil || pr.Stats.FilesChanged == nil {
output, err := c.git.DiffStats(ctx, &git.DiffParams{
ReadParams: git.CreateReadParams(repo),
BaseRef: baseRef,
HeadRef: headRef,
})
if err != nil {
return nil, err
}
pr.Stats.DiffStats.Commits = output.Commits
pr.Stats.DiffStats.FilesChanged = output.FilesChanged
pr.Stats.DiffStats = types.NewDiffStats(output.Commits, output.FilesChanged)
}
return pr, nil
}

View File

@ -121,10 +121,7 @@ func (c *Controller) DiffStats(
return types.DiffStats{}, err
}
return types.DiffStats{
Commits: output.Commits,
FilesChanged: output.FilesChanged,
}, nil
return types.NewDiffStats(output.Commits, output.FilesChanged), nil
}
func (c *Controller) Diff(

View File

@ -103,6 +103,9 @@ func (s *Service) triggerPREventOnBranchUpdate(ctx context.Context,
pr.MergeCheckStatus = enum.MergeCheckStatusUnchecked
pr.MergeSHA = nil
pr.MergeConflicts = nil
pr.Stats.DiffStats.Commits = nil
pr.Stats.DiffStats.FilesChanged = nil
return nil
})
if errors.Is(err, errPRNotOpen) {

View File

@ -257,6 +257,7 @@ func (s *Service) updateMergeDataInner(
pr.MergeSHA = &mergeOutput.MergeSHA
pr.MergeConflicts = nil
}
pr.Stats.DiffStats = types.NewDiffStats(mergeOutput.CommitCount, mergeOutput.ChangedFileCount)
return nil
})

View File

@ -0,0 +1,3 @@
ALTER TABLE pullreqs
DROP COLUMN pullreq_commit_count,
DROP COLUMN pullreq_file_count;

View File

@ -0,0 +1,3 @@
ALTER TABLE pullreqs
ADD COLUMN pullreq_commit_count INTEGER,
ADD COLUMN pullreq_file_count INTEGER;

View File

@ -0,0 +1,2 @@
ALTER TABLE pullreqs DROP COLUMN pullreq_commit_count;
ALTER TABLE pullreqs DROP COLUMN pullreq_file_count;

View File

@ -0,0 +1,2 @@
ALTER TABLE pullreqs ADD COLUMN pullreq_commit_count INTEGER;
ALTER TABLE pullreqs ADD COLUMN pullreq_file_count INTEGER;

View File

@ -89,6 +89,9 @@ type pullReq struct {
MergeBaseSHA string `db:"pullreq_merge_base_sha"`
MergeSHA null.String `db:"pullreq_merge_sha"`
MergeConflicts null.String `db:"pullreq_merge_conflicts"`
CommitCount null.Int `db:"pullreq_commit_count"`
FileCount null.Int `db:"pullreq_file_count"`
}
const (
@ -119,7 +122,9 @@ const (
,pullreq_merge_target_sha
,pullreq_merge_base_sha
,pullreq_merge_sha
,pullreq_merge_conflicts`
,pullreq_merge_conflicts
,pullreq_commit_count
,pullreq_file_count`
pullReqSelectBase = `
SELECT` + pullReqColumns + `
@ -209,6 +214,8 @@ func (s *PullReqStore) Create(ctx context.Context, pr *types.PullReq) error {
,pullreq_merge_base_sha
,pullreq_merge_sha
,pullreq_merge_conflicts
,pullreq_commit_count
,pullreq_file_count
) values (
:pullreq_version
,:pullreq_number
@ -236,6 +243,8 @@ func (s *PullReqStore) Create(ctx context.Context, pr *types.PullReq) error {
,:pullreq_merge_base_sha
,:pullreq_merge_sha
,:pullreq_merge_conflicts
,:pullreq_commit_count
,:pullreq_file_count
) RETURNING pullreq_id`
db := dbtx.GetAccessor(ctx, s.db)
@ -276,6 +285,8 @@ func (s *PullReqStore) Update(ctx context.Context, pr *types.PullReq) error {
,pullreq_merge_base_sha = :pullreq_merge_base_sha
,pullreq_merge_sha = :pullreq_merge_sha
,pullreq_merge_conflicts = :pullreq_merge_conflicts
,pullreq_commit_count = :pullreq_commit_count
,pullreq_file_count = :pullreq_file_count
WHERE pullreq_id = :pullreq_id AND pullreq_version = :pullreq_version - 1`
db := dbtx.GetAccessor(ctx, s.db)
@ -359,9 +370,11 @@ func (s *PullReqStore) UpdateMergeCheckStatus(
pullreq_updated = $1
,pullreq_merge_check_status = $2
,pullreq_version = pullreq_version + 1
,pullreq_commit_count = NULL
,pullreq_file_count = NULL
WHERE pullreq_target_repo_id = $3 AND
pullreq_target_branch = $4 AND
pullreq_state not in ($5, $6)`
pullreq_target_branch = $4 AND
pullreq_state not in ($5, $6)`
db := dbtx.GetAccessor(ctx, s.db)
@ -547,8 +560,8 @@ func mapPullReq(pr *pullReq) *types.PullReq {
Conversations: pr.CommentCount,
UnresolvedCount: pr.UnresolvedCount,
DiffStats: types.DiffStats{
Commits: 0,
FilesChanged: 0,
Commits: pr.CommitCount.Ptr(),
FilesChanged: pr.FileCount.Ptr(),
},
},
}
@ -584,6 +597,8 @@ func mapInternalPullReq(pr *types.PullReq) *pullReq {
MergeBaseSHA: pr.MergeBaseSHA,
MergeSHA: null.StringFromPtr(pr.MergeSHA),
MergeConflicts: null.NewString(mergeConflicts, mergeConflicts != ""),
CommitCount: null.IntFromPtr(pr.Stats.Commits),
FileCount: null.IntFromPtr(pr.Stats.FilesChanged),
}
return m

View File

@ -63,8 +63,17 @@ type PullReq struct {
// DiffStats shows total number of commits and modified files.
type DiffStats struct {
Commits int `json:"commits,omitempty"`
FilesChanged int `json:"files_changed,omitempty"`
Commits *int64 `json:"commits,omitempty"`
FilesChanged *int64 `json:"files_changed,omitempty"`
}
func NewDiffStats(commitCount int, fileCount int) DiffStats {
cc := int64(commitCount)
fc := int64(fileCount)
return DiffStats{
Commits: &cc,
FilesChanged: &fc,
}
}
// PullReqStats shows Diff statistics and number of conversations.