From 482ba937ed118d68448c5fe347ee7e43f5621c0b Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 2 Jul 2020 15:09:09 +0100 Subject: [PATCH] Provide option to unlink a fork (#11858) * Provide option to unlink a fork Fix #4566 Signed-off-by: Andrew Thornton * Apply suggestions from code review Co-authored-by: techknowlogick * Add check that user can create repo Signed-off-by: Andrew Thornton * as per @cirnoT Signed-off-by: Andrew Thornton Co-authored-by: techknowlogick --- models/repo.go | 12 +-- options/locale/locale_en-US.ini | 5 ++ routers/repo/setting.go | 41 +++++++++- templates/repo/settings/options.tmpl | 109 ++++++++++++++++++--------- web_src/less/_repository.less | 5 +- 5 files changed, 124 insertions(+), 48 deletions(-) diff --git a/models/repo.go b/models/repo.go index 3b874f335..c7d2ef467 100644 --- a/models/repo.go +++ b/models/repo.go @@ -209,19 +209,9 @@ func (repo *Repository) SanitizedOriginalURL() string { // ColorFormat returns a colored string to represent this repo func (repo *Repository) ColorFormat(s fmt.State) { - var ownerName interface{} - - if repo.OwnerName != "" { - ownerName = repo.OwnerName - } else if repo.Owner != nil { - ownerName = repo.Owner.Name - } else { - ownerName = log.NewColoredIDValue(strconv.FormatInt(repo.OwnerID, 10)) - } - log.ColorFprintf(s, "%d:%s/%s", log.NewColoredIDValue(repo.ID), - ownerName, + repo.OwnerName, repo.Name) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 7bde0d2af..036665256 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1382,6 +1382,11 @@ settings.convert_desc = You can convert this mirror into a regular repository. T settings.convert_notices_1 = This operation will convert the mirror into a regular repository and cannot be undone. settings.convert_confirm = Convert Repository settings.convert_succeed = The mirror has been converted into a regular repository. +settings.convert_fork = Convert to Regular Repository +settings.convert_fork_desc = You can convert this fork into a regular repository. This cannot be undone. +settings.convert_fork_notices_1 = This operation will convert the fork into a regular repository and cannot be undone. +settings.convert_fork_confirm = Convert Repository +settings.convert_fork_succeed = The fork has been converted into a regular repository. settings.transfer = Transfer Ownership settings.transfer_desc = Transfer this repository to a user or to an organization for which you have administrator rights. settings.transfer_notices_1 = - You will lose access to the repository if you transfer it to an individual user. diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 635ec4133..02331c232 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -356,9 +356,46 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { ctx.ServerError("DeleteMirrorByRepoID", err) return } - log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name) + log.Trace("Repository converted from mirror to regular: %s", repo.FullName()) ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed")) - ctx.Redirect(setting.AppSubURL + "/" + ctx.Repo.Owner.Name + "/" + repo.Name) + ctx.Redirect(repo.Link()) + + case "convert_fork": + if !ctx.Repo.IsOwner() { + ctx.Error(404) + return + } + if err := repo.GetOwner(); err != nil { + ctx.ServerError("Convert Fork", err) + return + } + if repo.Name != form.RepoName { + ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil) + return + } + + if !repo.IsFork { + ctx.Error(404) + return + } + + if !ctx.Repo.Owner.CanCreateRepo() { + ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit())) + ctx.Redirect(repo.Link() + "/settings") + return + } + + repo.IsFork = false + repo.ForkID = 0 + if err := models.UpdateRepository(repo, false); err != nil { + log.Error("Unable to update repository %-v whilst converting from fork", repo) + ctx.ServerError("Convert Fork", err) + return + } + + log.Trace("Repository converted from fork to regular: %s", repo.FullName()) + ctx.Flash.Success(ctx.Tr("repo.settings.convert_fork_succeed")) + ctx.Redirect(repo.Link()) case "transfer": if !ctx.Repo.IsOwner() { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index c674fcf7f..5666b8f95 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -358,17 +358,28 @@
{{if .Repository.IsMirror}} -
-
- +
+
+ +
+
+
{{.i18n.Tr "repo.settings.convert"}}
+

{{.i18n.Tr "repo.settings.convert_desc"}}

+
-
-
{{.i18n.Tr "repo.settings.convert"}}
-

{{.i18n.Tr "repo.settings.convert_desc"}}

+
+ {{end}} + {{if and .Repository.IsFork .Repository.Owner.CanCreateRepo}} +
+
+ +
+
+
{{.i18n.Tr "repo.settings.convert_fork"}}
+

{{.i18n.Tr "repo.settings.convert_fork_desc"}}

+
-
- -
+
{{end}}
@@ -437,37 +448,67 @@ {{if .Permission.IsOwner}} {{if .Repository.IsMirror}} -