2020-09-24 22:09:23 -06:00
|
|
|
// 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-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-09-24 22:09:23 -06:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-11-27 19:42:08 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-09-24 22:09:23 -06:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2021-11-16 06:30:11 -07:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-09-24 22:09:23 -06: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 05:18:23 -07:00
|
|
|
if listOptions.Page == 0 {
|
|
|
|
listOptions.Page = 1
|
|
|
|
}
|
2021-11-16 06:30:11 -07:00
|
|
|
repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx.FormString("query"), &listOptions)
|
2020-09-24 22:09:23 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
2022-02-27 05:18:23 -07:00
|
|
|
return
|
2020-09-24 22:09:23 -06:00
|
|
|
}
|
|
|
|
|
2021-08-12 06:43:08 -06:00
|
|
|
ctx.SetTotalCountHeader(int64(count))
|
2020-09-24 22:09:23 -06: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")
|
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
ctxUser, err := user_model.GetUserByName(ownerName)
|
2020-09-24 22:09:23 -06:00
|
|
|
if err != nil {
|
2021-11-24 02:49:20 -07:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2021-12-09 18:27:50 -07:00
|
|
|
has, err := repo_model.IsRepositoryExist(ctxUser, repoName)
|
2020-11-27 19:42:08 -07:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 18:27:50 -07:00
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-27 19:42:08 -07:00
|
|
|
if err != nil {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-27 19:42:08 -07:00
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
2021-11-16 06:30:11 -07:00
|
|
|
if _, err := repo_service.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{
|
2020-09-24 22:09:23 -06: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")
|
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
ctxUser, err := user_model.GetUserByName(ownerName)
|
2020-09-24 22:09:23 -06:00
|
|
|
if err != nil {
|
2021-11-24 02:49:20 -07:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not a repo
|
2021-12-09 18:27:50 -07:00
|
|
|
has, err := repo_model.IsRepositoryExist(ctxUser, repoName)
|
2020-11-27 19:42:08 -07:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 18:27:50 -07:00
|
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
2020-11-27 19:42:08 -07:00
|
|
|
if err != nil {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2020-11-27 19:42:08 -07:00
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-16 06:30:11 -07:00
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctx.User, ctxUser, repoName); err != nil {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|