Skip to content

Commit

Permalink
Use GetDiffShortStat to obtain diff changes. Add descriptive comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chief-Detektor committed Jan 5, 2024
1 parent 65241f0 commit f0c640c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
5 changes: 3 additions & 2 deletions models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,11 @@ func (pr *PullRequest) GetGitHeadBranchRefName() string {
return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch)
}

// GetReviewCommentsCount returns the number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int {
opts := FindCommentsOptions{
Type: CommentTypeReview,
IssueID: pr.IssueID,
Type: CommentTypeReview,
IssueID: pr.IssueID,
}
conds := opts.ToConds()
if pr.ID == 0 {
Expand Down
9 changes: 5 additions & 4 deletions modules/structs/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ type PullRequest struct {
Draft bool `json:"draft"`
IsLocked bool `json:"is_locked"`
Comments int `json:"comments"`
ReviewComments int `json:"review_comments"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
ChangedFiles int `json:"changed_files"`
// number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
ReviewComments int `json:"review_comments"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
ChangedFiles int `json:"changed_files"`

HTMLURL string `json:"html_url"`
DiffURL string `json:"diff_url"`
Expand Down
19 changes: 2 additions & 17 deletions services/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/gitdiff"
)

// ToAPIPullRequest assumes following fields have been assigned with valid values:
Expand Down Expand Up @@ -203,24 +201,11 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
// Calculate diff
startCommitID = pr.MergeBase

// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
diff, err := gitdiff.GetDiff(ctx, gitRepo,
&gitdiff.DiffOptions{
BeforeCommitID: startCommitID,
AfterCommitID: endCommitID,
MaxLines: setting.Git.MaxGitDiffLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
MaxFiles: -1, // GetDiff() will return all files
WhitespaceBehavior: gitdiff.GetWhitespaceFlag("show-all"),
})
apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID)
if err != nil {
log.Error("GetDiff: %v", err)
log.Error("GetDiffShortStat: %v", err)
return nil
}

apiPullRequest.Additions = diff.TotalAddition
apiPullRequest.Deletions = diff.TotalDeletion
apiPullRequest.ChangedFiles = diff.NumFiles
}

if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
Expand Down

0 comments on commit f0c640c

Please sign in to comment.