Skip to content

Commit 62ac325

Browse files
authored
Remove stars when repo goes private (#19904)
Fixes #18600
1 parent 8e63373 commit 62ac325

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

models/repo/star.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,17 @@ func GetStargazers(repo *Repository, opts db.ListOptions) ([]*user_model.User, e
8585
users := make([]*user_model.User, 0, 8)
8686
return users, sess.Find(&users)
8787
}
88+
89+
// ClearRepoStars clears all stars for a repository and from the user that starred it.
90+
// Used when a repository is set to private.
91+
func ClearRepoStars(ctx context.Context, repoID int64) error {
92+
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 {
93+
return err
94+
}
95+
96+
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = 0 WHERE id = ?", repoID); err != nil {
97+
return err
98+
}
99+
100+
return db.DeleteBeans(ctx, Star{RepoID: repoID})
101+
}

models/repo/star_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,21 @@ func TestRepository_GetStargazers2(t *testing.T) {
5151
assert.NoError(t, err)
5252
assert.Len(t, gazers, 0)
5353
}
54+
55+
func TestClearRepoStars(t *testing.T) {
56+
assert.NoError(t, unittest.PrepareTestDatabase())
57+
const userID = 2
58+
const repoID = 1
59+
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
60+
assert.NoError(t, repo_model.StarRepo(userID, repoID, true))
61+
unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
62+
assert.NoError(t, repo_model.StarRepo(userID, repoID, false))
63+
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
64+
assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repoID))
65+
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
66+
67+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
68+
gazers, err := repo_model.GetStargazers(repo, db.ListOptions{Page: 0})
69+
assert.NoError(t, err)
70+
assert.Len(t, gazers, 0)
71+
}

modules/repository/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili
397397
if err != nil {
398398
return err
399399
}
400+
401+
if err = repo_model.ClearRepoStars(ctx, repo.ID); err != nil {
402+
return err
403+
}
400404
}
401405

402406
// Create/Remove git-daemon-export-ok for git-daemon...

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ mirror_password_blank_placeholder = (Unset)
972972
mirror_password_help = Change the username to erase a stored password.
973973
watchers = Watchers
974974
stargazers = Stargazers
975+
stars_remove_warning = This will remove all stars from this repository.
975976
forks = Forks
976977
reactions_more = and %d more
977978
unit_disabled = The site administrator has disabled this repository section.

templates/repo/settings/options.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{{if not .Repository.IsFork}}
2727
<div class="inline field">
2828
<label>{{.locale.Tr "repo.visibility"}}</label>
29-
<div class="ui checkbox">
29+
<div class="ui checkbox{{if and (not .Repository.IsPrivate) (gt .Repository.NumStars 0)}} tooltip{{end}}" data-content="{{.locale.Tr "repo.stars_remove_warning"}}">
3030
{{if .IsAdmin}}
3131
<input name="private" type="checkbox" {{if .Repository.IsPrivate}}checked{{end}}>
3232
{{else}}

0 commit comments

Comments
 (0)