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 setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-11-28 08:12:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-16 19:00:11 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-09-25 09:39:23 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// AdoptOrDeleteRepository adopts or deletes a repository
|
|
|
|
func AdoptOrDeleteRepository(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsRepos"] = true
|
|
|
|
allowAdopt := ctx.IsUserSiteAdmin() || setting.Repository.AllowAdoptionOfUnadoptedRepositories
|
|
|
|
ctx.Data["allowAdopt"] = allowAdopt
|
|
|
|
allowDelete := ctx.IsUserSiteAdmin() || setting.Repository.AllowDeleteOfUnadoptedRepositories
|
|
|
|
ctx.Data["allowDelete"] = allowDelete
|
|
|
|
|
2021-08-11 06:01:13 +05:30
|
|
|
dir := ctx.FormString("id")
|
|
|
|
action := ctx.FormString("action")
|
2020-09-25 09:39:23 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
ctxUser := ctx.Doer
|
2021-11-24 15:19:20 +05:30
|
|
|
root := user_model.UserPath(ctxUser.LowerName)
|
2020-09-25 09:39:23 +05:30
|
|
|
|
|
|
|
// check not a repo
|
2022-05-20 19:38:52 +05:30
|
|
|
has, err := repo_model.IsRepositoryExist(ctx, ctxUser, dir)
|
2020-11-28 08:12:08 +05:30
|
|
|
if err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.ServerError("IsRepositoryExist", err)
|
|
|
|
return
|
2020-11-28 08:12:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
isDir, err := util.IsDir(filepath.Join(root, dir+".git"))
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("IsDir", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-25 09:39:23 +05:30
|
|
|
// Fallthrough to failure mode
|
|
|
|
} else if action == "adopt" && allowAdopt {
|
2021-11-16 19:00:11 +05:30
|
|
|
if _, err := repo_service.AdoptRepository(ctxUser, ctxUser, models.CreateRepoOptions{
|
2020-09-25 09:39:23 +05:30
|
|
|
Name: dir,
|
|
|
|
IsPrivate: true,
|
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("repository.AdoptRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.adopt_preexisting_success", dir))
|
|
|
|
} else if action == "delete" && allowDelete {
|
2021-11-16 19:00:11 +05:30
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctxUser, ctxUser, dir); err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
ctx.ServerError("repository.AdoptRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/repos")
|
|
|
|
}
|