From ea731efb03cc748534292010e6be0ec7d1dcd1c6 Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 6 Jun 2022 23:59:50 +0200 Subject: [PATCH 1/8] Remove stars when repo goes private Fixes #18600 --- models/repo.go | 4 ++++ models/repo/star.go | 14 ++++++++++++++ models/repo/star_test.go | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/models/repo.go b/models/repo.go index 6c3dca41be062..da0703ae7aba5 100644 --- a/models/repo.go +++ b/models/repo.go @@ -524,6 +524,10 @@ func UpdateRepositoryCtx(ctx context.Context, repo *repo_model.Repository, visib if err != nil { return err } + + if err = repo_model.ZeroStarRepo(ctx, repo.ID); err != nil { + return err + } } // Create/Remove git-daemon-export-ok for git-daemon... diff --git a/models/repo/star.go b/models/repo/star.go index 113b56f595f50..6e2751c5e6a34 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -86,3 +86,17 @@ func GetStargazers(repo *Repository, opts db.ListOptions) ([]*user_model.User, e users := make([]*user_model.User, 0, 8) return users, sess.Find(&users) } + +// ZeroStarRepo clears all stars for a repository and from the user that starred it. +// Used when a repository is set to private. +func ZeroStarRepo(ctx context.Context, repoID int64) error { + if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repoID); err != nil { + return err + } + + if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = 0 WHERE id = ?", repoID); err != nil { + return err + } + + return db.DeleteBeans(ctx, Star{RepoID: repoID}) +} diff --git a/models/repo/star_test.go b/models/repo/star_test.go index 2dde09c74514b..3d36cae89217d 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -51,3 +51,19 @@ func TestRepository_GetStargazers2(t *testing.T) { assert.NoError(t, err) assert.Len(t, gazers, 0) } + +func TestZeroStarRepo(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + const userID = 2 + const repoID = 1 + unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + assert.NoError(t, StarRepo(userID, repoID, true)) + unittest.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) + assert.NoError(t, ZeroStarRepo(db.DefaultContext, repoID)) + unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + gazers, err := GetStargazers(repo, db.ListOptions{Page: 0}) + assert.NoError(t, err) + assert.Len(t, gazers, 0) +} From c57c5459d2bdc3ba5e2ec84dd4dec542a2a5e5b2 Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 7 Jun 2022 00:23:49 +0200 Subject: [PATCH 2/8] Fix with upstream refactoring --- models/repo/star_test.go | 16 +++++++++------- modules/repository/create.go | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/models/repo/star_test.go b/models/repo/star_test.go index 5316b26aa4cca..0cb4bc16d51fd 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -57,14 +57,16 @@ func TestZeroStarRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) const userID = 2 const repoID = 1 - unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) - assert.NoError(t, StarRepo(userID, repoID, true)) - unittest.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) - assert.NoError(t, ZeroStarRepo(db.DefaultContext, repoID)) - unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) + assert.NoError(t, repo_model.StarRepo(userID, repoID, true)) + unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) + assert.NoError(t, repo_model.StarRepo(userID, repoID, false)) + unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) + assert.NoError(t, repo_model.ZeroStarRepo(db.DefaultContext, repoID)) + unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) - repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - gazers, err := GetStargazers(repo, db.ListOptions{Page: 0}) + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) + gazers, err := repo_model.GetStargazers(repo, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) } diff --git a/modules/repository/create.go b/modules/repository/create.go index 95bb825403913..29b516308f0ba 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -225,6 +225,10 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili if err != nil { return err } + + if err = repo_model.ZeroStarRepo(ctx, repo.ID); err != nil { + return err + } } // Create/Remove git-daemon-export-ok for git-daemon... From 3befc50ddd943cff0a90846811b1a8ad3d6141f5 Mon Sep 17 00:00:00 2001 From: Wim Date: Wed, 8 Jun 2022 23:05:40 +0200 Subject: [PATCH 3/8] Add tooltip warning about removal --- options/locale/locale_en-US.ini | 1 + templates/repo/settings/options.tmpl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index d52fe05569496..781bac2877ecf 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -876,6 +876,7 @@ mirror_password_blank_placeholder = (Unset) mirror_password_help = Change the username to erase a stored password. watchers = Watchers stargazers = Stargazers +stars_remove_warning = This will remove all stars from this repository. forks = Forks pick_reaction = Pick your reaction reactions_more = and %d more diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index e2d6c5e1d5039..1794640fc95c3 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -30,7 +30,7 @@ {{if not .Repository.IsFork}}
-
+
{{if .IsAdmin}} {{else}} From 330ea631a7ab074833bcc4e28b4b0e5e85a33240 Mon Sep 17 00:00:00 2001 From: Wim Date: Wed, 8 Jun 2022 23:24:21 +0200 Subject: [PATCH 4/8] Update templates/repo/settings/options.tmpl Co-authored-by: silverwind --- templates/repo/settings/options.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 1794640fc95c3..06fbca40c9663 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -30,7 +30,7 @@ {{if not .Repository.IsFork}}
-
+
{{if .IsAdmin}} {{else}} From 63d6d1fe8e7ffcd2c2e528266adb124794d0cf2a Mon Sep 17 00:00:00 2001 From: Wim Date: Thu, 9 Jun 2022 00:24:39 +0200 Subject: [PATCH 5/8] Only show notice if stars != 0 --- templates/repo/settings/options.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 06fbca40c9663..cc2e21b9e4b58 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -30,7 +30,7 @@ {{if not .Repository.IsFork}}
-
+
{{if .IsAdmin}} {{else}} From b0b0201363ef83ca003835f86d85f0ca5ead49f0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 15 Jun 2022 08:16:54 +0200 Subject: [PATCH 6/8] rename func Co-authored-by: delvh --- models/repo/star.go | 4 ++-- models/repo/star_test.go | 4 ++-- modules/repository/create.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/models/repo/star.go b/models/repo/star.go index 6e2751c5e6a34..e43065423992a 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -87,9 +87,9 @@ func GetStargazers(repo *Repository, opts db.ListOptions) ([]*user_model.User, e return users, sess.Find(&users) } -// ZeroStarRepo clears all stars for a repository and from the user that starred it. +// ClearRepoStars clears all stars for a repository and from the user that starred it. // Used when a repository is set to private. -func ZeroStarRepo(ctx context.Context, repoID int64) error { +func ClearRepoStars(ctx context.Context, repoID int64) error { if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repoID); err != nil { return err } diff --git a/models/repo/star_test.go b/models/repo/star_test.go index 0cb4bc16d51fd..f985faadd270a 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -53,7 +53,7 @@ func TestRepository_GetStargazers2(t *testing.T) { assert.Len(t, gazers, 0) } -func TestZeroStarRepo(t *testing.T) { +func TestClearRepoStars(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) const userID = 2 const repoID = 1 @@ -62,7 +62,7 @@ func TestZeroStarRepo(t *testing.T) { unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) assert.NoError(t, repo_model.StarRepo(userID, repoID, false)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) - assert.NoError(t, repo_model.ZeroStarRepo(db.DefaultContext, repoID)) + assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repoID)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) diff --git a/modules/repository/create.go b/modules/repository/create.go index 29b516308f0ba..12fd1aa98d575 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -226,7 +226,7 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili return err } - if err = repo_model.ZeroStarRepo(ctx, repo.ID); err != nil { + if err = repo_model.ClearRepoStars(ctx, repo.ID); err != nil { return err } } From cac2946bb40132a56f993aa5a032bfcab0be3706 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 15 Jun 2022 08:17:40 +0200 Subject: [PATCH 7/8] use greater instead of notequal Co-authored-by: delvh --- templates/repo/settings/options.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index cc2e21b9e4b58..0e1fe8d8e2568 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -30,7 +30,7 @@ {{if not .Repository.IsFork}}
-
+
{{if .IsAdmin}} {{else}} From 810e470704c6af225783f4902efa211f4e2e825d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 5 Jun 2023 15:00:34 +0200 Subject: [PATCH 8/8] adjust to latest codebase --- models/repo/star_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/repo/star_test.go b/models/repo/star_test.go index 09d6268a600f1..f15ac12ebea65 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -64,7 +64,7 @@ func TestClearRepoStars(t *testing.T) { assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repoID)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) gazers, err := repo_model.GetStargazers(repo, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0)