repos.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. "context"
  7. "fmt"
  8. "strings"
  9. "time"
  10. api "github.com/gogs/go-gogs-client"
  11. "github.com/pkg/errors"
  12. "gorm.io/gorm"
  13. "gogs.io/gogs/internal/errutil"
  14. "gogs.io/gogs/internal/repoutil"
  15. )
  16. // ReposStore is the persistent interface for repositories.
  17. type ReposStore interface {
  18. // Create creates a new repository record in the database. It returns
  19. // ErrNameNotAllowed when the repository name is not allowed, or
  20. // ErrRepoAlreadyExist when a repository with same name already exists for the
  21. // owner.
  22. Create(ctx context.Context, ownerID int64, opts CreateRepoOptions) (*Repository, error)
  23. // GetByCollaboratorID returns a list of repositories that the given
  24. // collaborator has access to. Results are limited to the given limit and sorted
  25. // by the given order (e.g. "updated_unix DESC"). Repositories that are owned
  26. // directly by the given collaborator are not included.
  27. GetByCollaboratorID(ctx context.Context, collaboratorID int64, limit int, orderBy string) ([]*Repository, error)
  28. // GetByCollaboratorIDWithAccessMode returns a list of repositories and
  29. // corresponding access mode that the given collaborator has access to.
  30. // Repositories that are owned directly by the given collaborator are not
  31. // included.
  32. GetByCollaboratorIDWithAccessMode(ctx context.Context, collaboratorID int64) (map[*Repository]AccessMode, error)
  33. // GetByID returns the repository with given ID. It returns ErrRepoNotExist when
  34. // not found.
  35. GetByID(ctx context.Context, id int64) (*Repository, error)
  36. // GetByName returns the repository with given owner and name. It returns
  37. // ErrRepoNotExist when not found.
  38. GetByName(ctx context.Context, ownerID int64, name string) (*Repository, error)
  39. // Star marks the user to star the repository.
  40. Star(ctx context.Context, userID, repoID int64) error
  41. // Touch updates the updated time to the current time and removes the bare state
  42. // of the given repository.
  43. Touch(ctx context.Context, id int64) error
  44. // ListWatches returns all watches of the given repository.
  45. ListWatches(ctx context.Context, repoID int64) ([]*Watch, error)
  46. // Watch marks the user to watch the repository.
  47. Watch(ctx context.Context, userID, repoID int64) error
  48. // HasForkedBy returns true if the given repository has forked by the given user.
  49. HasForkedBy(ctx context.Context, repoID, userID int64) bool
  50. }
  51. var Repos ReposStore
  52. // BeforeCreate implements the GORM create hook.
  53. func (r *Repository) BeforeCreate(tx *gorm.DB) error {
  54. if r.CreatedUnix == 0 {
  55. r.CreatedUnix = tx.NowFunc().Unix()
  56. }
  57. return nil
  58. }
  59. // BeforeUpdate implements the GORM update hook.
  60. func (r *Repository) BeforeUpdate(tx *gorm.DB) error {
  61. r.UpdatedUnix = tx.NowFunc().Unix()
  62. return nil
  63. }
  64. // AfterFind implements the GORM query hook.
  65. func (r *Repository) AfterFind(_ *gorm.DB) error {
  66. r.Created = time.Unix(r.CreatedUnix, 0).Local()
  67. r.Updated = time.Unix(r.UpdatedUnix, 0).Local()
  68. return nil
  69. }
  70. type RepositoryAPIFormatOptions struct {
  71. Permission *api.Permission
  72. Parent *api.Repository
  73. }
  74. // APIFormat returns the API format of a repository.
  75. func (r *Repository) APIFormat(owner *User, opts ...RepositoryAPIFormatOptions) *api.Repository {
  76. var opt RepositoryAPIFormatOptions
  77. if len(opts) > 0 {
  78. opt = opts[0]
  79. }
  80. cloneLink := repoutil.NewCloneLink(owner.Name, r.Name, false)
  81. return &api.Repository{
  82. ID: r.ID,
  83. Owner: owner.APIFormat(),
  84. Name: r.Name,
  85. FullName: owner.Name + "/" + r.Name,
  86. Description: r.Description,
  87. Private: r.IsPrivate,
  88. Fork: r.IsFork,
  89. Parent: opt.Parent,
  90. Empty: r.IsBare,
  91. Mirror: r.IsMirror,
  92. Size: r.Size,
  93. HTMLURL: repoutil.HTMLURL(owner.Name, r.Name),
  94. SSHURL: cloneLink.SSH,
  95. CloneURL: cloneLink.HTTPS,
  96. Website: r.Website,
  97. Stars: r.NumStars,
  98. Forks: r.NumForks,
  99. Watchers: r.NumWatches,
  100. OpenIssues: r.NumOpenIssues,
  101. DefaultBranch: r.DefaultBranch,
  102. Created: r.Created,
  103. Updated: r.Updated,
  104. Permissions: opt.Permission,
  105. }
  106. }
  107. var _ ReposStore = (*repos)(nil)
  108. type repos struct {
  109. *gorm.DB
  110. }
  111. // NewReposStore returns a persistent interface for repositories with given
  112. // database connection.
  113. func NewReposStore(db *gorm.DB) ReposStore {
  114. return &repos{DB: db}
  115. }
  116. type ErrRepoAlreadyExist struct {
  117. args errutil.Args
  118. }
  119. func IsErrRepoAlreadyExist(err error) bool {
  120. _, ok := err.(ErrRepoAlreadyExist)
  121. return ok
  122. }
  123. func (err ErrRepoAlreadyExist) Error() string {
  124. return fmt.Sprintf("repository already exists: %v", err.args)
  125. }
  126. type CreateRepoOptions struct {
  127. Name string
  128. Description string
  129. DefaultBranch string
  130. Private bool
  131. Mirror bool
  132. EnableWiki bool
  133. EnableIssues bool
  134. EnablePulls bool
  135. Fork bool
  136. ForkID int64
  137. }
  138. func (db *repos) Create(ctx context.Context, ownerID int64, opts CreateRepoOptions) (*Repository, error) {
  139. err := isRepoNameAllowed(opts.Name)
  140. if err != nil {
  141. return nil, err
  142. }
  143. _, err = db.GetByName(ctx, ownerID, opts.Name)
  144. if err == nil {
  145. return nil, ErrRepoAlreadyExist{
  146. args: errutil.Args{
  147. "ownerID": ownerID,
  148. "name": opts.Name,
  149. },
  150. }
  151. } else if !IsErrRepoNotExist(err) {
  152. return nil, err
  153. }
  154. repo := &Repository{
  155. OwnerID: ownerID,
  156. LowerName: strings.ToLower(opts.Name),
  157. Name: opts.Name,
  158. Description: opts.Description,
  159. DefaultBranch: opts.DefaultBranch,
  160. IsPrivate: opts.Private,
  161. IsMirror: opts.Mirror,
  162. EnableWiki: opts.EnableWiki,
  163. EnableIssues: opts.EnableIssues,
  164. EnablePulls: opts.EnablePulls,
  165. IsFork: opts.Fork,
  166. ForkID: opts.ForkID,
  167. }
  168. return repo, db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  169. err = tx.Create(repo).Error
  170. if err != nil {
  171. return errors.Wrap(err, "create")
  172. }
  173. err = NewReposStore(tx).Watch(ctx, ownerID, repo.ID)
  174. if err != nil {
  175. return errors.Wrap(err, "watch")
  176. }
  177. return nil
  178. })
  179. }
  180. func (db *repos) GetByCollaboratorID(ctx context.Context, collaboratorID int64, limit int, orderBy string) ([]*Repository, error) {
  181. /*
  182. Equivalent SQL for PostgreSQL:
  183. SELECT * FROM repository
  184. JOIN access ON access.repo_id = repository.id AND access.user_id = @collaboratorID
  185. WHERE access.mode >= @accessModeRead
  186. ORDER BY @orderBy
  187. LIMIT @limit
  188. */
  189. var repos []*Repository
  190. return repos, db.WithContext(ctx).
  191. Joins("JOIN access ON access.repo_id = repository.id AND access.user_id = ?", collaboratorID).
  192. Where("access.mode >= ?", AccessModeRead).
  193. Order(orderBy).
  194. Limit(limit).
  195. Find(&repos).
  196. Error
  197. }
  198. func (db *repos) GetByCollaboratorIDWithAccessMode(ctx context.Context, collaboratorID int64) (map[*Repository]AccessMode, error) {
  199. /*
  200. Equivalent SQL for PostgreSQL:
  201. SELECT
  202. repository.*,
  203. access.mode
  204. FROM repository
  205. JOIN access ON access.repo_id = repository.id AND access.user_id = @collaboratorID
  206. WHERE access.mode >= @accessModeRead
  207. */
  208. var reposWithAccessMode []*struct {
  209. *Repository
  210. Mode AccessMode
  211. }
  212. err := db.WithContext(ctx).
  213. Select("repository.*", "access.mode").
  214. Table("repository").
  215. Joins("JOIN access ON access.repo_id = repository.id AND access.user_id = ?", collaboratorID).
  216. Where("access.mode >= ?", AccessModeRead).
  217. Find(&reposWithAccessMode).
  218. Error
  219. if err != nil {
  220. return nil, err
  221. }
  222. repos := make(map[*Repository]AccessMode, len(reposWithAccessMode))
  223. for _, repoWithAccessMode := range reposWithAccessMode {
  224. repos[repoWithAccessMode.Repository] = repoWithAccessMode.Mode
  225. }
  226. return repos, nil
  227. }
  228. var _ errutil.NotFound = (*ErrRepoNotExist)(nil)
  229. type ErrRepoNotExist struct {
  230. args errutil.Args
  231. }
  232. func IsErrRepoNotExist(err error) bool {
  233. return errors.As(err, &ErrRepoNotExist{})
  234. }
  235. func (err ErrRepoNotExist) Error() string {
  236. return fmt.Sprintf("repository does not exist: %v", err.args)
  237. }
  238. func (ErrRepoNotExist) NotFound() bool {
  239. return true
  240. }
  241. func (db *repos) GetByID(ctx context.Context, id int64) (*Repository, error) {
  242. repo := new(Repository)
  243. err := db.WithContext(ctx).Where("id = ?", id).First(repo).Error
  244. if err != nil {
  245. if errors.Is(err, gorm.ErrRecordNotFound) {
  246. return nil, ErrRepoNotExist{errutil.Args{"repoID": id}}
  247. }
  248. return nil, err
  249. }
  250. return repo, nil
  251. }
  252. func (db *repos) GetByName(ctx context.Context, ownerID int64, name string) (*Repository, error) {
  253. repo := new(Repository)
  254. err := db.WithContext(ctx).
  255. Where("owner_id = ? AND lower_name = ?", ownerID, strings.ToLower(name)).
  256. First(repo).
  257. Error
  258. if err != nil {
  259. if errors.Is(err, gorm.ErrRecordNotFound) {
  260. return nil, ErrRepoNotExist{
  261. args: errutil.Args{
  262. "ownerID": ownerID,
  263. "name": name,
  264. },
  265. }
  266. }
  267. return nil, err
  268. }
  269. return repo, nil
  270. }
  271. func (db *repos) recountStars(tx *gorm.DB, userID, repoID int64) error {
  272. /*
  273. Equivalent SQL for PostgreSQL:
  274. UPDATE repository
  275. SET num_stars = (
  276. SELECT COUNT(*) FROM star WHERE repo_id = @repoID
  277. )
  278. WHERE id = @repoID
  279. */
  280. err := tx.Model(&Repository{}).
  281. Where("id = ?", repoID).
  282. Update(
  283. "num_stars",
  284. tx.Model(&Star{}).Select("COUNT(*)").Where("repo_id = ?", repoID),
  285. ).
  286. Error
  287. if err != nil {
  288. return errors.Wrap(err, `update "repository.num_stars"`)
  289. }
  290. /*
  291. Equivalent SQL for PostgreSQL:
  292. UPDATE "user"
  293. SET num_stars = (
  294. SELECT COUNT(*) FROM star WHERE uid = @userID
  295. )
  296. WHERE id = @userID
  297. */
  298. err = tx.Model(&User{}).
  299. Where("id = ?", userID).
  300. Update(
  301. "num_stars",
  302. tx.Model(&Star{}).Select("COUNT(*)").Where("uid = ?", userID),
  303. ).
  304. Error
  305. if err != nil {
  306. return errors.Wrap(err, `update "user.num_stars"`)
  307. }
  308. return nil
  309. }
  310. func (db *repos) Star(ctx context.Context, userID, repoID int64) error {
  311. return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  312. s := &Star{
  313. UserID: userID,
  314. RepoID: repoID,
  315. }
  316. result := tx.FirstOrCreate(s, s)
  317. if result.Error != nil {
  318. return errors.Wrap(result.Error, "upsert")
  319. } else if result.RowsAffected <= 0 {
  320. return nil // Relation already exists
  321. }
  322. return db.recountStars(tx, userID, repoID)
  323. })
  324. }
  325. func (db *repos) Touch(ctx context.Context, id int64) error {
  326. return db.WithContext(ctx).
  327. Model(new(Repository)).
  328. Where("id = ?", id).
  329. Updates(map[string]any{
  330. "is_bare": false,
  331. "updated_unix": db.NowFunc().Unix(),
  332. }).
  333. Error
  334. }
  335. func (db *repos) ListWatches(ctx context.Context, repoID int64) ([]*Watch, error) {
  336. var watches []*Watch
  337. return watches, db.WithContext(ctx).Where("repo_id = ?", repoID).Find(&watches).Error
  338. }
  339. func (db *repos) recountWatches(tx *gorm.DB, repoID int64) error {
  340. /*
  341. Equivalent SQL for PostgreSQL:
  342. UPDATE repository
  343. SET num_watches = (
  344. SELECT COUNT(*) FROM watch WHERE repo_id = @repoID
  345. )
  346. WHERE id = @repoID
  347. */
  348. return tx.Model(&Repository{}).
  349. Where("id = ?", repoID).
  350. Update(
  351. "num_watches",
  352. tx.Model(&Watch{}).Select("COUNT(*)").Where("repo_id = ?", repoID),
  353. ).
  354. Error
  355. }
  356. func (db *repos) Watch(ctx context.Context, userID, repoID int64) error {
  357. return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  358. w := &Watch{
  359. UserID: userID,
  360. RepoID: repoID,
  361. }
  362. result := tx.FirstOrCreate(w, w)
  363. if result.Error != nil {
  364. return errors.Wrap(result.Error, "upsert")
  365. } else if result.RowsAffected <= 0 {
  366. return nil // Relation already exists
  367. }
  368. return db.recountWatches(tx, repoID)
  369. })
  370. }
  371. func (db *repos) HasForkedBy(ctx context.Context, repoID, userID int64) bool {
  372. var count int64
  373. db.WithContext(ctx).Model(new(Repository)).Where("owner_id = ? AND fork_id = ?", userID, repoID).Count(&count)
  374. return count > 0
  375. }