Skip to content

Commit 037c989

Browse files
authored
Support copy protected branch from template repository (#25889)
Fix #14303
1 parent 2b6f224 commit 037c989

File tree

8 files changed

+86
-38
lines changed

8 files changed

+86
-38
lines changed

modules/repository/generate.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,23 @@ func GenerateGitContent(ctx context.Context, templateRepo, generateRepo *repo_mo
303303

304304
// GenerateRepoOptions contains the template units to generate
305305
type GenerateRepoOptions struct {
306-
Name string
307-
DefaultBranch string
308-
Description string
309-
Private bool
310-
GitContent bool
311-
Topics bool
312-
GitHooks bool
313-
Webhooks bool
314-
Avatar bool
315-
IssueLabels bool
306+
Name string
307+
DefaultBranch string
308+
Description string
309+
Private bool
310+
GitContent bool
311+
Topics bool
312+
GitHooks bool
313+
Webhooks bool
314+
Avatar bool
315+
IssueLabels bool
316+
ProtectedBranch bool
316317
}
317318

318319
// IsValid checks whether at least one option is chosen for generation
319320
func (gro GenerateRepoOptions) IsValid() bool {
320-
return gro.GitContent || gro.Topics || gro.GitHooks || gro.Webhooks || gro.Avatar || gro.IssueLabels // or other items as they are added
321+
return gro.GitContent || gro.Topics || gro.GitHooks || gro.Webhooks || gro.Avatar ||
322+
gro.IssueLabels || gro.ProtectedBranch // or other items as they are added
321323
}
322324

323325
// GenerateRepository generates a repository from a template

modules/structs/repo.go

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ type GenerateRepoOption struct {
238238
Avatar bool `json:"avatar"`
239239
// include labels in template repo
240240
Labels bool `json:"labels"`
241+
// include protected branches in template repo
242+
ProtectedBranch bool `json:"protected_branch"`
241243
}
242244

243245
// CreateBranchRepoOption options when creating a branch in a repository

routers/api/v1/repo/repo.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,17 @@ func Generate(ctx *context.APIContext) {
355355
}
356356

357357
opts := repo_module.GenerateRepoOptions{
358-
Name: form.Name,
359-
DefaultBranch: form.DefaultBranch,
360-
Description: form.Description,
361-
Private: form.Private,
362-
GitContent: form.GitContent,
363-
Topics: form.Topics,
364-
GitHooks: form.GitHooks,
365-
Webhooks: form.Webhooks,
366-
Avatar: form.Avatar,
367-
IssueLabels: form.Labels,
358+
Name: form.Name,
359+
DefaultBranch: form.DefaultBranch,
360+
Description: form.Description,
361+
Private: form.Private,
362+
GitContent: form.GitContent,
363+
Topics: form.Topics,
364+
GitHooks: form.GitHooks,
365+
Webhooks: form.Webhooks,
366+
Avatar: form.Avatar,
367+
IssueLabels: form.Labels,
368+
ProtectedBranch: form.ProtectedBranch,
368369
}
369370

370371
if !opts.IsValid() {

routers/web/repo/repo.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,16 @@ func CreatePost(ctx *context.Context) {
241241
var err error
242242
if form.RepoTemplate > 0 {
243243
opts := repo_module.GenerateRepoOptions{
244-
Name: form.RepoName,
245-
Description: form.Description,
246-
Private: form.Private,
247-
GitContent: form.GitContent,
248-
Topics: form.Topics,
249-
GitHooks: form.GitHooks,
250-
Webhooks: form.Webhooks,
251-
Avatar: form.Avatar,
252-
IssueLabels: form.Labels,
244+
Name: form.RepoName,
245+
Description: form.Description,
246+
Private: form.Private,
247+
GitContent: form.GitContent,
248+
Topics: form.Topics,
249+
GitHooks: form.GitHooks,
250+
Webhooks: form.Webhooks,
251+
Avatar: form.Avatar,
252+
IssueLabels: form.Labels,
253+
ProtectedBranch: form.ProtectedBranch,
253254
}
254255

255256
if !opts.IsValid() {

services/forms/repo_form.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ type CreateRepoForm struct {
4242
Readme string
4343
Template bool
4444

45-
RepoTemplate int64
46-
GitContent bool
47-
Topics bool
48-
GitHooks bool
49-
Webhooks bool
50-
Avatar bool
51-
Labels bool
52-
TrustModel string
45+
RepoTemplate int64
46+
GitContent bool
47+
Topics bool
48+
GitHooks bool
49+
Webhooks bool
50+
Avatar bool
51+
Labels bool
52+
ProtectedBranch bool
53+
TrustModel string
5354
}
5455

5556
// Validate validates the fields

services/repository/template.go

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88

99
"code.gitea.io/gitea/models/db"
10+
git_model "code.gitea.io/gitea/models/git"
1011
issues_model "code.gitea.io/gitea/models/issues"
1112
repo_model "code.gitea.io/gitea/models/repo"
1213
user_model "code.gitea.io/gitea/models/user"
@@ -39,6 +40,28 @@ func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_m
3940
return db.Insert(ctx, newLabels)
4041
}
4142

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+
4265
// GenerateRepository generates a repository from a template
4366
func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templateRepo *repo_model.Repository, opts repo_module.GenerateRepoOptions) (_ *repo_model.Repository, err error) {
4467
if !doer.IsAdmin && !owner.CanCreateRepo() {
@@ -96,6 +119,12 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
96119
}
97120
}
98121

122+
if opts.ProtectedBranch {
123+
if err = GenerateProtectedBranch(ctx, templateRepo, generateRepo); err != nil {
124+
return err
125+
}
126+
}
127+
99128
return nil
100129
}); err != nil {
101130
return nil, err

templates/repo/create.tmpl

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@
107107
<label>{{.locale.Tr "repo.template.issue_labels"}}</label>
108108
</div>
109109
</div>
110+
<div class="inline field">
111+
<label></label>
112+
<div class="ui checkbox">
113+
<input name="protected_branch" type="checkbox" tabindex="0" {{if .protected_branch}}checked{{end}}>
114+
<label>{{.locale.Tr "repo.settings.protected_branch"}}</label>
115+
</div>
116+
</div>
110117
</div>
111118

112119
<div id="non_template">

templates/swagger/v1_json.tmpl

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)