Skip to content

Commit c49823c

Browse files
committed
api: respect DISABLE_STARS config option
- add APIGone response type - re-generate Swagger spec if stars are disabled: - render 404 page on /repo/stars - return 410 error on star-related API endpoints and actions - return empty value on star model functions (StarRepo, IsStaring, GetStargazers)
1 parent 5c150ce commit c49823c

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

models/repo/star.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"code.gitea.io/gitea/models/db"
1010
user_model "code.gitea.io/gitea/models/user"
11+
"code.gitea.io/gitea/modules/setting"
1112
"code.gitea.io/gitea/modules/timeutil"
1213
)
1314

@@ -24,7 +25,12 @@ func init() {
2425
}
2526

2627
// StarRepo or unstar repository.
28+
//
29+
// Will do nothing if stars are disabled.
2730
func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star bool) error {
31+
if setting.Repository.DisableStars {
32+
return nil
33+
}
2834
ctx, committer, err := db.TxContext(ctx)
2935
if err != nil {
3036
return err
@@ -70,13 +76,23 @@ func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star
7076
}
7177

7278
// IsStaring checks if user has starred given repository.
79+
//
80+
// Will always return false if stars are disabled.
7381
func IsStaring(ctx context.Context, userID, repoID int64) bool {
82+
if setting.Repository.DisableStars {
83+
return false
84+
}
7485
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})
7586
return has
7687
}
7788

7889
// GetStargazers returns the users that starred the repo.
90+
//
91+
// Will always return an empty slice if stars are disabled.
7992
func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
93+
if setting.Repository.DisableStars {
94+
return make([]*user_model.User, 0), nil
95+
}
8096
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
8197
Join("LEFT", "star", "`user`.id = star.uid")
8298
if opts.Page > 0 {

routers/api/v1/repo/star.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88

99
repo_model "code.gitea.io/gitea/models/repo"
10+
"code.gitea.io/gitea/modules/setting"
1011
api "code.gitea.io/gitea/modules/structs"
1112
"code.gitea.io/gitea/routers/api/v1/utils"
1213
"code.gitea.io/gitea/services/context"
@@ -44,6 +45,13 @@ func ListStargazers(ctx *context.APIContext) {
4445
// "$ref": "#/responses/UserList"
4546
// "404":
4647
// "$ref": "#/responses/notFound"
48+
// "410":
49+
// "$ref": "#/responses/gone"
50+
51+
if setting.Repository.DisableStars {
52+
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
53+
return
54+
}
4755

4856
stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx))
4957
if err != nil {

routers/api/v1/user/star.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
access_model "code.gitea.io/gitea/models/perm/access"
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
user_model "code.gitea.io/gitea/models/user"
14+
"code.gitea.io/gitea/modules/setting"
1415
api "code.gitea.io/gitea/modules/structs"
1516
"code.gitea.io/gitea/routers/api/v1/utils"
1617
"code.gitea.io/gitea/services/context"
@@ -66,6 +67,13 @@ func GetStarredRepos(ctx *context.APIContext) {
6667
// "$ref": "#/responses/RepositoryList"
6768
// "404":
6869
// "$ref": "#/responses/notFound"
70+
// "410":
71+
// "$ref": "#/responses/gone"
72+
73+
if setting.Repository.DisableStars {
74+
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
75+
return
76+
}
6977

7078
private := ctx.ContextUser.ID == ctx.Doer.ID
7179
repos, err := getStarredRepos(ctx, ctx.ContextUser, private)
@@ -97,6 +105,13 @@ func GetMyStarredRepos(ctx *context.APIContext) {
97105
// responses:
98106
// "200":
99107
// "$ref": "#/responses/RepositoryList"
108+
// "410":
109+
// "$ref": "#/responses/gone"
110+
111+
if setting.Repository.DisableStars {
112+
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
113+
return
114+
}
100115

101116
repos, err := getStarredRepos(ctx, ctx.Doer, true)
102117
if err != nil {
@@ -128,6 +143,13 @@ func IsStarring(ctx *context.APIContext) {
128143
// "$ref": "#/responses/empty"
129144
// "404":
130145
// "$ref": "#/responses/notFound"
146+
// "410":
147+
// "$ref": "#/responses/gone"
148+
149+
if setting.Repository.DisableStars {
150+
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
151+
return
152+
}
131153

132154
if repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) {
133155
ctx.Status(http.StatusNoContent)
@@ -159,6 +181,13 @@ func Star(ctx *context.APIContext) {
159181
// "$ref": "#/responses/forbidden"
160182
// "404":
161183
// "$ref": "#/responses/notFound"
184+
// "410":
185+
// "$ref": "#/responses/gone"
186+
187+
if setting.Repository.DisableStars {
188+
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
189+
return
190+
}
162191

163192
err := repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, true)
164193
if err != nil {
@@ -193,6 +222,13 @@ func Unstar(ctx *context.APIContext) {
193222
// "$ref": "#/responses/empty"
194223
// "404":
195224
// "$ref": "#/responses/notFound"
225+
// "410":
226+
// "$ref": "#/responses/gone"
227+
228+
if setting.Repository.DisableStars {
229+
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
230+
return
231+
}
196232

197233
err := repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, false)
198234
if err != nil {

routers/web/repo/view.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ func Watchers(ctx *context.Context) {
346346

347347
// Stars render repository's starred users
348348
func Stars(ctx *context.Context) {
349+
if setting.Repository.DisableStars {
350+
ctx.NotFound("Stars disabled", nil)
351+
return
352+
}
349353
ctx.Data["Title"] = ctx.Tr("repo.stargazers")
350354
ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
351355
RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) {

services/context/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ type APINotFound struct{}
8989
// swagger:response conflict
9090
type APIConflict struct{}
9191

92+
// APIGone means the functionality has been disabled
93+
// swagger:response gone
94+
type APIGone struct {
95+
APIError
96+
}
97+
9298
// APIRedirect is a redirect response
9399
// swagger:response redirect
94100
type APIRedirect struct{}

templates/swagger/v1_json.tmpl

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)