2020-09-24 22:09:23 -06:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-24 22:09:23 -06:00
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
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/setting"
|
2020-11-27 19:42:08 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-02-27 00:12:22 -07:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-11-16 06:30:11 -07:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2020-09-24 22:09:23 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// AdoptOrDeleteRepository adopts or deletes a repository
|
|
|
|
func AdoptOrDeleteRepository(ctx *context.Context) {
|
2023-02-01 15:56:10 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("settings.adopt")
|
2020-09-24 22:09:23 -06:00
|
|
|
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-10 18:31:13 -06:00
|
|
|
dir := ctx.FormString("id")
|
|
|
|
action := ctx.FormString("action")
|
2020-09-24 22:09:23 -06:00
|
|
|
|
2022-03-22 01:03:22 -06:00
|
|
|
ctxUser := ctx.Doer
|
2021-11-24 02:49:20 -07:00
|
|
|
root := user_model.UserPath(ctxUser.LowerName)
|
2020-09-24 22:09:23 -06:00
|
|
|
|
|
|
|
// check not a repo
|
2023-04-28 12:14:26 -06:00
|
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, dir)
|
2020-11-27 19:42:08 -07:00
|
|
|
if err != nil {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.ServerError("IsRepositoryExist", err)
|
|
|
|
return
|
2020-11-27 19:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isDir, err := util.IsDir(filepath.Join(root, dir+".git"))
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("IsDir", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if has || !isDir {
|
2020-09-24 22:09:23 -06:00
|
|
|
// Fallthrough to failure mode
|
|
|
|
} else if action == "adopt" && allowAdopt {
|
2023-09-06 06:08:51 -06:00
|
|
|
if _, err := repo_service.AdoptRepository(ctx, ctxUser, ctxUser, repo_service.CreateRepoOptions{
|
2020-09-24 22:09:23 -06:00
|
|
|
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 {
|
2023-02-28 15:17:51 -07:00
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctx, ctxUser, ctxUser, dir); err != nil {
|
2020-09-24 22:09:23 -06:00
|
|
|
ctx.ServerError("repository.AdoptRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/repos")
|
|
|
|
}
|