2020-09-25 13:09:23 +09:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 03:20:29 +09:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-25 13:09:23 +09:00
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-10 10:27:50 +09:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-09-25 13:09:23 +09:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-08-25 11:31:57 +09:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-11-28 11:42:08 +09:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-09-25 13:09:23 +09:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2021-11-16 22:30:11 +09:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-09-25 13:09:23 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListUnadoptedRepositories lists the unadopted repositories that match the provided names
|
|
|
|
func ListUnadoptedRepositories(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /admin/unadopted admin adminUnadoptedList
|
|
|
|
// ---
|
|
|
|
// summary: List unadopted repositories
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// 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
|
|
|
|
// type: integer
|
|
|
|
// - name: pattern
|
|
|
|
// in: query
|
|
|
|
// description: pattern of repositories to search for
|
|
|
|
// type: string
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/StringSlice"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
|
|
|
listOptions := utils.GetListOptions(ctx)
|
2022-02-27 21:17:42 +09:00
|
|
|
if listOptions.Page == 0 {
|
|
|
|
listOptions.Page = 1
|
|
|
|
}
|
2023-03-01 07:17:51 +09:00
|
|
|
repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx, ctx.FormString("query"), &listOptions)
|
2020-09-25 13:09:23 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
2022-02-27 21:17:42 +09:00
|
|
|
return
|
2020-09-25 13:09:23 +09:00
|
|
|
}
|
|
|
|
|
2021-08-12 21:43:08 +09:00
|
|
|
ctx.SetTotalCountHeader(int64(count))
|
2020-09-25 13:09:23 +09:00
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, repoNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdoptRepository will adopt an unadopted repository
|
|
|
|
func AdoptRepository(ctx *context.APIContext) {
|
|
|
|
// swagger:operation POST /admin/unadopted/{owner}/{repo} admin adminAdoptRepository
|
|
|
|
// ---
|
|
|
|
// summary: Adopt unadopted files as a repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
ownerName := ctx.Params(":username")
|
|
|
|
repoName := ctx.Params(":reponame")
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
2020-09-25 13:09:23 +09:00
|
|
|
if err != nil {
|
2021-11-24 18:49:20 +09:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2023-04-29 03:14:26 +09:00
|
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
|
2020-11-28 11:42:08 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 10:27:50 +09:00
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-28 11:42:08 +09:00
|
|
|
if err != nil {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-28 11:42:08 +09:00
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
2023-03-01 07:17:51 +09:00
|
|
|
if _, err := repo_service.AdoptRepository(ctx, ctx.Doer, ctxUser, repo_module.CreateRepoOptions{
|
2020-09-25 13:09:23 +09:00
|
|
|
Name: repoName,
|
|
|
|
IsPrivate: true,
|
|
|
|
}); err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUnadoptedRepository will delete an unadopted repository
|
|
|
|
func DeleteUnadoptedRepository(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /admin/unadopted/{owner}/{repo} admin adminDeleteUnadoptedRepository
|
|
|
|
// ---
|
|
|
|
// summary: Delete unadopted files
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
ownerName := ctx.Params(":username")
|
|
|
|
repoName := ctx.Params(":reponame")
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
2020-09-25 13:09:23 +09:00
|
|
|
if err != nil {
|
2021-11-24 18:49:20 +09:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2023-04-29 03:14:26 +09:00
|
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
|
2020-11-28 11:42:08 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 10:27:50 +09:00
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-28 11:42:08 +09:00
|
|
|
if err != nil {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-28 11:42:08 +09:00
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-01 07:17:51 +09:00
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctx, ctx.Doer, ctxUser, repoName); err != nil {
|
2020-09-25 13:09:23 +09:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|