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