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