|
7 | 7 | "context"
|
8 | 8 |
|
9 | 9 | "code.gitea.io/gitea/models/db"
|
| 10 | + git_model "code.gitea.io/gitea/models/git" |
10 | 11 | issues_model "code.gitea.io/gitea/models/issues"
|
11 | 12 | repo_model "code.gitea.io/gitea/models/repo"
|
12 | 13 | user_model "code.gitea.io/gitea/models/user"
|
@@ -39,6 +40,28 @@ func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_m
|
39 | 40 | return db.Insert(ctx, newLabels)
|
40 | 41 | }
|
41 | 42 |
|
| 43 | +func GenerateProtectedBranch(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error { |
| 44 | + templateBranches, err := git_model.FindRepoProtectedBranchRules(ctx, templateRepo.ID) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + // Prevent insert being called with an empty slice which would result in |
| 49 | + // err "no element on slice when insert". |
| 50 | + if len(templateBranches) == 0 { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + newBranches := make([]*git_model.ProtectedBranch, 0, len(templateBranches)) |
| 55 | + for _, templateBranch := range templateBranches { |
| 56 | + templateBranch.ID = 0 |
| 57 | + templateBranch.RepoID = generateRepo.ID |
| 58 | + templateBranch.UpdatedUnix = 0 |
| 59 | + templateBranch.CreatedUnix = 0 |
| 60 | + newBranches = append(newBranches, templateBranch) |
| 61 | + } |
| 62 | + return db.Insert(ctx, newBranches) |
| 63 | +} |
| 64 | + |
42 | 65 | // GenerateRepository generates a repository from a template
|
43 | 66 | func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templateRepo *repo_model.Repository, opts repo_module.GenerateRepoOptions) (_ *repo_model.Repository, err error) {
|
44 | 67 | if !doer.IsAdmin && !owner.CanCreateRepo() {
|
@@ -96,6 +119,12 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
|
96 | 119 | }
|
97 | 120 | }
|
98 | 121 |
|
| 122 | + if opts.ProtectedBranch { |
| 123 | + if err = GenerateProtectedBranch(ctx, templateRepo, generateRepo); err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + } |
| 127 | + |
99 | 128 | return nil
|
100 | 129 | }); err != nil {
|
101 | 130 | return nil, err
|
|
0 commit comments