Skip to content

Commit e45a4c9

Browse files
Move repository visibility to danger zone in the settings area (#31126)
Moved repository visibility to the danger zone in the settings area. To change the visibility, it is necessary to go to the danger zone, click on the private/public button, and accept the change in the modal. Resolves: #23826 --- ## Screenshots <details> <summary>Before</summary> Private repo: ![Private repo](https://github.com/go-gitea/gitea/assets/65479069/4313492a-4854-48bc-9f47-974e3539d791) Public repo: ![Public repo](https://github.com/go-gitea/gitea/assets/65479069/1c45f6e4-ee93-4799-9331-e9d4a7e0f16a) </details> <details> <summary>After</summary> Make private: ![Screenshot from 2024-05-28 21-35-38](https://github.com/go-gitea/gitea/assets/65479069/4887e28a-0514-4990-aa69-bf3ddc7e6c7d) Make private modal ![Screenshot from 2024-06-13 23-55-55](https://github.com/go-gitea/gitea/assets/65479069/9f5a7604-069b-41a2-973b-ee2d58e85953) ![Screenshot from 2024-06-13 23-53-09](https://github.com/go-gitea/gitea/assets/65479069/06c22726-eab2-4bce-8df7-62849dcce974) Make public: ![Screenshot from 2024-05-28 21-34-27](https://github.com/go-gitea/gitea/assets/65479069/6d388f99-0356-48a0-9d85-320cdba55179) Make public modal ![Screenshot from 2024-06-13 23-53-37](https://github.com/go-gitea/gitea/assets/65479069/8944972e-f2d4-4aea-ba96-b892febb5ced) </details> --------- Co-authored-by: Kemal Zebari <[email protected]>
1 parent ff1779d commit e45a4c9

File tree

4 files changed

+120
-23
lines changed

4 files changed

+120
-23
lines changed

options/locale/locale_en-US.ini

+12
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,18 @@ settings.thread_id = Thread ID
24662466
settings.matrix.homeserver_url = Homeserver URL
24672467
settings.matrix.room_id = Room ID
24682468
settings.matrix.message_type = Message Type
2469+
settings.visibility.private.button = Make Private
2470+
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.
2471+
settings.visibility.private.bullet_title = <strong>Changing the visibility to private will:</strong>
2472+
settings.visibility.private.bullet_one = Make the repo visible to only allowed members.
2473+
settings.visibility.private.bullet_two = May remove the relation between it and <strong>forks</strong>, <strong>watchers</strong>, and <strong>stars</strong>.
2474+
settings.visibility.public.button = Make Public
2475+
settings.visibility.public.text = Changing the visibility to public will make the repo visible to anyone.
2476+
settings.visibility.public.bullet_title= <strong>Changing the visibility to public will:</strong>
2477+
settings.visibility.public.bullet_one = Make the repo visible to anyone.
2478+
settings.visibility.success = Repository visibility changed.
2479+
settings.visibility.error = An error occurred while trying to change the repo visibility.
2480+
settings.visibility.fork_error = Can't change the visibility of a forked repo.
24692481
settings.archive.button = Archive Repo
24702482
settings.archive.header = Archive This Repo
24712483
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.

routers/web/repo/setting/setting.go

+34-9
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,7 @@ func SettingsPost(ctx *context.Context) {
170170
form.Private = repo.BaseRepo.IsPrivate || repo.BaseRepo.Owner.Visibility == structs.VisibleTypePrivate
171171
}
172172

173-
visibilityChanged := repo.IsPrivate != form.Private
174-
// when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public
175-
if visibilityChanged && setting.Repository.ForcePrivate && !form.Private && !ctx.Doer.IsAdmin {
176-
ctx.RenderWithErr(ctx.Tr("form.repository_force_private"), tplSettingsOptions, form)
177-
return
178-
}
179-
180-
repo.IsPrivate = form.Private
181-
if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil {
173+
if err := repo_service.UpdateRepository(ctx, repo, false); err != nil {
182174
ctx.ServerError("UpdateRepository", err)
183175
return
184176
}
@@ -940,6 +932,39 @@ func SettingsPost(ctx *context.Context) {
940932
log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
941933
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
942934

935+
case "visibility":
936+
if repo.IsFork {
937+
ctx.Flash.Error(ctx.Tr("repo.settings.visibility.fork_error"))
938+
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
939+
return
940+
}
941+
942+
var err error
943+
944+
// when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public
945+
if setting.Repository.ForcePrivate && repo.IsPrivate && !ctx.Doer.IsAdmin {
946+
ctx.RenderWithErr(ctx.Tr("form.repository_force_private"), tplSettingsOptions, form)
947+
return
948+
}
949+
950+
if repo.IsPrivate {
951+
err = repo_service.MakeRepoPublic(ctx, repo)
952+
} else {
953+
err = repo_service.MakeRepoPrivate(ctx, repo)
954+
}
955+
956+
if err != nil {
957+
log.Error("Tried to change the visibility of the repo: %s", err)
958+
ctx.Flash.Error(ctx.Tr("repo.settings.visibility.error"))
959+
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
960+
return
961+
}
962+
963+
ctx.Flash.Success(ctx.Tr("repo.settings.visibility.success"))
964+
965+
log.Trace("Repository visibility changed: %s/%s", ctx.Repo.Owner.Name, repo.Name)
966+
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
967+
943968
default:
944969
ctx.NotFound("", nil)
945970
}

services/repository/repository.go

+25
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibili
122122
return committer.Commit()
123123
}
124124

125+
func UpdateRepositoryVisibility(ctx context.Context, repo *repo_model.Repository, isPrivate bool) (err error) {
126+
ctx, committer, err := db.TxContext(ctx)
127+
if err != nil {
128+
return err
129+
}
130+
131+
defer committer.Close()
132+
133+
repo.IsPrivate = isPrivate
134+
135+
if err = repo_module.UpdateRepository(ctx, repo, true); err != nil {
136+
return fmt.Errorf("UpdateRepositoryVisibility: %w", err)
137+
}
138+
139+
return committer.Commit()
140+
}
141+
142+
func MakeRepoPublic(ctx context.Context, repo *repo_model.Repository) (err error) {
143+
return UpdateRepositoryVisibility(ctx, repo, false)
144+
}
145+
146+
func MakeRepoPrivate(ctx context.Context, repo *repo_model.Repository) (err error) {
147+
return UpdateRepositoryVisibility(ctx, repo, true)
148+
}
149+
125150
// LinkedRepository returns the linked repo if any
126151
func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) {
127152
if a.IssueID != 0 {

templates/repo/settings/options.tmpl

+49-14
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@
2323
<label>{{ctx.Locale.Tr "repo.template_helper"}}</label>
2424
</div>
2525
</div>
26-
{{if not .Repository.IsFork}}
27-
<div class="inline field">
28-
<label>{{ctx.Locale.Tr "repo.visibility"}}</label>
29-
<div class="ui checkbox" {{if and (not .Repository.IsPrivate) (gt .Repository.NumStars 0)}}data-tooltip-content="{{ctx.Locale.Tr "repo.stars_remove_warning"}}"{{end}}>
30-
{{if .IsAdmin}}
31-
<input name="private" type="checkbox" {{if .Repository.IsPrivate}}checked{{end}}>
32-
{{else}}
33-
<input name="private" type="checkbox" {{if .Repository.IsPrivate}}checked{{end}}{{if and $.ForcePrivate .Repository.IsPrivate}} disabled{{end}}>
34-
{{if and .Repository.IsPrivate $.ForcePrivate}}<input type="hidden" name="private" value="{{.Repository.IsPrivate}}">{{end}}
35-
{{end}}
36-
<label>{{ctx.Locale.Tr "repo.visibility_helper"}} {{if .Repository.NumForks}}<span class="text red">{{ctx.Locale.Tr "repo.visibility_fork_helper"}}</span>{{end}}</label>
37-
</div>
38-
</div>
39-
{{end}}
4026
<div class="field {{if .Err_Description}}error{{end}}">
4127
<label for="description">{{ctx.Locale.Tr "repo.repo_desc"}}</label>
4228
<textarea id="description" name="description" rows="2" maxlength="2048">{{.Repository.Description}}</textarea>
@@ -786,6 +772,27 @@
786772
</h4>
787773
<div class="ui attached error danger segment">
788774
<div class="flex-list">
775+
{{if not .Repository.IsFork}}
776+
<div class="flex-item tw-items-center">
777+
<div class="flex-item-main">
778+
<div class="flex-item-title">{{ctx.Locale.Tr "repo.visibility"}}</div>
779+
{{if .Repository.IsPrivate}}
780+
<div class="flex-item-body">{{ctx.Locale.Tr "repo.settings.visibility.public.text"}}</div>
781+
{{else}}
782+
<div class="flex-item-body">{{ctx.Locale.Tr "repo.settings.visibility.private.text"}}</div>
783+
{{end}}
784+
</div>
785+
<div class="flex-item-trailing">
786+
<button class="ui basic red show-modal button" data-modal="#visibility-repo-modal">
787+
{{if .Repository.IsPrivate}}
788+
{{ctx.Locale.Tr "repo.settings.visibility.public.button"}}
789+
{{else}}
790+
{{ctx.Locale.Tr "repo.settings.visibility.private.button"}}
791+
{{end}}
792+
</button>
793+
</div>
794+
</div>
795+
{{end}}
789796
{{if .Repository.IsMirror}}
790797
<div class="flex-item">
791798
<div class="flex-item-main">
@@ -1012,6 +1019,34 @@
10121019
</div>
10131020
</div>
10141021

1022+
{{if not .Repository.IsFork}}
1023+
<div class="ui g-modal-confirm modal" id="visibility-repo-modal">
1024+
<div class="header">
1025+
{{ctx.Locale.Tr "repo.visibility"}}
1026+
</div>
1027+
<div class="content">
1028+
{{if .Repository.IsPrivate}}
1029+
<p>{{ctx.Locale.Tr "repo.settings.visibility.public.bullet_title"}}</p>
1030+
<ul>
1031+
<li>{{ctx.Locale.Tr "repo.settings.visibility.public.bullet_one"}}</li>
1032+
</ul>
1033+
{{else}}
1034+
<p>{{ctx.Locale.Tr "repo.settings.visibility.private.bullet_title"}}</p>
1035+
<ul>
1036+
<li>{{ctx.Locale.Tr "repo.settings.visibility.private.bullet_one"}}</li>
1037+
<li>{{ctx.Locale.Tr "repo.settings.visibility.private.bullet_two"}}{{if .Repository.NumForks}}<span class="text red">{{ctx.Locale.Tr "repo.visibility_fork_helper"}}</span>{{end}}</li>
1038+
</ul>
1039+
{{end}}
1040+
</div>
1041+
<form action="{{.Link}}" method="post">
1042+
{{.CsrfTokenHtml}}
1043+
<input type="hidden" name="action" value="visibility">
1044+
<input type="hidden" name="repo_id" value="{{.Repository.ID}}">
1045+
{{template "base/modal_actions_confirm" .}}
1046+
</form>
1047+
</div>
1048+
{{end}}
1049+
10151050
{{if .Repository.UnitEnabled $.Context ctx.Consts.RepoUnitTypeWiki}}
10161051
<div class="ui small modal" id="delete-wiki-modal">
10171052
<div class="header">

0 commit comments

Comments
 (0)