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