Skip to content

Commit c0f99e8

Browse files
awwalkerbkcsoft
authored andcommitted
API: support /users/:username/repos
clean up fix arguments remove repeated token give admins listing rights
1 parent 9084bdd commit c0f99e8

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

routers/api/v1/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func RegisterRoutes(m *macaron.Macaron) {
242242
m.Group("/:username", func() {
243243
m.Get("", user.GetInfo)
244244

245+
m.Get("/repos", user.ListUserRepos)
245246
m.Group("/tokens", func() {
246247
m.Combo("").Get(user.ListAccessTokens).
247248
Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
@@ -284,6 +285,9 @@ func RegisterRoutes(m *macaron.Macaron) {
284285
Delete(user.DeletePublicKey)
285286
})
286287

288+
m.Combo("/repos").Get(user.ListMyRepos).
289+
Post(bind(api.CreateRepoOption{}), repo.Create)
290+
287291
m.Group("/starred", func() {
288292
m.Get("", user.GetMyStarredRepos)
289293
m.Group("/:username/:reponame", func() {
@@ -297,8 +301,6 @@ func RegisterRoutes(m *macaron.Macaron) {
297301
}, reqToken())
298302

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

304306
m.Group("/repos", func() {

routers/api/v1/repo/repo.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,6 @@ func Search(ctx *context.APIContext) {
8181
})
8282
}
8383

84-
// ListMyRepos list all my repositories
85-
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
86-
func ListMyRepos(ctx *context.APIContext) {
87-
ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "")
88-
if err != nil {
89-
ctx.Error(500, "GetRepositories", err)
90-
return
91-
}
92-
numOwnRepos := len(ownRepos)
93-
94-
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
95-
if err != nil {
96-
ctx.Error(500, "GetRepositoryAccesses", err)
97-
return
98-
}
99-
100-
repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
101-
for i := range ownRepos {
102-
repos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
103-
}
104-
i := numOwnRepos
105-
106-
for repo, access := range accessibleRepos {
107-
repos[i] = repo.APIFormat(access)
108-
i++
109-
}
110-
111-
ctx.JSON(200, &repos)
112-
}
113-
11484
// CreateUserRepo create a repository for a user
11585
func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
11686
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{

routers/api/v1/user/repo.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package user
2+
3+
import (
4+
"code.gitea.io/gitea/models"
5+
"code.gitea.io/gitea/modules/context"
6+
api "code.gitea.io/sdk/gitea"
7+
)
8+
9+
// listUserRepos - List the repositories owned by the given user.
10+
func listUserRepos(ctx *context.APIContext, u *models.User) {
11+
userID := u.ID
12+
showPrivateRepos := (ctx.User.ID == userID || ctx.User.IsAdmin) && ctx.IsSigned
13+
ownRepos, err := models.GetUserRepositories(userID, showPrivateRepos, 1, u.NumRepos, "")
14+
if err != nil {
15+
ctx.Error(500, "GetUserRepositories", err)
16+
return
17+
}
18+
accessibleRepos, err := getAccessibleRepos(ctx)
19+
if err != nil {
20+
ctx.Error(500, "GetAccessibleRepos", err)
21+
}
22+
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleRepos))
23+
// Set owned repositories.
24+
for i := range ownRepos {
25+
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
26+
}
27+
// Set repositories user has access to.
28+
for i := len(ownRepos); i < len(apiRepos); i++ {
29+
apiRepos[i] = accessibleRepos[i]
30+
}
31+
ctx.JSON(200, &apiRepos)
32+
}
33+
34+
// ListUserRepos - list the repos owned and accessible by the given user.
35+
func ListUserRepos(ctx *context.APIContext) {
36+
user := GetUserByParams(ctx)
37+
if ctx.Written() {
38+
return
39+
}
40+
listUserRepos(ctx, user)
41+
}
42+
43+
// ListMyRepos - list the repositories owned by you.
44+
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
45+
func ListMyRepos(ctx *context.APIContext) {
46+
listUserRepos(ctx, ctx.User)
47+
}
48+
49+
// getAccessibleRepos - Get the repositories a user has access to.
50+
func getAccessibleRepos(ctx *context.APIContext) ([]*api.Repository, error) {
51+
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
52+
if err != nil {
53+
return nil, err
54+
}
55+
i := 0
56+
repos := make([]*api.Repository, len(accessibleRepos))
57+
for repo, access := range accessibleRepos {
58+
repos[i] = repo.APIFormat(access)
59+
i++
60+
}
61+
return repos, nil
62+
}

0 commit comments

Comments
 (0)