Skip to content

Commit 1a7fc53

Browse files
ethantkoeniglunny
authored andcommitted
API endpoint for stargazers (#597)
1 parent 61306fa commit 1a7fc53

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

models/star.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ func IsStaring(userID, repoID int64) bool {
6363
// GetStargazers returns the users that starred the repo.
6464
func (repo *Repository) GetStargazers(page int) ([]*User, error) {
6565
users := make([]*User, 0, ItemsPerPage)
66-
err := x.
67-
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
68-
Where("star.repo_id = ?", repo.ID).
69-
Join("LEFT", "star", "`user`.id = star.uid").
70-
Find(&users)
71-
return users, err
66+
sess := x.Where("star.repo_id = ?", repo.ID).
67+
Join("LEFT", "star", "`user`.id = star.uid")
68+
if page > 0 {
69+
sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
70+
}
71+
return users, sess.Find(&users)
7272
}
7373

7474
// GetStarredRepos returns the repos the user starred.

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ func RegisterRoutes(m *macaron.Macaron) {
326326
Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
327327
Delete(reqRepoWriter(), repo.DeleteMilestone)
328328
})
329+
m.Get("/stargazers", repo.ListStargazers)
329330
m.Group("/subscription", func() {
330331
m.Get("", user.IsWatching)
331332
m.Put("", user.Watch)

routers/api/v1/repo/star.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repo
6+
7+
import (
8+
api "code.gitea.io/sdk/gitea"
9+
10+
"code.gitea.io/gitea/modules/context"
11+
)
12+
13+
// ListStargazers list a repository's stargazers
14+
func ListStargazers(ctx *context.APIContext) {
15+
stargazers, err := ctx.Repo.Repository.GetStargazers(-1)
16+
if err != nil {
17+
ctx.Error(500, "GetStargazers", err)
18+
return
19+
}
20+
users := make([]*api.User, len(stargazers))
21+
for i, stargazer := range stargazers {
22+
users[i] = stargazer.APIFormat()
23+
}
24+
ctx.JSON(200, users)
25+
}

0 commit comments

Comments
 (0)