2017-07-13 07:14:15 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-07-13 07:14:15 -04:00
|
|
|
|
2017-02-24 16:39:49 -05:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2019-12-20 12:07:12 -05:00
|
|
|
"net/http"
|
|
|
|
|
2022-05-11 06:09:36 -04:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2022-06-06 04:01:49 -04:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-05-11 06:21:34 -04:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-01-24 14:00:29 -05:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2024-02-27 02:12:22 -05:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-28 21:57:15 -05:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2017-02-24 16:39:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// listUserRepos - List the repositories owned by the given user.
|
2021-11-24 04:49:20 -05:00
|
|
|
func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) {
|
2020-06-13 07:35:59 -04:00
|
|
|
opts := utils.GetListOptions(ctx)
|
|
|
|
|
2023-10-15 11:46:06 -04:00
|
|
|
repos, count, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
|
2020-01-24 14:00:29 -05:00
|
|
|
Actor: u,
|
|
|
|
Private: private,
|
2020-06-13 07:35:59 -04:00
|
|
|
ListOptions: opts,
|
2020-07-02 10:55:42 -04:00
|
|
|
OrderBy: "id ASC",
|
2020-01-24 14:00:29 -05:00
|
|
|
})
|
2017-02-24 16:39:49 -05:00
|
|
|
if err != nil {
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err)
|
2017-02-24 16:39:49 -05:00
|
|
|
return
|
|
|
|
}
|
2018-11-28 06:26:14 -05:00
|
|
|
|
2023-03-13 07:31:41 -04:00
|
|
|
if err := repos.LoadAttributes(ctx); err != nil {
|
2022-01-25 01:33:40 -05:00
|
|
|
ctx.Error(http.StatusInternalServerError, "RepositoryList.LoadAttributes", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-23 16:23:27 -05:00
|
|
|
apiRepos := make([]*api.Repository, 0, len(repos))
|
2017-07-10 07:07:39 -04:00
|
|
|
for i := range repos {
|
2023-06-22 09:08:08 -04:00
|
|
|
permission, err := access_model.GetUserRepoPermission(ctx, repos[i], ctx.Doer)
|
2017-04-29 00:33:25 -04:00
|
|
|
if err != nil {
|
2023-06-22 09:08:08 -04:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
|
2017-07-10 07:07:39 -04:00
|
|
|
return
|
2017-04-29 00:33:25 -04:00
|
|
|
}
|
2024-05-03 03:58:31 -04:00
|
|
|
if ctx.IsSigned && ctx.Doer.IsAdmin || permission.HasAccess() {
|
2023-06-22 09:08:08 -04:00
|
|
|
apiRepos = append(apiRepos, convert.ToRepo(ctx, repos[i], permission))
|
2018-11-23 16:23:27 -05:00
|
|
|
}
|
2017-02-24 16:39:49 -05:00
|
|
|
}
|
2020-06-13 07:35:59 -04:00
|
|
|
|
|
|
|
ctx.SetLinkHeader(int(count), opts.PageSize)
|
2021-08-12 08:43:08 -04:00
|
|
|
ctx.SetTotalCountHeader(count)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.JSON(http.StatusOK, &apiRepos)
|
2017-02-24 16:39:49 -05:00
|
|
|
}
|
|
|
|
|
2017-07-10 07:07:39 -04:00
|
|
|
// ListUserRepos - list the repos owned by the given user.
|
2017-02-24 16:39:49 -05:00
|
|
|
func ListUserRepos(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05: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 14:00:29 -05:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 00:57:38 -04:00
|
|
|
// description: page size of results
|
2020-01-24 14:00:29 -05:00
|
|
|
// type: integer
|
2017-11-13 02:02:25 -05:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2023-09-12 22:37:54 -04:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 12:07:12 -05:00
|
|
|
|
2019-10-23 14:46:32 -04:00
|
|
|
private := ctx.IsSigned
|
2022-03-26 05:04:22 -04:00
|
|
|
listUserRepos(ctx, ctx.ContextUser, private)
|
2017-02-24 16:39:49 -05:00
|
|
|
}
|
|
|
|
|
2017-07-10 07:07:39 -04:00
|
|
|
// ListMyRepos - list the repositories you own or have access to.
|
2017-02-24 16:39:49 -05:00
|
|
|
func ListMyRepos(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:operation GET /user/repos user userCurrentListRepos
|
|
|
|
// ---
|
2021-07-21 12:49:10 -04:00
|
|
|
// summary: List the repos that the authenticated user owns
|
2017-11-13 02:02:25 -05:00
|
|
|
// produces:
|
|
|
|
// - application/json
|
2020-01-24 14:00:29 -05:00
|
|
|
// parameters:
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 00:57:38 -04:00
|
|
|
// description: page size of results
|
2020-01-24 14:00:29 -05:00
|
|
|
// type: integer
|
2024-07-06 04:43:34 -04:00
|
|
|
// - name: order_by
|
|
|
|
// in: query
|
|
|
|
// description: order the repositories by name (default), id, or size
|
|
|
|
// type: string
|
2017-11-13 02:02:25 -05:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2024-07-06 04:43:34 -04:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
2019-12-20 12:07:12 -05:00
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
opts := &repo_model.SearchRepoOptions{
|
2020-06-13 07:35:59 -04:00
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
2022-03-22 03:03:22 -04:00
|
|
|
Actor: ctx.Doer,
|
|
|
|
OwnerID: ctx.Doer.ID,
|
2020-06-13 07:35:59 -04:00
|
|
|
Private: ctx.IsSigned,
|
|
|
|
IncludeDescription: true,
|
2017-07-10 07:07:39 -04:00
|
|
|
}
|
2024-07-06 04:43:34 -04:00
|
|
|
orderBy := ctx.FormTrim("order_by")
|
|
|
|
switch orderBy {
|
|
|
|
case "name":
|
|
|
|
opts.OrderBy = "name ASC"
|
|
|
|
case "size":
|
|
|
|
opts.OrderBy = "size DESC"
|
|
|
|
case "id":
|
|
|
|
opts.OrderBy = "id ASC"
|
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "invalid order_by")
|
|
|
|
return
|
|
|
|
}
|
2020-06-13 07:35:59 -04:00
|
|
|
|
|
|
|
var err error
|
2022-11-19 03:12:33 -05:00
|
|
|
repos, count, err := repo_model.SearchRepository(ctx, opts)
|
2017-02-24 16:39:49 -05:00
|
|
|
if err != nil {
|
2020-06-13 07:35:59 -04:00
|
|
|
ctx.Error(http.StatusInternalServerError, "SearchRepository", err)
|
2017-07-10 07:07:39 -04:00
|
|
|
return
|
2017-02-24 16:39:49 -05:00
|
|
|
}
|
2017-07-10 07:07:39 -04:00
|
|
|
|
2020-06-13 07:35:59 -04:00
|
|
|
results := make([]*api.Repository, len(repos))
|
|
|
|
for i, repo := range repos {
|
2023-02-18 07:11:03 -05:00
|
|
|
if err = repo.LoadOwner(ctx); err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "LoadOwner", err)
|
2020-06-13 07:35:59 -04:00
|
|
|
return
|
|
|
|
}
|
2023-06-22 09:08:08 -04:00
|
|
|
permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer)
|
2020-06-13 07:35:59 -04:00
|
|
|
if err != nil {
|
2023-06-22 09:08:08 -04:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
|
2020-06-13 07:35:59 -04:00
|
|
|
}
|
2023-06-22 09:08:08 -04:00
|
|
|
results[i] = convert.ToRepo(ctx, repo, permission)
|
2017-02-24 16:39:49 -05:00
|
|
|
}
|
2020-06-13 07:35:59 -04:00
|
|
|
|
|
|
|
ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize)
|
2021-08-12 08:43:08 -04:00
|
|
|
ctx.SetTotalCountHeader(count)
|
2020-06-13 07:35:59 -04:00
|
|
|
ctx.JSON(http.StatusOK, &results)
|
2017-02-24 16:39:49 -05:00
|
|
|
}
|
2017-07-13 07:14:15 -04:00
|
|
|
|
|
|
|
// ListOrgRepos - list the repositories of an organization.
|
|
|
|
func ListOrgRepos(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05: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 14:00:29 -05:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 00:57:38 -04:00
|
|
|
// description: page size of results
|
2020-01-24 14:00:29 -05:00
|
|
|
// type: integer
|
2017-11-13 02:02:25 -05:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2023-09-12 22:37:54 -04:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 12:07:12 -05:00
|
|
|
|
2021-11-19 06:41:40 -05:00
|
|
|
listUserRepos(ctx, ctx.Org.Organization.AsUser(), ctx.IsSigned)
|
2017-07-13 07:14:15 -04:00
|
|
|
}
|