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.

77 lines
1.9 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. )
  10. // GenerateRepository generates a repository from a template
  11. func GenerateRepository(doer, owner *models.User, templateRepo *models.Repository, opts models.GenerateRepoOptions) (_ *models.Repository, err error) {
  12. var generateRepo *models.Repository
  13. if err = models.WithTx(func(ctx models.DBContext) error {
  14. generateRepo, err = models.GenerateRepository(ctx, doer, owner, templateRepo, opts)
  15. if err != nil {
  16. return err
  17. }
  18. // Git Content
  19. if opts.GitContent && !templateRepo.IsEmpty {
  20. if err = models.GenerateGitContent(ctx, templateRepo, generateRepo); err != nil {
  21. return err
  22. }
  23. }
  24. // Topics
  25. if opts.Topics {
  26. if err = models.GenerateTopics(ctx, templateRepo, generateRepo); err != nil {
  27. return err
  28. }
  29. }
  30. // Git Hooks
  31. if opts.GitHooks {
  32. if err = models.GenerateGitHooks(ctx, templateRepo, generateRepo); err != nil {
  33. return err
  34. }
  35. }
  36. // Webhooks
  37. if opts.Webhooks {
  38. if err = models.GenerateWebhooks(ctx, templateRepo, generateRepo); err != nil {
  39. return err
  40. }
  41. }
  42. // Avatar
  43. if opts.Avatar && len(templateRepo.Avatar) > 0 {
  44. if err = models.GenerateAvatar(ctx, templateRepo, generateRepo); err != nil {
  45. return err
  46. }
  47. }
  48. // Issue Labels
  49. if opts.IssueLabels {
  50. if err = models.GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil {
  51. return err
  52. }
  53. }
  54. return nil
  55. }); err != nil {
  56. if generateRepo != nil {
  57. if errDelete := models.DeleteRepository(doer, owner.ID, generateRepo.ID); errDelete != nil {
  58. log.Error("Rollback deleteRepository: %v", errDelete)
  59. }
  60. }
  61. return nil, err
  62. }
  63. notification.NotifyCreateRepository(doer, owner, generateRepo)
  64. return generateRepo, nil
  65. }