Skip to content

Commit 89945d5

Browse files
committed
Create functions in the service layer to change the visibility
1 parent fd47dd6 commit 89945d5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

routers/web/repo/setting/setting.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,15 @@ func SettingsPost(ctx *context.Context) {
947947
return
948948
}
949949

950-
repo.IsPrivate = !repo.IsPrivate
951-
if err := repo_service.UpdateRepository(ctx, repo, true); err != nil {
950+
var err error
951+
952+
if repo.IsPrivate {
953+
err = repo_service.MakeRepoPublic(ctx, repo)
954+
} else {
955+
err = repo_service.MakeRepoPrivate(ctx, repo)
956+
}
957+
958+
if err != nil {
952959
log.Error("Tried to change the visibility of the repo: %s", err)
953960
ctx.Flash.Error(ctx.Tr("repo.settings.visibility.error"))
954961
ctx.Redirect(ctx.Repo.RepoLink + "/settings")

services/repository/repository.go

Lines changed: 25 additions & 0 deletions
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 {

0 commit comments

Comments
 (0)