You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repository
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/notification"
  9. repo_module "code.gitea.io/gitea/modules/repository"
  10. )
  11. // GenerateRepository generates a repository from a template
  12. func GenerateRepository(doer, owner *models.User, templateRepo *models.Repository, opts models.GenerateRepoOptions) (_ *models.Repository, err error) {
  13. var generateRepo *models.Repository
  14. if err = models.WithTx(func(ctx models.DBContext) error {
  15. generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts)
  16. if err != nil {
  17. return err
  18. }
  19. // Git Content
  20. if opts.GitContent && !templateRepo.IsEmpty {
  21. if err = repo_module.GenerateGitContent(ctx, templateRepo, generateRepo); err != nil {
  22. return err
  23. }
  24. }
  25. // Topics
  26. if opts.Topics {
  27. if err = models.GenerateTopics(ctx, templateRepo, generateRepo); err != nil {
  28. return err
  29. }
  30. }
  31. // Git Hooks
  32. if opts.GitHooks {
  33. if err = models.GenerateGitHooks(ctx, templateRepo, generateRepo); err != nil {
  34. return err
  35. }
  36. }
  37. // Webhooks
  38. if opts.Webhooks {
  39. if err = models.GenerateWebhooks(ctx, templateRepo, generateRepo); err != nil {
  40. return err
  41. }
  42. }
  43. // Avatar
  44. if opts.Avatar && len(templateRepo.Avatar) > 0 {
  45. if err = models.GenerateAvatar(ctx, templateRepo, generateRepo); err != nil {
  46. return err
  47. }
  48. }
  49. // Issue Labels
  50. if opts.IssueLabels {
  51. if err = models.GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil {
  52. return err
  53. }
  54. }
  55. return nil
  56. }); err != nil {
  57. if generateRepo != nil {
  58. if errDelete := models.DeleteRepository(doer, owner.ID, generateRepo.ID); errDelete != nil {
  59. log.Error("Rollback deleteRepository: %v", errDelete)
  60. }
  61. }
  62. return nil, err
  63. }
  64. notification.NotifyCreateRepository(doer, owner, generateRepo)
  65. return generateRepo, nil
  66. }