diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-07-21 12:32:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-21 12:32:47 +0800 |
commit | 037c9895a7406b42f88991295382db18f98dbef9 (patch) | |
tree | d3d893d77c082f629219d386c9cf6a45dc958f84 /services | |
parent | 2b6f2243366b6640a4a322b33b419445c710cba9 (diff) | |
download | gitea-037c9895a7406b42f88991295382db18f98dbef9.tar.gz gitea-037c9895a7406b42f88991295382db18f98dbef9.zip |
Support copy protected branch from template repository (#25889)
Fix #14303
Diffstat (limited to 'services')
-rw-r--r-- | services/forms/repo_form.go | 17 | ||||
-rw-r--r-- | services/repository/template.go | 29 |
2 files changed, 38 insertions, 8 deletions
diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 8108a55f7a..a26a2d89c5 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -42,14 +42,15 @@ type CreateRepoForm struct { Readme string Template bool - RepoTemplate int64 - GitContent bool - Topics bool - GitHooks bool - Webhooks bool - Avatar bool - Labels bool - TrustModel string + RepoTemplate int64 + GitContent bool + Topics bool + GitHooks bool + Webhooks bool + Avatar bool + Labels bool + ProtectedBranch bool + TrustModel string } // Validate validates the fields diff --git a/services/repository/template.go b/services/repository/template.go index 42174d095b..9a69360aff 100644 --- a/services/repository/template.go +++ b/services/repository/template.go @@ -7,6 +7,7 @@ import ( "context" "code.gitea.io/gitea/models/db" + git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -39,6 +40,28 @@ func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_m return db.Insert(ctx, newLabels) } +func GenerateProtectedBranch(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error { + templateBranches, err := git_model.FindRepoProtectedBranchRules(ctx, templateRepo.ID) + if err != nil { + return err + } + // Prevent insert being called with an empty slice which would result in + // err "no element on slice when insert". + if len(templateBranches) == 0 { + return nil + } + + newBranches := make([]*git_model.ProtectedBranch, 0, len(templateBranches)) + for _, templateBranch := range templateBranches { + templateBranch.ID = 0 + templateBranch.RepoID = generateRepo.ID + templateBranch.UpdatedUnix = 0 + templateBranch.CreatedUnix = 0 + newBranches = append(newBranches, templateBranch) + } + return db.Insert(ctx, newBranches) +} + // GenerateRepository generates a repository from a template func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templateRepo *repo_model.Repository, opts repo_module.GenerateRepoOptions) (_ *repo_model.Repository, err error) { if !doer.IsAdmin && !owner.CanCreateRepo() { @@ -96,6 +119,12 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ } } + if opts.ProtectedBranch { + if err = GenerateProtectedBranch(ctx, templateRepo, generateRepo); err != nil { + return err + } + } + return nil }); err != nil { return nil, err |