Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove stars when repo goes private #19904

Merged
merged 12 commits into from
Jun 5, 2023
14 changes: 14 additions & 0 deletions models/repo/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
6543 marked this conversation as resolved.
Show resolved Hide resolved
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})
}
18 changes: 18 additions & 0 deletions models/repo/star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ func TestRepository_GetStargazers2(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, gazers, 0)
}

func TestZeroStarRepo(t *testing.T) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, unittest.PrepareTestDatabase())
const userID = 2
const repoID = 1
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))
6543 marked this conversation as resolved.
Show resolved Hide resolved
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})

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)
}
4 changes: 4 additions & 0 deletions modules/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
6543 marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}

// Create/Remove git-daemon-export-ok for git-daemon...
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{{if not .Repository.IsFork}}
<div class="inline field">
<label>{{.i18n.Tr "repo.visibility"}}</label>
<div class="ui checkbox">
<div class="ui checkbox{{if and (not .Repository.IsPrivate) (ne .Repository.NumStars 0)}} tooltip{{end}}" data-content="{{.i18n.Tr "repo.stars_remove_warning"}}">
6543 marked this conversation as resolved.
Show resolved Hide resolved
{{if .IsAdmin}}
6543 marked this conversation as resolved.
Show resolved Hide resolved
<input name="private" type="checkbox" {{if .Repository.IsPrivate}}checked{{end}}>
{{else}}
Expand Down