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

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