repos.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2020 The Gogs 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 db
  5. import (
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/jinzhu/gorm"
  10. "gogs.io/gogs/internal/errutil"
  11. )
  12. // ReposStore is the persistent interface for repositories.
  13. //
  14. // NOTE: All methods are sorted in alphabetical order.
  15. type ReposStore interface {
  16. // GetByName returns the repository with given owner and name.
  17. // It returns ErrRepoNotExist when not found.
  18. GetByName(ownerID int64, name string) (*Repository, error)
  19. }
  20. var Repos ReposStore
  21. // NOTE: This is a GORM create hook.
  22. func (r *Repository) BeforeCreate() {
  23. r.CreatedUnix = gorm.NowFunc().Unix()
  24. }
  25. // NOTE: This is a GORM update hook.
  26. func (r *Repository) BeforeUpdate() {
  27. r.UpdatedUnix = gorm.NowFunc().Unix()
  28. }
  29. // NOTE: This is a GORM query hook.
  30. func (r *Repository) AfterFind() {
  31. r.Created = time.Unix(r.CreatedUnix, 0).Local()
  32. r.Updated = time.Unix(r.UpdatedUnix, 0).Local()
  33. }
  34. var _ ReposStore = (*repos)(nil)
  35. type repos struct {
  36. *gorm.DB
  37. }
  38. type ErrRepoAlreadyExist struct {
  39. args errutil.Args
  40. }
  41. func IsErrRepoAlreadyExist(err error) bool {
  42. _, ok := err.(ErrRepoAlreadyExist)
  43. return ok
  44. }
  45. func (err ErrRepoAlreadyExist) Error() string {
  46. return fmt.Sprintf("repository already exists: %v", err.args)
  47. }
  48. type createRepoOpts struct {
  49. Name string
  50. Description string
  51. DefaultBranch string
  52. Private bool
  53. Mirror bool
  54. EnableWiki bool
  55. EnableIssues bool
  56. EnablePulls bool
  57. Fork bool
  58. ForkID int64
  59. }
  60. // create creates a new repository record in the database. Fields of "repo" will be updated
  61. // in place upon insertion. It returns ErrNameNotAllowed when the repository name is not allowed,
  62. // or ErrRepoAlreadyExist when a repository with same name already exists for the owner.
  63. func (db *repos) create(ownerID int64, opts createRepoOpts) (*Repository, error) {
  64. err := isRepoNameAllowed(opts.Name)
  65. if err != nil {
  66. return nil, err
  67. }
  68. _, err = db.GetByName(ownerID, opts.Name)
  69. if err == nil {
  70. return nil, ErrRepoAlreadyExist{args: errutil.Args{"ownerID": ownerID, "name": opts.Name}}
  71. } else if !IsErrRepoNotExist(err) {
  72. return nil, err
  73. }
  74. repo := &Repository{
  75. OwnerID: ownerID,
  76. LowerName: strings.ToLower(opts.Name),
  77. Name: opts.Name,
  78. Description: opts.Description,
  79. DefaultBranch: opts.DefaultBranch,
  80. IsPrivate: opts.Private,
  81. IsMirror: opts.Mirror,
  82. EnableWiki: opts.EnableWiki,
  83. EnableIssues: opts.EnableIssues,
  84. EnablePulls: opts.EnablePulls,
  85. IsFork: opts.Fork,
  86. ForkID: opts.ForkID,
  87. }
  88. return repo, db.DB.Create(repo).Error
  89. }
  90. var _ errutil.NotFound = (*ErrRepoNotExist)(nil)
  91. type ErrRepoNotExist struct {
  92. args map[string]interface{}
  93. }
  94. func IsErrRepoNotExist(err error) bool {
  95. _, ok := err.(ErrRepoNotExist)
  96. return ok
  97. }
  98. func (err ErrRepoNotExist) Error() string {
  99. return fmt.Sprintf("repository does not exist: %v", err.args)
  100. }
  101. func (ErrRepoNotExist) NotFound() bool {
  102. return true
  103. }
  104. func (db *repos) GetByName(ownerID int64, name string) (*Repository, error) {
  105. repo := new(Repository)
  106. err := db.Where("owner_id = ? AND lower_name = ?", ownerID, strings.ToLower(name)).First(repo).Error
  107. if err != nil {
  108. if gorm.IsRecordNotFoundError(err) {
  109. return nil, ErrRepoNotExist{args: map[string]interface{}{"ownerID": ownerID, "name": name}}
  110. }
  111. return nil, err
  112. }
  113. return repo, nil
  114. }