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_mirror.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/xorm"
  11. )
  12. // RemoteMirrorer defines base methods for pull/push mirrors.
  13. type RemoteMirrorer interface {
  14. GetRepository() *Repository
  15. GetRemoteName() string
  16. }
  17. // Mirror represents mirror information of a repository.
  18. type Mirror struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. RepoID int64 `xorm:"INDEX"`
  21. Repo *Repository `xorm:"-"`
  22. Interval time.Duration
  23. EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
  24. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX"`
  25. NextUpdateUnix timeutil.TimeStamp `xorm:"INDEX"`
  26. LFS bool `xorm:"lfs_enabled NOT NULL DEFAULT false"`
  27. LFSEndpoint string `xorm:"lfs_endpoint TEXT"`
  28. Address string `xorm:"-"`
  29. }
  30. // BeforeInsert will be invoked by XORM before inserting a record
  31. func (m *Mirror) BeforeInsert() {
  32. if m != nil {
  33. m.UpdatedUnix = timeutil.TimeStampNow()
  34. m.NextUpdateUnix = timeutil.TimeStampNow()
  35. }
  36. }
  37. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  38. func (m *Mirror) AfterLoad(session *xorm.Session) {
  39. if m == nil {
  40. return
  41. }
  42. var err error
  43. m.Repo, err = getRepositoryByID(session, m.RepoID)
  44. if err != nil {
  45. log.Error("getRepositoryByID[%d]: %v", m.ID, err)
  46. }
  47. }
  48. // GetRepository returns the repository.
  49. func (m *Mirror) GetRepository() *Repository {
  50. return m.Repo
  51. }
  52. // GetRemoteName returns the name of the remote.
  53. func (m *Mirror) GetRemoteName() string {
  54. return "origin"
  55. }
  56. // ScheduleNextUpdate calculates and sets next update time.
  57. func (m *Mirror) ScheduleNextUpdate() {
  58. if m.Interval != 0 {
  59. m.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(m.Interval)
  60. } else {
  61. m.NextUpdateUnix = 0
  62. }
  63. }
  64. func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
  65. m := &Mirror{RepoID: repoID}
  66. has, err := e.Get(m)
  67. if err != nil {
  68. return nil, err
  69. } else if !has {
  70. return nil, ErrMirrorNotExist
  71. }
  72. return m, nil
  73. }
  74. // GetMirrorByRepoID returns mirror information of a repository.
  75. func GetMirrorByRepoID(repoID int64) (*Mirror, error) {
  76. return getMirrorByRepoID(x, repoID)
  77. }
  78. func updateMirror(e Engine, m *Mirror) error {
  79. _, err := e.ID(m.ID).AllCols().Update(m)
  80. return err
  81. }
  82. // UpdateMirror updates the mirror
  83. func UpdateMirror(m *Mirror) error {
  84. return updateMirror(x, m)
  85. }
  86. // DeleteMirrorByRepoID deletes a mirror by repoID
  87. func DeleteMirrorByRepoID(repoID int64) error {
  88. _, err := x.Delete(&Mirror{RepoID: repoID})
  89. return err
  90. }
  91. // MirrorsIterate iterates all mirror repositories.
  92. func MirrorsIterate(f func(idx int, bean interface{}) error) error {
  93. return x.
  94. Where("next_update_unix<=?", time.Now().Unix()).
  95. And("next_update_unix!=0").
  96. Iterate(new(Mirror), f)
  97. }
  98. // InsertMirror inserts a mirror to database
  99. func InsertMirror(mirror *Mirror) error {
  100. _, err := x.Insert(mirror)
  101. return err
  102. }