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.

repo_pushmirror.go 3.0KB

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