2020-09-25 09:39:23 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 06:57:50 +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"
|
2020-09-25 09:39:23 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-11-28 08:12:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-09-25 09:39:23 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2021-11-16 19:00:11 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-09-25 09:39:23 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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 17:47:42 +05:30
|
|
|
if listOptions.Page == 0 {
|
|
|
|
listOptions.Page = 1
|
|
|
|
}
|
2021-11-16 19:00:11 +05:30
|
|
|
repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx.FormString("query"), &listOptions)
|
2020-09-25 09:39:23 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
2022-02-27 17:47:42 +05:30
|
|
|
return
|
2020-09-25 09:39:23 +05:30
|
|
|
}
|
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
ctx.SetTotalCountHeader(int64(count))
|
2020-09-25 09:39:23 +05:30
|
|
|
|
|
|
|
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 19:38:52 +05:30
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
2020-09-25 09:39:23 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2022-05-20 19:38:52 +05:30
|
|
|
has, err := repo_model.IsRepositoryExist(ctx, ctxUser, repoName)
|
2020-11-28 08:12:08 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 06:57:50 +05:30
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-28 08:12:08 +05:30
|
|
|
if err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-28 08:12:08 +05:30
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if _, err := repo_service.AdoptRepository(ctx.Doer, ctxUser, models.CreateRepoOptions{
|
2020-09-25 09:39:23 +05:30
|
|
|
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 19:38:52 +05:30
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
2020-09-25 09:39:23 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2022-05-20 19:38:52 +05:30
|
|
|
has, err := repo_model.IsRepositoryExist(ctx, ctxUser, repoName)
|
2020-11-28 08:12:08 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 06:57:50 +05:30
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-28 08:12:08 +05:30
|
|
|
if err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-28 08:12:08 +05:30
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctx.Doer, ctxUser, repoName); err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|