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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2021 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 repo
  5. import (
  6. "errors"
  7. "time"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. )
  12. // ErrPushMirrorNotExist mirror does not exist error
  13. var ErrPushMirrorNotExist = errors.New("PushMirror does not exist")
  14. // PushMirror represents mirror information of a repository.
  15. type PushMirror struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. RepoID int64 `xorm:"INDEX"`
  18. Repo *Repository `xorm:"-"`
  19. RemoteName string
  20. Interval time.Duration
  21. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  22. LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
  23. LastError string `xorm:"text"`
  24. }
  25. func init() {
  26. db.RegisterModel(new(PushMirror))
  27. }
  28. // GetRepository returns the path of the repository.
  29. func (m *PushMirror) GetRepository() *Repository {
  30. if m.Repo != nil {
  31. return m.Repo
  32. }
  33. var err error
  34. m.Repo, err = GetRepositoryByIDCtx(db.DefaultContext, m.RepoID)
  35. if err != nil {
  36. log.Error("getRepositoryByID[%d]: %v", m.ID, err)
  37. }
  38. return m.Repo
  39. }
  40. // GetRemoteName returns the name of the remote.
  41. func (m *PushMirror) GetRemoteName() string {
  42. return m.RemoteName
  43. }
  44. // InsertPushMirror inserts a push-mirror to database
  45. func InsertPushMirror(m *PushMirror) error {
  46. _, err := db.GetEngine(db.DefaultContext).Insert(m)
  47. return err
  48. }
  49. // UpdatePushMirror updates the push-mirror
  50. func UpdatePushMirror(m *PushMirror) error {
  51. _, err := db.GetEngine(db.DefaultContext).ID(m.ID).AllCols().Update(m)
  52. return err
  53. }
  54. // DeletePushMirrorByID deletes a push-mirrors by ID
  55. func DeletePushMirrorByID(ID int64) error {
  56. _, err := db.GetEngine(db.DefaultContext).ID(ID).Delete(&PushMirror{})
  57. return err
  58. }
  59. // DeletePushMirrorsByRepoID deletes all push-mirrors by repoID
  60. func DeletePushMirrorsByRepoID(repoID int64) error {
  61. _, err := db.GetEngine(db.DefaultContext).Delete(&PushMirror{RepoID: repoID})
  62. return err
  63. }
  64. // GetPushMirrorByID returns push-mirror information.
  65. func GetPushMirrorByID(ID int64) (*PushMirror, error) {
  66. m := &PushMirror{}
  67. has, err := db.GetEngine(db.DefaultContext).ID(ID).Get(m)
  68. if err != nil {
  69. return nil, err
  70. } else if !has {
  71. return nil, ErrPushMirrorNotExist
  72. }
  73. return m, nil
  74. }
  75. // GetPushMirrorsByRepoID returns push-mirror information of a repository.
  76. func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
  77. mirrors := make([]*PushMirror, 0, 10)
  78. return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors)
  79. }
  80. // PushMirrorsIterate iterates all push-mirror repositories.
  81. func PushMirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
  82. return db.GetEngine(db.DefaultContext).
  83. Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
  84. And("`interval` != 0").
  85. OrderBy("last_update ASC").
  86. Limit(limit).
  87. Iterate(new(PushMirror), f)
  88. }