diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 28b3df6c4..b30504edd 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2466,6 +2466,18 @@ settings.thread_id = Thread ID settings.matrix.homeserver_url = Homeserver URL settings.matrix.room_id = Room ID settings.matrix.message_type = Message Type +settings.visibility.private.button = Make Private +settings.visibility.private.text = Changing the visibility to private will not only make the repo visible to only allowed members but may remove the relation between it and forks, watchers, and stars. +settings.visibility.private.bullet_title = Changing the visibility to private will: +settings.visibility.private.bullet_one = Make the repo visible to only allowed members. +settings.visibility.private.bullet_two = May remove the relation between it and forks, watchers, and stars. +settings.visibility.public.button = Make Public +settings.visibility.public.text = Changing the visibility to public will make the repo visible to anyone. +settings.visibility.public.bullet_title= Changing the visibility to public will: +settings.visibility.public.bullet_one = Make the repo visible to anyone. +settings.visibility.success = Repository visibility changed. +settings.visibility.error = An error occurred while trying to change the repo visibility. +settings.visibility.fork_error = Can't change the visibility of a forked repo. settings.archive.button = Archive Repo settings.archive.header = Archive This Repo settings.archive.text = Archiving the repo will make it entirely read-only. It will be hidden from the dashboard. Nobody (not even you!) will be able to make new commits, or open any issues or pull requests. diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go index 1e0349cde..3f9140857 100644 --- a/routers/web/repo/setting/setting.go +++ b/routers/web/repo/setting/setting.go @@ -170,15 +170,7 @@ func SettingsPost(ctx *context.Context) { form.Private = repo.BaseRepo.IsPrivate || repo.BaseRepo.Owner.Visibility == structs.VisibleTypePrivate } - visibilityChanged := repo.IsPrivate != form.Private - // when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public - if visibilityChanged && setting.Repository.ForcePrivate && !form.Private && !ctx.Doer.IsAdmin { - ctx.RenderWithErr(ctx.Tr("form.repository_force_private"), tplSettingsOptions, form) - return - } - - repo.IsPrivate = form.Private - if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil { + if err := repo_service.UpdateRepository(ctx, repo, false); err != nil { ctx.ServerError("UpdateRepository", err) return } @@ -940,6 +932,39 @@ func SettingsPost(ctx *context.Context) { log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name) ctx.Redirect(ctx.Repo.RepoLink + "/settings") + case "visibility": + if repo.IsFork { + ctx.Flash.Error(ctx.Tr("repo.settings.visibility.fork_error")) + ctx.Redirect(ctx.Repo.RepoLink + "/settings") + return + } + + var err error + + // when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public + if setting.Repository.ForcePrivate && repo.IsPrivate && !ctx.Doer.IsAdmin { + ctx.RenderWithErr(ctx.Tr("form.repository_force_private"), tplSettingsOptions, form) + return + } + + if repo.IsPrivate { + err = repo_service.MakeRepoPublic(ctx, repo) + } else { + err = repo_service.MakeRepoPrivate(ctx, repo) + } + + if err != nil { + log.Error("Tried to change the visibility of the repo: %s", err) + ctx.Flash.Error(ctx.Tr("repo.settings.visibility.error")) + ctx.Redirect(ctx.Repo.RepoLink + "/settings") + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.visibility.success")) + + log.Trace("Repository visibility changed: %s/%s", ctx.Repo.Owner.Name, repo.Name) + ctx.Redirect(ctx.Repo.RepoLink + "/settings") + default: ctx.NotFound("", nil) } diff --git a/services/repository/repository.go b/services/repository/repository.go index b7aac3cfe..5306e7d45 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -122,6 +122,31 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili return committer.Commit() } +func UpdateRepositoryVisibility(ctx context.Context, repo *repo_model.Repository, isPrivate bool) (err error) { + ctx, committer, err := db.TxContext(ctx) + if err != nil { + return err + } + + defer committer.Close() + + repo.IsPrivate = isPrivate + + if err = repo_module.UpdateRepository(ctx, repo, true); err != nil { + return fmt.Errorf("UpdateRepositoryVisibility: %w", err) + } + + return committer.Commit() +} + +func MakeRepoPublic(ctx context.Context, repo *repo_model.Repository) (err error) { + return UpdateRepositoryVisibility(ctx, repo, false) +} + +func MakeRepoPrivate(ctx context.Context, repo *repo_model.Repository) (err error) { + return UpdateRepositoryVisibility(ctx, repo, true) +} + // LinkedRepository returns the linked repo if any func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) { if a.IssueID != 0 { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 4f98133df..f12bbbdf4 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -23,20 +23,6 @@ - {{if not .Repository.IsFork}} -
- -
- {{if .IsAdmin}} - - {{else}} - - {{if and .Repository.IsPrivate $.ForcePrivate}}{{end}} - {{end}} - -
-
- {{end}}
@@ -786,6 +772,27 @@
+ {{if not .Repository.IsFork}} +
+
+
{{ctx.Locale.Tr "repo.visibility"}}
+ {{if .Repository.IsPrivate}} +
{{ctx.Locale.Tr "repo.settings.visibility.public.text"}}
+ {{else}} +
{{ctx.Locale.Tr "repo.settings.visibility.private.text"}}
+ {{end}} +
+
+ +
+
+ {{end}} {{if .Repository.IsMirror}}
@@ -1012,6 +1019,34 @@
+ {{if not .Repository.IsFork}} + + {{end}} + {{if .Repository.UnitEnabled $.Context ctx.Consts.RepoUnitTypeWiki}}