2020-09-25 04:09:23 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-25 04:09:23 +00:00
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-09-25 04:09:23 +00:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-11-28 02:42:08 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-09-25 04:09:23 +00:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2021-11-16 13:30:11 +00:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-09-25 04:09:23 +00: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 12:17:42 +00:00
|
|
|
if listOptions.Page == 0 {
|
|
|
|
listOptions.Page = 1
|
|
|
|
}
|
2023-02-28 22:17:51 +00:00
|
|
|
repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx, ctx.FormString("query"), &listOptions)
|
2020-09-25 04:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
2022-02-27 12:17:42 +00:00
|
|
|
return
|
2020-09-25 04:09:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 12:43:08 +00:00
|
|
|
ctx.SetTotalCountHeader(int64(count))
|
2020-09-25 04:09:23 +00: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 14:08:52 +00:00
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
2020-09-25 04:09:23 +00:00
|
|
|
if err != nil {
|
2021-11-24 09:49:20 +00:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2023-04-28 18:14:26 +00:00
|
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
|
2020-11-28 02:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 01:27:50 +00:00
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-28 02:42:08 +00:00
|
|
|
if err != nil {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-28 02:42:08 +00:00
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
2023-09-06 12:08:51 +00:00
|
|
|
if _, err := repo_service.AdoptRepository(ctx, ctx.Doer, ctxUser, repo_service.CreateRepoOptions{
|
2020-09-25 04:09:23 +00: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 14:08:52 +00:00
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
2020-09-25 04:09:23 +00:00
|
|
|
if err != nil {
|
2021-11-24 09:49:20 +00:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2023-04-28 18:14:26 +00:00
|
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
|
2020-11-28 02:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 01:27:50 +00:00
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-28 02:42:08 +00:00
|
|
|
if err != nil {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-28 02:42:08 +00:00
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctx, ctx.Doer, ctxUser, repoName); err != nil {
|
2020-09-25 04:09:23 +00:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|