diff --git a/services/repository/generate.go b/services/repository/generate.go index 83e9c22e54..fb747067f9 100644 --- a/services/repository/generate.go +++ b/services/repository/generate.go @@ -207,6 +207,9 @@ func processGiteaTemplateFile(ctx context.Context, tmpDir string, templateRepo, } func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error { + // set default branch based on whether it's specified in the newly generated repo or not + repo.DefaultBranch = util.IfZero(repo.DefaultBranch, templateRepo.DefaultBranch) + // Clone to temporary path and do the init commit. if err := gitrepo.CloneRepoToLocal(ctx, templateRepo, tmpDir, git.CloneRepoOptions{ Depth: 1, @@ -246,13 +249,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r return fmt.Errorf("failed to add submodules: %v", err) } - // 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) + return initRepoCommit(ctx, tmpDir, repo, repo.Owner, repo.DefaultBranch) } // GenerateGitContent generates git content from a template repository @@ -266,17 +263,6 @@ func GenerateGitContent(ctx context.Context, templateRepo, generateRepo *repo_mo if err = generateRepoCommit(ctx, generateRepo, templateRepo, generateRepo, tmpDir); err != nil { return fmt.Errorf("generateRepoCommit: %w", err) } - - // re-fetch repo - if generateRepo, err = repo_model.GetRepositoryByID(ctx, generateRepo.ID); err != nil { - return fmt.Errorf("getRepositoryByID: %w", err) - } - - // if there was no default branch supplied when generating the repo, use the default one from the template - if strings.TrimSpace(generateRepo.DefaultBranch) == "" { - generateRepo.DefaultBranch = templateRepo.DefaultBranch - } - if err = gitrepo.SetDefaultBranch(ctx, generateRepo, generateRepo.DefaultBranch); err != nil { return fmt.Errorf("setDefaultBranch: %w", err) } @@ -291,6 +277,9 @@ func GenerateGitContent(ctx context.Context, templateRepo, generateRepo *repo_mo if err := git_model.CopyLFS(ctx, generateRepo, templateRepo); err != nil { return fmt.Errorf("failed to copy LFS: %w", err) } + if _, err := repo_module.SyncRepoBranches(ctx, generateRepo.ID, 0); err != nil { + return fmt.Errorf("SyncRepoBranches: %w", err) + } return nil } diff --git a/tests/integration/api_repo_test.go b/tests/integration/api_repo_test.go index deacd68a49..263243f0fa 100644 --- a/tests/integration/api_repo_test.go +++ b/tests/integration/api_repo_test.go @@ -671,6 +671,16 @@ func TestAPIGenerateRepo(t *testing.T) { templateRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 44}) + assertGeneratedRepoIsUsable := func(t *testing.T, ownerName string, repo *api.Repository) { + t.Helper() + assert.NotEmpty(t, repo.DefaultBranch) + + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/branches/%s", ownerName, repo.Name, repo.DefaultBranch).AddTokenAuth(token) + resp := MakeRequest(t, req, http.StatusOK) + branch := DecodeJSON(t, resp, &api.Branch{}) + assert.Equal(t, repo.DefaultBranch, branch.Name) + } + // user repo := new(api.Repository) req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/generate", templateRepo.OwnerName, templateRepo.Name), &api.GenerateRepoOption{ @@ -684,6 +694,7 @@ func TestAPIGenerateRepo(t *testing.T) { DecodeJSON(t, resp, repo) assert.Equal(t, "new-repo", repo.Name) + assertGeneratedRepoIsUsable(t, user.Name, repo) // org req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/generate", templateRepo.OwnerName, templateRepo.Name), &api.GenerateRepoOption{ @@ -697,6 +708,7 @@ func TestAPIGenerateRepo(t *testing.T) { DecodeJSON(t, resp, repo) assert.Equal(t, "new-repo", repo.Name) + assertGeneratedRepoIsUsable(t, "org3", repo) } func TestAPIRepoGetReviewers(t *testing.T) {