diff options
author | awwalker <awwalker3@gmail.com> | 2017-02-24 16:39:49 -0500 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-02-27 07:46:01 +0100 |
commit | c0f99e82299a3f9a6679ae6d643f854418ca16af (patch) | |
tree | 0215be0bd7a5f0141415c67d103f729e0b152fdc /routers/api/v1/user/repo.go | |
parent | 9084bdd8639aecad35a3c6c6421b3550b6e8f53a (diff) | |
download | gitea-c0f99e82299a3f9a6679ae6d643f854418ca16af.tar.gz gitea-c0f99e82299a3f9a6679ae6d643f854418ca16af.zip |
API: support /users/:username/repos
clean up
fix arguments
remove repeated token
give admins listing rights
Diffstat (limited to 'routers/api/v1/user/repo.go')
-rw-r--r-- | routers/api/v1/user/repo.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go new file mode 100644 index 0000000000..d80e3a9baf --- /dev/null +++ b/routers/api/v1/user/repo.go @@ -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 +} |