Skip to content

Commit de26c8a

Browse files
lunnysilverwind
andauthored
Fix viewed files number is not right if not all files loaded (#35821)
Fix #35803 --------- Signed-off-by: silverwind <[email protected]> Co-authored-by: silverwind <[email protected]>
1 parent d9c0f86 commit de26c8a

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
@@ -782,12 +782,16 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
782782
// as the viewed information is designed to be loaded only on latest PR
783783
// diff and if you're signed in.
784784
var reviewState *pull_model.ReviewState
785+
var numViewedFiles int
785786
if ctx.IsSigned && isShowAllCommits {
786787
reviewState, err = gitdiff.SyncUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diff, diffOptions)
787788
if err != nil {
788789
ctx.ServerError("SyncUserSpecificDiff", err)
789790
return
790791
}
792+
if reviewState != nil {
793+
numViewedFiles = reviewState.GetViewedFileCount()
794+
}
791795
}
792796

793797
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, beforeCommitID, afterCommitID)
@@ -796,10 +800,11 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
796800
return
797801
}
798802
ctx.Data["DiffShortStat"] = diffShortStat
803+
ctx.Data["NumViewedFiles"] = numViewedFiles
799804

800805
ctx.PageData["prReview"] = map[string]any{
801806
"numberOfFiles": diffShortStat.NumFiles,
802-
"numberOfViewedFiles": diff.NumViewedFiles,
807+
"numberOfViewedFiles": numViewedFiles,
803808
}
804809

805810
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
@@ -520,10 +520,9 @@ func getCommitFileLineCountAndLimitedContent(commit *git.Commit, filePath string
520520

521521
// Diff represents a difference between two git trees.
522522
type Diff struct {
523-
Start, End string
524-
Files []*DiffFile
525-
IsIncomplete bool
526-
NumViewedFiles int // user-specific
523+
Start, End string
524+
Files []*DiffFile
525+
IsIncomplete bool
527526
}
528527

529528
// LoadComments loads comments into each line
@@ -1412,7 +1411,6 @@ outer:
14121411
// Check whether the file has already been viewed
14131412
if fileViewedState == pull_model.Viewed {
14141413
diffFile.IsViewed = true
1415-
diff.NumViewedFiles++
14161414
}
14171415
}
14181416

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)