Skip to content

Commit 8116742

Browse files
lunnysilverwind
andauthored
Fix viewed files number is not right if not all files loaded (go-gitea#35821) (go-gitea#35844)
Fix go-gitea#35803 Backport go-gitea#35821 Signed-off-by: silverwind <[email protected]> Co-authored-by: silverwind <[email protected]>
1 parent 0a9cbf3 commit 8116742

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

models/pull/review_state.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ func init() {
4949
db.RegisterModel(new(ReviewState))
5050
}
5151

52+
func (rs *ReviewState) GetViewedFileCount() int {
53+
if len(rs.UpdatedFiles) == 0 {
54+
return 0
55+
}
56+
var numViewedFiles int
57+
for _, state := range rs.UpdatedFiles {
58+
if state == Viewed {
59+
numViewedFiles++
60+
}
61+
}
62+
return numViewedFiles
63+
}
64+
5265
// GetReviewState returns the ReviewState with all given values prefilled, whether or not it exists in the database.
5366
// If the review didn't exist before in the database, it won't afterwards either.
5467
// The returned boolean shows whether the review exists in the database

routers/web/repo/pull.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,16 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
753753
// as the viewed information is designed to be loaded only on latest PR
754754
// diff and if you're signed in.
755755
var reviewState *pull_model.ReviewState
756+
var numViewedFiles int
756757
if ctx.IsSigned && isShowAllCommits {
757758
reviewState, err = gitdiff.SyncUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diff, diffOptions)
758759
if err != nil {
759760
ctx.ServerError("SyncUserSpecificDiff", err)
760761
return
761762
}
763+
if reviewState != nil {
764+
numViewedFiles = reviewState.GetViewedFileCount()
765+
}
762766
}
763767

764768
diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, beforeCommitID, afterCommitID)
@@ -767,10 +771,11 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
767771
return
768772
}
769773
ctx.Data["DiffShortStat"] = diffShortStat
774+
ctx.Data["NumViewedFiles"] = numViewedFiles
770775

771776
ctx.PageData["prReview"] = map[string]any{
772777
"numberOfFiles": diffShortStat.NumFiles,
773-
"numberOfViewedFiles": diff.NumViewedFiles,
778+
"numberOfViewedFiles": numViewedFiles,
774779
}
775780

776781
if err = diff.LoadComments(ctx, issue, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil {

services/gitdiff/gitdiff.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,9 @@ func getCommitFileLineCountAndLimitedContent(commit *git.Commit, filePath string
449449

450450
// Diff represents a difference between two git trees.
451451
type Diff struct {
452-
Start, End string
453-
Files []*DiffFile
454-
IsIncomplete bool
455-
NumViewedFiles int // user-specific
452+
Start, End string
453+
Files []*DiffFile
454+
IsIncomplete bool
456455
}
457456

458457
// LoadComments loads comments into each line
@@ -1342,7 +1341,6 @@ outer:
13421341
// Check whether the file has already been viewed
13431342
if fileViewedState == pull_model.Viewed {
13441343
diffFile.IsViewed = true
1345-
diff.NumViewedFiles++
13461344
}
13471345
}
13481346

templates/repo/diff/box.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
{{if and .PageIsPullFiles $.SignedUserID (not .DiffNotAvailable)}}
2828
<div class="not-mobile tw-flex tw-items-center tw-flex-col tw-whitespace-nowrap tw-mr-1">
2929
<label for="viewed-files-summary" id="viewed-files-summary-label" data-text-changed-template="{{ctx.Locale.Tr "repo.pulls.viewed_files_label"}}">
30-
{{ctx.Locale.Tr "repo.pulls.viewed_files_label" .Diff.NumViewedFiles .DiffShortStat.NumFiles}}
30+
{{ctx.Locale.Tr "repo.pulls.viewed_files_label" .NumViewedFiles .DiffShortStat.NumFiles}}
3131
</label>
32-
<progress id="viewed-files-summary" value="{{.Diff.NumViewedFiles}}" max="{{.DiffShortStat.NumFiles}}"></progress>
32+
<progress id="viewed-files-summary" value="{{.NumViewedFiles}}" max="{{.DiffShortStat.NumFiles}}"></progress>
3333
</div>
3434
{{end}}
3535
{{template "repo/diff/whitespace_dropdown" .}}

0 commit comments

Comments
 (0)