loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Support copy protected branch from template repository (#25889)

Fix #14303

authored by

Lunny Xiao and committed by
GitHub
037c9895 2b6f2243

+86 -38
+13 -11
modules/repository/generate.go
··· 303 303 304 304 // GenerateRepoOptions contains the template units to generate 305 305 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 316 317 } 317 318 318 319 // IsValid checks whether at least one option is chosen for generation 319 320 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 321 323 } 322 324 323 325 // GenerateRepository generates a repository from a template
+2
modules/structs/repo.go
··· 238 238 Avatar bool `json:"avatar"` 239 239 // include labels in template repo 240 240 Labels bool `json:"labels"` 241 + // include protected branches in template repo 242 + ProtectedBranch bool `json:"protected_branch"` 241 243 } 242 244 243 245 // CreateBranchRepoOption options when creating a branch in a repository
+11 -10
routers/api/v1/repo/repo.go
··· 355 355 } 356 356 357 357 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, 368 369 } 369 370 370 371 if !opts.IsValid() {
+10 -9
routers/web/repo/repo.go
··· 241 241 var err error 242 242 if form.RepoTemplate > 0 { 243 243 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, 253 254 } 254 255 255 256 if !opts.IsValid() {
+9 -8
services/forms/repo_form.go
··· 42 42 Readme string 43 43 Template bool 44 44 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 53 54 } 54 55 55 56 // Validate validates the fields
+29
services/repository/template.go
··· 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 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() { ··· 92 115 // Issue Labels 93 116 if opts.IssueLabels { 94 117 if err = GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil { 118 + return err 119 + } 120 + } 121 + 122 + if opts.ProtectedBranch { 123 + if err = GenerateProtectedBranch(ctx, templateRepo, generateRepo); err != nil { 95 124 return err 96 125 } 97 126 }
+7
templates/repo/create.tmpl
··· 107 107 <label>{{.locale.Tr "repo.template.issue_labels"}}</label> 108 108 </div> 109 109 </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> 110 117 </div> 111 118 112 119 <div id="non_template">
+5
templates/swagger/v1_json.tmpl
··· 18906 18906 "type": "boolean", 18907 18907 "x-go-name": "Private" 18908 18908 }, 18909 + "protected_branch": { 18910 + "description": "include protected branches in template repo", 18911 + "type": "boolean", 18912 + "x-go-name": "ProtectedBranch" 18913 + }, 18909 18914 "topics": { 18910 18915 "description": "include topics in template repo", 18911 18916 "type": "boolean",