From f7da8f6d57e39fc8400eafc4fbacc8ff8e724e92 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 18 Nov 2024 21:21:18 -0800 Subject: [PATCH 1/2] Remove num_watches column from table repository --- models/fixtures/repository.yml | 57 ------------------------------- models/migrations/migrations.go | 1 + models/migrations/v1_23/v310.go | 16 +++++++++ models/repo.go | 11 ------ models/repo/repo.go | 14 +++++++- models/repo/repo_list.go | 32 +++++++++++++++++ models/repo/watch.go | 16 ++++----- models/unittest/consistency.go | 7 +--- routers/api/v1/repo/subscriber.go | 8 ++++- routers/web/explore/repo.go | 14 ++++---- routers/web/repo/repo.go | 7 +++- routers/web/repo/view.go | 8 ++++- services/context/repo.go | 5 +++ services/convert/repository.go | 9 ++++- services/repository/migrate.go | 12 ------- services/user/delete.go | 12 ------- 16 files changed, 109 insertions(+), 120 deletions(-) create mode 100644 models/migrations/v1_23/v310.go diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index bbb028eb7bb0c..092e8964b7534 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -6,7 +6,6 @@ lower_name: repo1 name: repo1 default_branch: master - num_watches: 4 num_stars: 0 num_forks: 0 num_issues: 2 @@ -37,7 +36,6 @@ lower_name: repo2 name: repo2 default_branch: master - num_watches: 0 num_stars: 1 num_forks: 0 num_issues: 2 @@ -68,7 +66,6 @@ lower_name: repo3 name: repo3 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 1 @@ -99,7 +96,6 @@ lower_name: repo4 name: repo4 default_branch: master - num_watches: 0 num_stars: 1 num_forks: 0 num_issues: 0 @@ -129,7 +125,6 @@ owner_name: org3 lower_name: repo5 name: repo5 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 1 @@ -159,7 +154,6 @@ owner_name: user10 lower_name: repo6 name: repo6 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -189,7 +183,6 @@ owner_name: user10 lower_name: repo7 name: repo7 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -219,7 +212,6 @@ owner_name: user10 lower_name: repo8 name: repo8 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -249,7 +241,6 @@ owner_name: user11 lower_name: repo9 name: repo9 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -280,7 +271,6 @@ lower_name: repo10 name: repo10 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 1 num_issues: 0 @@ -311,7 +301,6 @@ lower_name: repo11 name: repo11 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -341,7 +330,6 @@ owner_name: user14 lower_name: test_repo_12 name: test_repo_12 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -371,7 +359,6 @@ owner_name: user14 lower_name: test_repo_13 name: test_repo_13 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -402,7 +389,6 @@ lower_name: test_repo_14 name: test_repo_14 description: test_description_14 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -433,7 +419,6 @@ lower_name: repo15 name: repo15 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -464,7 +449,6 @@ lower_name: repo16 name: repo16 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -494,7 +478,6 @@ owner_name: user15 lower_name: big_test_public_1 name: big_test_public_1 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -524,7 +507,6 @@ owner_name: user15 lower_name: big_test_public_2 name: big_test_public_2 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -554,7 +536,6 @@ owner_name: user15 lower_name: big_test_private_1 name: big_test_private_1 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -584,7 +565,6 @@ owner_name: user15 lower_name: big_test_private_2 name: big_test_private_2 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -614,7 +594,6 @@ owner_name: user16 lower_name: big_test_public_3 name: big_test_public_3 - num_watches: 1 num_stars: 1 num_forks: 0 num_issues: 0 @@ -644,7 +623,6 @@ owner_name: user16 lower_name: big_test_private_3 name: big_test_private_3 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -674,7 +652,6 @@ owner_name: org17 lower_name: big_test_public_4 name: big_test_public_4 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -704,7 +681,6 @@ owner_name: org17 lower_name: big_test_private_4 name: big_test_private_4 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -734,7 +710,6 @@ owner_name: user20 lower_name: big_test_public_mirror_5 name: big_test_public_mirror_5 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -764,7 +739,6 @@ owner_name: user20 lower_name: big_test_private_mirror_5 name: big_test_private_mirror_5 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -794,7 +768,6 @@ owner_name: org19 lower_name: big_test_public_mirror_6 name: big_test_public_mirror_6 - num_watches: 0 num_stars: 0 num_forks: 1 num_issues: 0 @@ -824,7 +797,6 @@ owner_name: org19 lower_name: big_test_private_mirror_6 name: big_test_private_mirror_6 - num_watches: 0 num_stars: 0 num_forks: 1 num_issues: 0 @@ -854,7 +826,6 @@ owner_name: user20 lower_name: big_test_public_fork_7 name: big_test_public_fork_7 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -884,7 +855,6 @@ owner_name: user20 lower_name: big_test_private_fork_7 name: big_test_private_fork_7 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -915,7 +885,6 @@ lower_name: repo20 name: repo20 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -945,7 +914,6 @@ owner_name: org3 lower_name: repo21 name: repo21 - num_watches: 1 num_stars: 1 num_forks: 0 num_issues: 2 @@ -976,7 +944,6 @@ lower_name: utf8 name: utf8 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1006,7 +973,6 @@ owner_name: user21 lower_name: golang name: golang - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1036,7 +1002,6 @@ owner_name: user21 lower_name: graphql name: graphql - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1067,7 +1032,6 @@ lower_name: commits_search_test name: commits_search_test default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1098,7 +1062,6 @@ lower_name: git_hooks_test name: git_hooks_test default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1129,7 +1092,6 @@ lower_name: public_repo_on_limited_org name: public_repo_on_limited_org default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1160,7 +1122,6 @@ lower_name: private_repo_on_limited_org name: private_repo_on_limited_org default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1191,7 +1152,6 @@ lower_name: public_repo_on_private_org name: public_repo_on_private_org default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1222,7 +1182,6 @@ lower_name: private_repo_on_private_org name: private_repo_on_private_org default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1253,7 +1212,6 @@ lower_name: glob name: glob default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 1 @@ -1283,7 +1241,6 @@ owner_name: org26 lower_name: repo26 name: repo26 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1314,7 +1271,6 @@ lower_name: template1 name: template1 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1344,7 +1300,6 @@ owner_name: user27 lower_name: template2 name: template2 - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1375,7 +1330,6 @@ lower_name: repo_external_tracker name: repo_external_tracker default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1406,7 +1360,6 @@ lower_name: repo_external_tracker_numeric name: repo_external_tracker_numeric default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1437,7 +1390,6 @@ lower_name: repo_external_tracker_alpha name: repo_external_tracker_alpha default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1468,7 +1420,6 @@ lower_name: repo49 name: repo49 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1499,7 +1450,6 @@ lower_name: repo50 name: repo50 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 1 @@ -1530,7 +1480,6 @@ lower_name: repo51 name: repo51 default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 1 @@ -1561,7 +1510,6 @@ lower_name: empty name: empty default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1601,7 +1549,6 @@ num_closed_pulls: 0 num_milestones: 0 num_closed_milestones: 0 - num_watches: 0 num_projects: 0 num_closed_projects: 0 status: 0 @@ -1670,7 +1617,6 @@ lower_name: commitsonpr name: commitsonpr default_branch: main - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1714,7 +1660,6 @@ lower_name: repo60 name: repo60 default_branch: main - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1745,7 +1690,6 @@ lower_name: repo61 name: repo61 default_branch: main - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 @@ -1776,7 +1720,6 @@ lower_name: search-by-path name: search-by-path default_branch: master - num_watches: 0 num_stars: 0 num_forks: 0 num_issues: 0 diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index e0361af86ba8e..57a205dfa6e6a 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -367,6 +367,7 @@ func prepareMigrationTasks() []*migration { newMigration(307, "Fix milestone deadline_unix when there is no due date", v1_23.FixMilestoneNoDueDate), newMigration(308, "Add index(user_id, is_deleted) for action table", v1_23.AddNewIndexForUserDashboard), newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices), + newMigration(310, "Remove repository column num_watches", v1_23.RemoveRepoNumWatches), } return preparedMigrations } diff --git a/models/migrations/v1_23/v310.go b/models/migrations/v1_23/v310.go new file mode 100644 index 0000000000000..7639ee9001bed --- /dev/null +++ b/models/migrations/v1_23/v310.go @@ -0,0 +1,16 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_23 //nolint + +import ( + "code.gitea.io/gitea/models/migrations/base" + + "xorm.io/xorm" +) + +func RemoveRepoNumWatches(x *xorm.Engine) error { + sess := x.NewSession() + defer sess.Close() + return base.DropTableColumns(sess, "repository", "num_watches") +} diff --git a/models/repo.go b/models/repo.go index 0dc8ee5df33e7..bda9970b897bc 100644 --- a/models/repo.go +++ b/models/repo.go @@ -59,10 +59,6 @@ func StatsCorrectSQL(ctx context.Context, sql string, id int64) error { return err } -func repoStatsCorrectNumWatches(ctx context.Context, id int64) error { - return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?", id) -} - func repoStatsCorrectNumStars(ctx context.Context, id int64) error { return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?", id) } @@ -139,12 +135,6 @@ func CheckRepoStats(ctx context.Context) error { log.Trace("Doing: CheckRepoStats") checkers := []*repoChecker{ - // Repository.NumWatches - { - statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id AND mode<>2)"), - repoStatsCorrectNumWatches, - "repository count 'num_watches'", - }, // Repository.NumStars { statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)"), @@ -259,7 +249,6 @@ func UpdateRepoStats(ctx context.Context, id int64) error { var err error for _, f := range []func(ctx context.Context, id int64) error{ - repoStatsCorrectNumWatches, repoStatsCorrectNumStars, repoStatsCorrectNumIssues, repoStatsCorrectNumPulls, diff --git a/models/repo/repo.go b/models/repo/repo.go index 7d78cee2877d4..96f0e874b3559 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -140,7 +140,7 @@ type Repository struct { DefaultBranch string DefaultWikiBranch string - NumWatches int + NumWatches int `xorm:"-"` NumStars int NumForks int NumIssues int @@ -317,6 +317,18 @@ func (repo *Repository) LoadAttributes(ctx context.Context) error { return nil } +func (repo *Repository) LoadNumWatchers(ctx context.Context) error { + if repo.NumWatches > 0 { + return nil + } + cnt, err := CountRepoWatchers(ctx, repo.ID) + if err != nil { + return err + } + repo.NumWatches = int(cnt) + return nil +} + // FullName returns the repository full name func (repo *Repository) FullName() string { return repo.OwnerName + "/" + repo.Name diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go index 9bed2e919723b..17f2245613b19 100644 --- a/models/repo/repo_list.go +++ b/models/repo/repo_list.go @@ -146,6 +146,38 @@ func (repos RepositoryList) LoadLanguageStats(ctx context.Context) error { return nil } +func (repos RepositoryList) LoadNumWatchers(ctx context.Context) error { + if len(repos) == 0 { + return nil + } + + type results struct { + RepoID int64 + NumWatchers int64 + } + // Load watchers. + watches := make([]results, 0, len(repos)) + if err := db.GetEngine(ctx). + Select("repo_id, COUNT(*) as num_watchers"). + Table("watch"). + In("repo_id", repos.IDs()). + And("`mode` <> ?", WatchModeDont). + GroupBy("repo_id"). + Find(&watches); err != nil { + return fmt.Errorf("find watchers: %w", err) + } + + watchesMap := make(map[int64]int, len(repos)) + for _, watch := range watches { + watchesMap[watch.RepoID] = int(watch.NumWatchers) + } + + for i := range repos { + repos[i].NumWatches = watchesMap[repos[i].ID] + } + return nil +} + // LoadAttributes loads the attributes for the given RepositoryList func (repos RepositoryList) LoadAttributes(ctx context.Context) error { if err := repos.LoadOwners(ctx); err != nil { diff --git a/models/repo/watch.go b/models/repo/watch.go index a616544cae314..a390ce6be5900 100644 --- a/models/repo/watch.go +++ b/models/repo/watch.go @@ -75,13 +75,6 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error) hadrec := watch.Mode != WatchModeNone needsrec := mode != WatchModeNone - repodiff := 0 - - if IsWatchMode(mode) && !IsWatchMode(watch.Mode) { - repodiff = 1 - } else if !IsWatchMode(mode) && IsWatchMode(watch.Mode) { - repodiff = -1 - } watch.Mode = mode @@ -98,9 +91,6 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error) } else if _, err = db.DeleteByID[Watch](ctx, watch.ID); err != nil { return err } - if repodiff != 0 { - _, err = db.GetEngine(ctx).Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID) - } return err } @@ -162,6 +152,12 @@ func GetRepoWatchers(ctx context.Context, repoID int64, opts db.ListOptions) ([] return users, sess.Find(&users) } +func CountRepoWatchers(ctx context.Context, repoID int64) (int64, error) { + return db.GetEngine(ctx).Where("repo_id=?", repoID). + And("mode<>?", WatchModeDont). + Count(new(Watch)) +} + // WatchIfAuto subscribes to repo if AutoWatchOnChanges is set func WatchIfAuto(ctx context.Context, userID, repoID int64, isWrite bool) error { if !isWrite || !setting.Service.AutoWatchOnChanges { diff --git a/models/unittest/consistency.go b/models/unittest/consistency.go index 71839001be08d..455e81a20b99f 100644 --- a/models/unittest/consistency.go +++ b/models/unittest/consistency.go @@ -86,12 +86,7 @@ func init() { AssertExistsAndLoadMap(t, "repository", builder.Eq{"id": repo.int("ForkID")}) } - actual := GetCountByCond(t, "watch", builder.Eq{"repo_id": repo.int("ID")}. - And(builder.Neq{"mode": modelsRepoWatchModeDont})) - assert.EqualValues(t, repo.int("NumWatches"), actual, - "Unexpected number of watches for repo id: %d", repo.int("ID")) - - actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "repo_id": repo.int("ID")}) + actual := GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "repo_id": repo.int("ID")}) assert.EqualValues(t, repo.int("NumIssues"), actual, "Unexpected number of issues for repo id: %d", repo.int("ID")) diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go index 8584182857825..3b9a6933af0db 100644 --- a/routers/api/v1/repo/subscriber.go +++ b/routers/api/v1/repo/subscriber.go @@ -55,6 +55,12 @@ func ListSubscribers(ctx *context.APIContext) { users[i] = convert.ToUser(ctx, subscriber, ctx.Doer) } - ctx.SetTotalCountHeader(int64(ctx.Repo.Repository.NumWatches)) + total, err := repo_model.CountRepoWatchers(ctx, ctx.Repo.Repository.ID) + if err != nil { + ctx.Error(http.StatusInternalServerError, "CountRepoWatchers", err) + return + } + + ctx.SetTotalCountHeader(total) ctx.JSON(http.StatusOK, users) } diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 5b6f612e72201..0ebed34980476 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -50,12 +50,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { opts.PageSize = setting.UI.SitemapPagingNum } - var ( - repos []*repo_model.Repository - count int64 - err error - orderBy db.SearchOrderBy - ) + var orderBy db.SearchOrderBy sortOrder := ctx.FormString("sort") if sortOrder == "" { @@ -95,7 +90,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { private := ctx.FormOptionalBool("private") ctx.Data["IsPrivate"] = private - repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{ + repos, count, err := repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{ ListOptions: db.ListOptions{ Page: page, PageSize: opts.PageSize, @@ -133,6 +128,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { return } + if err := repos.LoadNumWatchers(ctx); err != nil { + ctx.ServerError("LoadNumWatchers", err) + return + } + ctx.Data["Keyword"] = keyword ctx.Data["Total"] = count ctx.Data["Repos"] = repos diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index be6f2d483ff31..68b94daf7525c 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -355,15 +355,20 @@ func Action(ctx *context.Context) { switch ctx.PathParam(":action") { case "watch", "unwatch", "star", "unstar": // we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed - ctx.Data["Repository"], err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name) + ctx.Repo.Repository, err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name) if err != nil { ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.PathParam(":action")), err) return } + ctx.Data["Repository"] = ctx.Repo.Repository } switch ctx.PathParam(":action") { case "watch", "unwatch": + if err := ctx.Repo.Repository.LoadNumWatchers(ctx); err != nil { + ctx.ServerError("LoadNumWatchers", err) + return + } ctx.HTML(http.StatusOK, tplWatchUnwatch) return case "star", "unstar": diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 5d68ace29b535..b421cfca0176d 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -1128,7 +1128,13 @@ func Watchers(ctx *context.Context) { ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers") ctx.Data["PageIsWatchers"] = true - RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) { + numWatchers, err := repo_model.CountRepoWatchers(ctx, ctx.Repo.Repository.ID) + if err != nil { + ctx.ServerError("CountRepoWatchers", err) + return + } + + RenderUserCards(ctx, int(numWatchers), func(opts db.ListOptions) ([]*user_model.User, error) { return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) }, tplWatchers) } diff --git a/services/context/repo.go b/services/context/repo.go index e7b32d62832d4..8087864041f56 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -399,6 +399,11 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { return } + if err = repo.LoadNumWatchers(ctx); err != nil { + ctx.ServerError("LoadNumWatchers", err) + return + } + ctx.Repo.Repository = repo ctx.Data["PushMirrors"] = pushMirrors ctx.Data["RepoName"] = ctx.Repo.Repository.Name diff --git a/services/convert/repository.go b/services/convert/repository.go index e026d0f4405f4..951074cd38012 100644 --- a/services/convert/repository.go +++ b/services/convert/repository.go @@ -177,6 +177,13 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR repoLicenses, err := repo_model.GetRepoLicenses(ctx, repo) if err != nil { + log.Error("GetRepoLicenses: %v", err) + return nil + } + + numWatchers, err := repo_model.CountRepoWatchers(ctx, repo.ID) + if err != nil { + log.Error("CountRepoWatchers: %v", err) return nil } @@ -206,7 +213,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR LanguagesURL: repoAPIURL + "/languages", Stars: repo.NumStars, Forks: repo.NumForks, - Watchers: repo.NumWatches, + Watchers: int(numWatchers), OpenIssues: repo.NumOpenIssues, OpenPulls: repo.NumOpenPulls, Releases: int(numReleases), diff --git a/services/repository/migrate.go b/services/repository/migrate.go index c627b46fab56f..34411f2404726 100644 --- a/services/repository/migrate.go +++ b/services/repository/migrate.go @@ -12,7 +12,6 @@ import ( "time" "code.gitea.io/gitea/models/db" - "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" @@ -72,17 +71,6 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, httpTransport *http.Transport, ) (*repo_model.Repository, error) { repoPath := repo_model.RepoPath(u.Name, opts.RepoName) - - if u.IsOrganization() { - t, err := organization.OrgFromUser(u).GetOwnerTeam(ctx) - if err != nil { - return nil, err - } - repo.NumWatches = t.NumMembers - } else { - repo.NumWatches = 1 - } - migrateTimeout := time.Duration(setting.Git.Timeout.Migrate) * time.Second if err := util.RemoveAll(repoPath); err != nil { diff --git a/services/user/delete.go b/services/user/delete.go index 39c6ef052dca7..9ea93553e7ae3 100644 --- a/services/user/delete.go +++ b/services/user/delete.go @@ -31,18 +31,6 @@ import ( func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) { e := db.GetEngine(ctx) - // ***** START: Watch ***** - watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id", - builder.Eq{"watch.user_id": u.ID}. - And(builder.Neq{"watch.mode": repo_model.WatchModeDont})) - if err != nil { - return fmt.Errorf("get all watches: %w", err) - } - if err = db.DecrByIDs(ctx, watchedRepoIDs, "num_watches", new(repo_model.Repository)); err != nil { - return fmt.Errorf("decrease repository num_watches: %w", err) - } - // ***** END: Watch ***** - // ***** START: Star ***** starredRepoIDs, err := db.FindIDs(ctx, "star", "star.repo_id", builder.Eq{"star.uid": u.ID}) From 1898a57472ffa257585e516e689e6abf619bfeaf Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 18 Nov 2024 22:27:07 -0800 Subject: [PATCH 2/2] Fix test --- models/repo/watch_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/models/repo/watch_test.go b/models/repo/watch_test.go index a95a267961638..3620ff84e08f7 100644 --- a/models/repo/watch_test.go +++ b/models/repo/watch_test.go @@ -33,8 +33,6 @@ func TestGetWatchers(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) watches, err := repo_model.GetWatchers(db.DefaultContext, repo.ID) assert.NoError(t, err) - // One watchers are inactive, thus minus 1 - assert.Len(t, watches, repo.NumWatches-1) for _, watch := range watches { assert.EqualValues(t, repo.ID, watch.RepoID) } @@ -50,7 +48,6 @@ func TestRepository_GetWatchers(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) assert.NoError(t, err) - assert.Len(t, watchers, repo.NumWatches) for _, watcher := range watchers { unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: watcher.ID, RepoID: repo.ID}) } @@ -69,11 +66,10 @@ func TestWatchIfAuto(t *testing.T) { watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) assert.NoError(t, err) - assert.Len(t, watchers, repo.NumWatches) setting.Service.AutoWatchOnChanges = false - prevCount := repo.NumWatches + prevCount := len(watchers) // Must not add watch assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))