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.

pushmirror.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "time"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "code.gitea.io/gitea/modules/util"
  11. "xorm.io/builder"
  12. )
  13. // ErrPushMirrorNotExist mirror does not exist error
  14. var ErrPushMirrorNotExist = util.NewNotExistErrorf("PushMirror does not exist")
  15. // PushMirror represents mirror information of a repository.
  16. type PushMirror struct {
  17. ID int64 `xorm:"pk autoincr"`
  18. RepoID int64 `xorm:"INDEX"`
  19. Repo *Repository `xorm:"-"`
  20. RemoteName string
  21. SyncOnCommit bool `xorm:"NOT NULL DEFAULT true"`
  22. Interval time.Duration
  23. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  24. LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
  25. LastError string `xorm:"text"`
  26. }
  27. type PushMirrorOptions struct {
  28. ID int64
  29. RepoID int64
  30. RemoteName string
  31. }
  32. func (opts *PushMirrorOptions) toConds() builder.Cond {
  33. cond := builder.NewCond()
  34. if opts.RepoID > 0 {
  35. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  36. }
  37. if opts.RemoteName != "" {
  38. cond = cond.And(builder.Eq{"remote_name": opts.RemoteName})
  39. }
  40. if opts.ID > 0 {
  41. cond = cond.And(builder.Eq{"id": opts.ID})
  42. }
  43. return cond
  44. }
  45. func init() {
  46. db.RegisterModel(new(PushMirror))
  47. }
  48. // GetRepository returns the path of the repository.
  49. func (m *PushMirror) GetRepository() *Repository {
  50. if m.Repo != nil {
  51. return m.Repo
  52. }
  53. var err error
  54. m.Repo, err = GetRepositoryByID(db.DefaultContext, m.RepoID)
  55. if err != nil {
  56. log.Error("getRepositoryByID[%d]: %v", m.ID, err)
  57. }
  58. return m.Repo
  59. }
  60. // GetRemoteName returns the name of the remote.
  61. func (m *PushMirror) GetRemoteName() string {
  62. return m.RemoteName
  63. }
  64. // InsertPushMirror inserts a push-mirror to database
  65. func InsertPushMirror(ctx context.Context, m *PushMirror) error {
  66. _, err := db.GetEngine(ctx).Insert(m)
  67. return err
  68. }
  69. // UpdatePushMirror updates the push-mirror
  70. func UpdatePushMirror(ctx context.Context, m *PushMirror) error {
  71. _, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
  72. return err
  73. }
  74. func DeletePushMirrors(ctx context.Context, opts PushMirrorOptions) error {
  75. if opts.RepoID > 0 {
  76. _, err := db.GetEngine(ctx).Where(opts.toConds()).Delete(&PushMirror{})
  77. return err
  78. }
  79. return util.NewInvalidArgumentErrorf("repoID required and must be set")
  80. }
  81. func GetPushMirror(ctx context.Context, opts PushMirrorOptions) (*PushMirror, error) {
  82. mirror := &PushMirror{}
  83. exist, err := db.GetEngine(ctx).Where(opts.toConds()).Get(mirror)
  84. if err != nil {
  85. return nil, err
  86. } else if !exist {
  87. return nil, ErrPushMirrorNotExist
  88. }
  89. return mirror, nil
  90. }
  91. // GetPushMirrorsByRepoID returns push-mirror information of a repository.
  92. func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*PushMirror, int64, error) {
  93. sess := db.GetEngine(ctx).Where("repo_id = ?", repoID)
  94. if listOptions.Page != 0 {
  95. sess = db.SetSessionPagination(sess, &listOptions)
  96. mirrors := make([]*PushMirror, 0, listOptions.PageSize)
  97. count, err := sess.FindAndCount(&mirrors)
  98. return mirrors, count, err
  99. }
  100. mirrors := make([]*PushMirror, 0, 10)
  101. count, err := sess.FindAndCount(&mirrors)
  102. return mirrors, count, err
  103. }
  104. // GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits
  105. func GetPushMirrorsSyncedOnCommit(ctx context.Context, repoID int64) ([]*PushMirror, error) {
  106. mirrors := make([]*PushMirror, 0, 10)
  107. return mirrors, db.GetEngine(ctx).
  108. Where("repo_id=? AND sync_on_commit=?", repoID, true).
  109. Find(&mirrors)
  110. }
  111. // PushMirrorsIterate iterates all push-mirror repositories.
  112. func PushMirrorsIterate(ctx context.Context, limit int, f func(idx int, bean interface{}) error) error {
  113. sess := db.GetEngine(ctx).
  114. Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
  115. And("`interval` != 0").
  116. OrderBy("last_update ASC")
  117. if limit > 0 {
  118. sess = sess.Limit(limit)
  119. }
  120. return sess.Iterate(new(PushMirror), f)
  121. }