2017-07-13 14:14:15 +03:00
|
|
|
// 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.
|
|
|
|
|
2017-02-25 00:39:49 +03:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2019-12-20 20:07:12 +03:00
|
|
|
"net/http"
|
|
|
|
|
2017-02-25 00:39:49 +03:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-05-11 13:21:34 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-01-24 22:00:29 +03:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2017-02-25 00:39:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// listUserRepos - List the repositories owned by the given user.
|
2018-11-24 00:23:27 +03:00
|
|
|
func listUserRepos(ctx *context.APIContext, u *models.User, private bool) {
|
2020-01-24 22:00:29 +03:00
|
|
|
repos, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
|
|
|
Actor: u,
|
|
|
|
Private: private,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
})
|
2017-02-25 00:39:49 +03:00
|
|
|
if err != nil {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
2017-02-25 00:39:49 +03:00
|
|
|
return
|
|
|
|
}
|
2018-11-28 14:26:14 +03:00
|
|
|
|
2018-11-24 00:23:27 +03:00
|
|
|
apiRepos := make([]*api.Repository, 0, len(repos))
|
2017-07-10 14:07:39 +03:00
|
|
|
for i := range repos {
|
2018-11-28 14:26:14 +03:00
|
|
|
access, err := models.AccessLevel(ctx.User, repos[i])
|
2017-04-29 07:33:25 +03:00
|
|
|
if err != nil {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
|
2017-07-10 14:07:39 +03:00
|
|
|
return
|
2017-04-29 07:33:25 +03:00
|
|
|
}
|
2018-11-24 00:23:27 +03:00
|
|
|
if ctx.IsSigned && ctx.User.IsAdmin || access >= models.AccessModeRead {
|
|
|
|
apiRepos = append(apiRepos, repos[i].APIFormat(access))
|
|
|
|
}
|
2017-02-25 00:39:49 +03:00
|
|
|
}
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.JSON(http.StatusOK, &apiRepos)
|
2017-02-25 00:39:49 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 14:07:39 +03:00
|
|
|
// ListUserRepos - list the repos owned by the given user.
|
2017-02-25 00:39:49 +03:00
|
|
|
func ListUserRepos(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation GET /users/{username}/repos user userListRepos
|
|
|
|
// ---
|
|
|
|
// summary: List the repos owned by the given user
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-24 22:00:29 +03:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, maximum page size is 50
|
|
|
|
// type: integer
|
2017-11-13 10:02:25 +03:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2017-02-25 00:39:49 +03:00
|
|
|
user := GetUserByParams(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-10-23 21:46:32 +03:00
|
|
|
private := ctx.IsSigned
|
2018-11-24 00:23:27 +03:00
|
|
|
listUserRepos(ctx, user, private)
|
2017-02-25 00:39:49 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 14:07:39 +03:00
|
|
|
// ListMyRepos - list the repositories you own or have access to.
|
2017-02-25 00:39:49 +03:00
|
|
|
func ListMyRepos(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation GET /user/repos user userCurrentListRepos
|
|
|
|
// ---
|
|
|
|
// summary: List the repos that the authenticated user owns or has access to
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2020-01-24 22:00:29 +03:00
|
|
|
// parameters:
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, maximum page size is 50
|
|
|
|
// type: integer
|
2017-11-13 10:02:25 +03:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
ownRepos, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
|
|
|
Actor: ctx.User,
|
|
|
|
Private: true,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
})
|
2017-07-10 14:07:39 +03:00
|
|
|
if err != nil {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
2017-07-10 14:07:39 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
accessibleReposMap, err := ctx.User.GetRepositoryAccesses()
|
2017-02-25 00:39:49 +03:00
|
|
|
if err != nil {
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetRepositoryAccesses", err)
|
2017-07-10 14:07:39 +03:00
|
|
|
return
|
2017-02-25 00:39:49 +03:00
|
|
|
}
|
2017-07-10 14:07:39 +03:00
|
|
|
|
|
|
|
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleReposMap))
|
|
|
|
for i := range ownRepos {
|
|
|
|
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
|
|
|
|
}
|
|
|
|
i := len(ownRepos)
|
|
|
|
for repo, access := range accessibleReposMap {
|
|
|
|
apiRepos[i] = repo.APIFormat(access)
|
2017-02-25 00:39:49 +03:00
|
|
|
i++
|
|
|
|
}
|
2019-12-20 20:07:12 +03:00
|
|
|
ctx.JSON(http.StatusOK, &apiRepos)
|
2017-02-25 00:39:49 +03:00
|
|
|
}
|
2017-07-13 14:14:15 +03:00
|
|
|
|
|
|
|
// ListOrgRepos - list the repositories of an organization.
|
|
|
|
func ListOrgRepos(ctx *context.APIContext) {
|
2017-11-13 10:02:25 +03:00
|
|
|
// swagger:operation GET /orgs/{org}/repos organization orgListRepos
|
|
|
|
// ---
|
|
|
|
// summary: List an organization's repos
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-24 22:00:29 +03:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, maximum page size is 50
|
|
|
|
// type: integer
|
2017-11-13 10:02:25 +03:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 20:07:12 +03:00
|
|
|
|
2018-11-24 00:23:27 +03:00
|
|
|
listUserRepos(ctx, ctx.Org.Organization, ctx.IsSigned)
|
2017-07-13 14:14:15 +03:00
|
|
|
}
|