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

API endpoint for stargazers #597

Merged
merged 1 commit into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions models/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func IsStaring(userID, repoID int64) bool {
// GetStargazers returns the users that starred the repo.
func (repo *Repository) GetStargazers(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage)
err := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("star.repo_id = ?", repo.ID).
Join("LEFT", "star", "`user`.id = star.uid").
Find(&users)
return users, err
sess := x.Where("star.repo_id = ?", repo.ID).
Join("LEFT", "star", "`user`.id = star.uid")
if page > 0 {
sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
}
return users, sess.Find(&users)
}

// GetStarredRepos returns the repos the user starred.
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func RegisterRoutes(m *macaron.Macaron) {
Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
Delete(reqRepoWriter(), repo.DeleteMilestone)
})
m.Get("/stargazers", repo.ListStargazers)
m.Group("/subscription", func() {
m.Get("", user.IsWatching)
m.Put("", user.Watch)
Expand Down
25 changes: 25 additions & 0 deletions routers/api/v1/repo/star.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repo

import (
api "code.gitea.io/sdk/gitea"

"code.gitea.io/gitea/modules/context"
)

// ListStargazers list a repository's stargazers
func ListStargazers(ctx *context.APIContext) {
stargazers, err := ctx.Repo.Repository.GetStargazers(-1)
if err != nil {
ctx.Error(500, "GetStargazers", err)
return
}
users := make([]*api.User, len(stargazers))
for i, stargazer := range stargazers {
users[i] = stargazer.APIFormat()
}
ctx.JSON(200, users)
}