2017-07-13 04:14:15 -07: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-24 14:39:49 -07:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2019-12-20 10:07:12 -07:00
|
|
|
"net/http"
|
|
|
|
|
2017-02-24 14:39:49 -07:00
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-09 18:27:50 -07:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 04:58:28 -07:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-02-24 14:39:49 -07:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-12-02 14:38:30 -07:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-05-11 03:21:34 -07:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-01-24 12:00:29 -07:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2017-02-24 14:39:49 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// listUserRepos - List the repositories owned by the given user.
|
2021-11-24 02:49:20 -07:00
|
|
|
func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) {
|
2020-06-13 04:35:59 -07:00
|
|
|
opts := utils.GetListOptions(ctx)
|
|
|
|
|
|
|
|
repos, count, err := models.GetUserRepositories(&models.SearchRepoOptions{
|
2020-01-24 12:00:29 -07:00
|
|
|
Actor: u,
|
|
|
|
Private: private,
|
2020-06-13 04:35:59 -07:00
|
|
|
ListOptions: opts,
|
2020-07-02 07:55:42 -07:00
|
|
|
OrderBy: "id ASC",
|
2020-01-24 12:00:29 -07:00
|
|
|
})
|
2017-02-24 14:39:49 -07:00
|
|
|
if err != nil {
|
2019-12-20 10:07:12 -07:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
2017-02-24 14:39:49 -07:00
|
|
|
return
|
|
|
|
}
|
2018-11-28 04:26:14 -07:00
|
|
|
|
2018-11-23 14:23:27 -07:00
|
|
|
apiRepos := make([]*api.Repository, 0, len(repos))
|
2017-07-10 04:07:39 -07:00
|
|
|
for i := range repos {
|
2018-11-28 04:26:14 -07:00
|
|
|
access, err := models.AccessLevel(ctx.User, repos[i])
|
2017-04-28 21:33:25 -07:00
|
|
|
if err != nil {
|
2019-12-20 10:07:12 -07:00
|
|
|
ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
|
2017-07-10 04:07:39 -07:00
|
|
|
return
|
2017-04-28 21:33:25 -07:00
|
|
|
}
|
2021-11-28 04:58:28 -07:00
|
|
|
if ctx.IsSigned && ctx.User.IsAdmin || access >= perm.AccessModeRead {
|
2020-12-02 14:38:30 -07:00
|
|
|
apiRepos = append(apiRepos, convert.ToRepo(repos[i], access))
|
2018-11-23 14:23:27 -07:00
|
|
|
}
|
2017-02-24 14:39:49 -07:00
|
|
|
}
|
2020-06-13 04:35:59 -07:00
|
|
|
|
|
|
|
ctx.SetLinkHeader(int(count), opts.PageSize)
|
2021-08-12 05:43:08 -07:00
|
|
|
ctx.SetTotalCountHeader(count)
|
2019-12-20 10:07:12 -07:00
|
|
|
ctx.JSON(http.StatusOK, &apiRepos)
|
2017-02-24 14:39:49 -07:00
|
|
|
}
|
|
|
|
|
2017-07-10 04:07:39 -07:00
|
|
|
// ListUserRepos - list the repos owned by the given user.
|
2017-02-24 14:39:49 -07:00
|
|
|
func ListUserRepos(ctx *context.APIContext) {
|
2017-11-13 00:02:25 -07: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 12:00:29 -07:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-08 21:57:38 -07:00
|
|
|
// description: page size of results
|
2020-01-24 12:00:29 -07:00
|
|
|
// type: integer
|
2017-11-13 00:02:25 -07:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 10:07:12 -07:00
|
|
|
|
2017-02-24 14:39:49 -07:00
|
|
|
user := GetUserByParams(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-10-23 11:46:32 -07:00
|
|
|
private := ctx.IsSigned
|
2018-11-23 14:23:27 -07:00
|
|
|
listUserRepos(ctx, user, private)
|
2017-02-24 14:39:49 -07:00
|
|
|
}
|
|
|
|
|
2017-07-10 04:07:39 -07:00
|
|
|
// ListMyRepos - list the repositories you own or have access to.
|
2017-02-24 14:39:49 -07:00
|
|
|
func ListMyRepos(ctx *context.APIContext) {
|
2017-11-13 00:02:25 -07:00
|
|
|
// swagger:operation GET /user/repos user userCurrentListRepos
|
|
|
|
// ---
|
2021-07-21 09:49:10 -07:00
|
|
|
// summary: List the repos that the authenticated user owns
|
2017-11-13 00:02:25 -07:00
|
|
|
// produces:
|
|
|
|
// - application/json
|
2020-01-24 12:00:29 -07:00
|
|
|
// parameters:
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-08 21:57:38 -07:00
|
|
|
// description: page size of results
|
2020-01-24 12:00:29 -07:00
|
|
|
// type: integer
|
2017-11-13 00:02:25 -07:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 10:07:12 -07:00
|
|
|
|
2020-06-13 04:35:59 -07:00
|
|
|
opts := &models.SearchRepoOptions{
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
Actor: ctx.User,
|
|
|
|
OwnerID: ctx.User.ID,
|
|
|
|
Private: ctx.IsSigned,
|
|
|
|
IncludeDescription: true,
|
2017-07-10 04:07:39 -07:00
|
|
|
}
|
2020-06-13 04:35:59 -07:00
|
|
|
|
|
|
|
var err error
|
|
|
|
repos, count, err := models.SearchRepository(opts)
|
2017-02-24 14:39:49 -07:00
|
|
|
if err != nil {
|
2020-06-13 04:35:59 -07:00
|
|
|
ctx.Error(http.StatusInternalServerError, "SearchRepository", err)
|
2017-07-10 04:07:39 -07:00
|
|
|
return
|
2017-02-24 14:39:49 -07:00
|
|
|
}
|
2017-07-10 04:07:39 -07:00
|
|
|
|
2020-06-13 04:35:59 -07:00
|
|
|
results := make([]*api.Repository, len(repos))
|
|
|
|
for i, repo := range repos {
|
2021-12-09 18:27:50 -07:00
|
|
|
if err = repo.GetOwner(db.DefaultContext); err != nil {
|
2020-06-13 04:35:59 -07:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetOwner", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
accessMode, err := models.AccessLevel(ctx.User, repo)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
|
|
|
|
}
|
2020-12-02 14:38:30 -07:00
|
|
|
results[i] = convert.ToRepo(repo, accessMode)
|
2017-02-24 14:39:49 -07:00
|
|
|
}
|
2020-06-13 04:35:59 -07:00
|
|
|
|
|
|
|
ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize)
|
2021-08-12 05:43:08 -07:00
|
|
|
ctx.SetTotalCountHeader(count)
|
2020-06-13 04:35:59 -07:00
|
|
|
ctx.JSON(http.StatusOK, &results)
|
2017-02-24 14:39:49 -07:00
|
|
|
}
|
2017-07-13 04:14:15 -07:00
|
|
|
|
|
|
|
// ListOrgRepos - list the repositories of an organization.
|
|
|
|
func ListOrgRepos(ctx *context.APIContext) {
|
2017-11-13 00:02:25 -07: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 12:00:29 -07:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-08 21:57:38 -07:00
|
|
|
// description: page size of results
|
2020-01-24 12:00:29 -07:00
|
|
|
// type: integer
|
2017-11-13 00:02:25 -07:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 10:07:12 -07:00
|
|
|
|
2021-11-19 04:41:40 -07:00
|
|
|
listUserRepos(ctx, ctx.Org.Organization.AsUser(), ctx.IsSigned)
|
2017-07-13 04:14:15 -07:00
|
|
|
}
|