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: support /users/:username/repos #1059

Merged
merged 1 commit into from
Feb 27, 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
6 changes: 4 additions & 2 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/:username", func() {
m.Get("", user.GetInfo)

m.Get("/repos", user.ListUserRepos)
m.Group("/tokens", func() {
m.Combo("").Get(user.ListAccessTokens).
Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
Expand Down Expand Up @@ -284,6 +285,9 @@ func RegisterRoutes(m *macaron.Macaron) {
Delete(user.DeletePublicKey)
})

m.Combo("/repos").Get(user.ListMyRepos).
Post(bind(api.CreateRepoOption{}), repo.Create)

m.Group("/starred", func() {
m.Get("", user.GetMyStarredRepos)
m.Group("/:username/:reponame", func() {
Expand All @@ -297,8 +301,6 @@ func RegisterRoutes(m *macaron.Macaron) {
}, reqToken())

// Repositories
m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
Post(bind(api.CreateRepoOption{}), repo.Create)
m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)

m.Group("/repos", func() {
Expand Down
30 changes: 0 additions & 30 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,6 @@ func Search(ctx *context.APIContext) {
})
}

// ListMyRepos list all my repositories
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *context.APIContext) {
ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "")
if err != nil {
ctx.Error(500, "GetRepositories", err)
return
}
numOwnRepos := len(ownRepos)

accessibleRepos, err := ctx.User.GetRepositoryAccesses()
if err != nil {
ctx.Error(500, "GetRepositoryAccesses", err)
return
}

repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
for i := range ownRepos {
repos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
}
i := numOwnRepos

for repo, access := range accessibleRepos {
repos[i] = repo.APIFormat(access)
i++
}

ctx.JSON(200, &repos)
}

// CreateUserRepo create a repository for a user
func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
Expand Down
62 changes: 62 additions & 0 deletions routers/api/v1/user/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package user

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/sdk/gitea"
)

// listUserRepos - List the repositories owned by the given user.
func listUserRepos(ctx *context.APIContext, u *models.User) {
userID := u.ID
showPrivateRepos := (ctx.User.ID == userID || ctx.User.IsAdmin) && ctx.IsSigned
ownRepos, err := models.GetUserRepositories(userID, showPrivateRepos, 1, u.NumRepos, "")
if err != nil {
ctx.Error(500, "GetUserRepositories", err)
return
}
accessibleRepos, err := getAccessibleRepos(ctx)
if err != nil {
ctx.Error(500, "GetAccessibleRepos", err)
}
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleRepos))
// Set owned repositories.
for i := range ownRepos {
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
}
// Set repositories user has access to.
for i := len(ownRepos); i < len(apiRepos); i++ {
apiRepos[i] = accessibleRepos[i]
}
ctx.JSON(200, &apiRepos)
}

// ListUserRepos - list the repos owned and accessible by the given user.
func ListUserRepos(ctx *context.APIContext) {
user := GetUserByParams(ctx)
if ctx.Written() {
return
}
listUserRepos(ctx, user)
}

// ListMyRepos - list the repositories owned by you.
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *context.APIContext) {
listUserRepos(ctx, ctx.User)
}

// getAccessibleRepos - Get the repositories a user has access to.
func getAccessibleRepos(ctx *context.APIContext) ([]*api.Repository, error) {
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
if err != nil {
return nil, err
}
i := 0
repos := make([]*api.Repository, len(accessibleRepos))
for repo, access := range accessibleRepos {
repos[i] = repo.APIFormat(access)
i++
}
return repos, nil
}