From f3165824585b55c0bd75cb4f46120fca8b4ace9f Mon Sep 17 00:00:00 2001 From: Abheek Dhawan <67982792+ADawesomeguy@users.noreply.github.com> Date: Sat, 26 Mar 2022 21:56:28 -0500 Subject: [PATCH] Set the default branch for repositories generated from templates (#19136) * Set the default branch for repositories generated from templates * Allows default branch to be set through the API for repos generated from templates * Update swagger API template * Only set default branch to the one from the template if not specified * Use specified default branch if it exists while generating git commits Fix #19082 Co-authored-by: John Olheiser Co-authored-by: zeripath --- models/repo_generate.go | 19 ++++++++++--------- modules/repository/generate.go | 15 +++++++++++++-- modules/structs/repo.go | 2 ++ routers/api/v1/repo/repo.go | 19 ++++++++++--------- templates/swagger/v1_json.tmpl | 5 +++++ 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/models/repo_generate.go b/models/repo_generate.go index fc749f1120..03b0908397 100644 --- a/models/repo_generate.go +++ b/models/repo_generate.go @@ -20,15 +20,16 @@ import ( // GenerateRepoOptions contains the template units to generate type GenerateRepoOptions struct { - Name string - Description string - Private bool - GitContent bool - Topics bool - GitHooks bool - Webhooks bool - Avatar bool - IssueLabels bool + Name string + DefaultBranch string + Description string + Private bool + GitContent bool + Topics bool + GitHooks bool + Webhooks bool + Avatar bool + IssueLabels bool } // IsValid checks whether at least one option is chosen for generation diff --git a/modules/repository/generate.go b/modules/repository/generate.go index d0b5fa0820..cf219f4c26 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -184,7 +184,13 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r return fmt.Errorf("git remote add: %v", err) } - return initRepoCommit(ctx, tmpDir, repo, repo.Owner, templateRepo.DefaultBranch) + // set default branch based on whether it's specified in the newly generated repo or not + defaultBranch := repo.DefaultBranch + if strings.TrimSpace(defaultBranch) == "" { + defaultBranch = templateRepo.DefaultBranch + } + + return initRepoCommit(ctx, tmpDir, repo, repo.Owner, defaultBranch) } func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) { @@ -208,7 +214,11 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r return fmt.Errorf("getRepositoryByID: %v", err) } - repo.DefaultBranch = templateRepo.DefaultBranch + // if there was no default branch supplied when generating the repo, use the default one from the template + if strings.TrimSpace(repo.DefaultBranch) == "" { + repo.DefaultBranch = templateRepo.DefaultBranch + } + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("openRepository: %v", err) @@ -249,6 +259,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ Name: opts.Name, LowerName: strings.ToLower(opts.Name), Description: opts.Description, + DefaultBranch: opts.DefaultBranch, IsPrivate: opts.Private, IsEmpty: !opts.GitContent || templateRepo.IsEmpty, IsFsckEnabled: templateRepo.IsFsckEnabled, diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 27d0b4f3ba..c218796620 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -201,6 +201,8 @@ type GenerateRepoOption struct { // required: true // unique: true Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"` + // Default branch of the new repository + DefaultBranch string `json:"default_branch"` // Description of the repository to create Description string `json:"description" binding:"MaxSize(255)"` // Whether the repository is private diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index d266b3f4f3..7e386d2e5b 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -359,15 +359,16 @@ func Generate(ctx *context.APIContext) { } opts := models.GenerateRepoOptions{ - Name: form.Name, - Description: form.Description, - Private: form.Private, - GitContent: form.GitContent, - Topics: form.Topics, - GitHooks: form.GitHooks, - Webhooks: form.Webhooks, - Avatar: form.Avatar, - IssueLabels: form.Labels, + Name: form.Name, + DefaultBranch: form.DefaultBranch, + Description: form.Description, + Private: form.Private, + GitContent: form.GitContent, + Topics: form.Topics, + GitHooks: form.GitHooks, + Webhooks: form.Webhooks, + Avatar: form.Avatar, + IssueLabels: form.Labels, } if !opts.IsValid() { diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 1aec9ec06f..284e1c71a0 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -15412,6 +15412,11 @@ "type": "boolean", "x-go-name": "Avatar" }, + "default_branch": { + "description": "Default branch of the new repository", + "type": "string", + "x-go-name": "DefaultBranch" + }, "description": { "description": "Description of the repository to create", "type": "string",