55package repo
66
77import (
8+ "context"
89"errors"
910"time"
1011
1112"code.gitea.io/gitea/models/db"
1213"code.gitea.io/gitea/modules/log"
1314"code.gitea.io/gitea/modules/timeutil"
15+
16+ "xorm.io/builder"
1417)
1518
1619// ErrPushMirrorNotExist mirror does not exist error
@@ -29,6 +32,25 @@ type PushMirror struct {
2932LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
3033LastError string `xorm:"text"`
3134}
35+ type PushMirrorOptions struct {
36+ ID int64
37+ RepoID int64
38+ RemoteName string
39+ }
40+
41+ func (opts * PushMirrorOptions ) toConds () builder.Cond {
42+ cond := builder .NewCond ()
43+ if opts .RepoID > 0 {
44+ cond = cond .And (builder.Eq {"repo_id" : opts .RepoID })
45+ }
46+ if opts .RemoteName != "" {
47+ cond = cond .And (builder.Eq {"remote_name" : opts .RemoteName })
48+ }
49+ if opts .ID > 0 {
50+ cond = cond .And (builder.Eq {"id" : opts .ID })
51+ }
52+ return cond
53+ }
3254
3355func init () {
3456db .RegisterModel (new (PushMirror ))
@@ -53,45 +75,48 @@ func (m *PushMirror) GetRemoteName() string {
5375}
5476
5577// InsertPushMirror inserts a push-mirror to database
56- func InsertPushMirror (m * PushMirror ) error {
57- _ , err := db .GetEngine (db . DefaultContext ).Insert (m )
78+ func InsertPushMirror (ctx context. Context , m * PushMirror ) error {
79+ _ , err := db .GetEngine (ctx ).Insert (m )
5880return err
5981}
6082
6183// UpdatePushMirror updates the push-mirror
62- func UpdatePushMirror (m * PushMirror ) error {
63- _ , err := db .GetEngine (db .DefaultContext ).ID (m .ID ).AllCols ().Update (m )
64- return err
65- }
66-
67- // DeletePushMirrorByID deletes a push-mirrors by ID
68- func DeletePushMirrorByID (ID int64 ) error {
69- _ , err := db .GetEngine (db .DefaultContext ).ID (ID ).Delete (& PushMirror {})
84+ func UpdatePushMirror (ctx context.Context , m * PushMirror ) error {
85+ _ , err := db .GetEngine (ctx ).ID (m .ID ).AllCols ().Update (m )
7086return err
7187}
7288
73- // DeletePushMirrorsByRepoID deletes all push-mirrors by repoID
74- func DeletePushMirrorsByRepoID (repoID int64 ) error {
75- _ , err := db .GetEngine (db .DefaultContext ).Delete (& PushMirror {RepoID : repoID })
76- return err
89+ func DeletePushMirrors (ctx context.Context , opts PushMirrorOptions ) error {
90+ if opts .RepoID > 0 {
91+ _ , err := db .GetEngine (ctx ).Where (opts .toConds ()).Delete (& PushMirror {})
92+ return err
93+ }
94+ return errors .New ("repoID required and must be set" )
7795}
7896
79- // GetPushMirrorByID returns push-mirror information.
80- func GetPushMirrorByID (ID int64 ) (* PushMirror , error ) {
81- m := & PushMirror {}
82- has , err := db .GetEngine (db .DefaultContext ).ID (ID ).Get (m )
97+ func GetPushMirror (ctx context.Context , opts PushMirrorOptions ) (* PushMirror , error ) {
98+ mirror := & PushMirror {}
99+ exist , err := db .GetEngine (ctx ).Where (opts .toConds ()).Get (mirror )
83100if err != nil {
84101return nil , err
85- } else if ! has {
102+ } else if ! exist {
86103return nil , ErrPushMirrorNotExist
87104}
88- return m , nil
105+ return mirror , nil
89106}
90107
91108// GetPushMirrorsByRepoID returns push-mirror information of a repository.
92- func GetPushMirrorsByRepoID (repoID int64 ) ([]* PushMirror , error ) {
109+ func GetPushMirrorsByRepoID (ctx context.Context , repoID int64 , listOptions db.ListOptions ) ([]* PushMirror , int64 , error ) {
110+ sess := db .GetEngine (ctx ).Where ("repo_id = ?" , repoID )
111+ if listOptions .Page != 0 {
112+ sess = db .SetSessionPagination (sess , & listOptions )
113+ mirrors := make ([]* PushMirror , 0 , listOptions .PageSize )
114+ count , err := sess .FindAndCount (& mirrors )
115+ return mirrors , count , err
116+ }
93117mirrors := make ([]* PushMirror , 0 , 10 )
94- return mirrors , db .GetEngine (db .DefaultContext ).Where ("repo_id=?" , repoID ).Find (& mirrors )
118+ count , err := sess .FindAndCount (& mirrors )
119+ return mirrors , count , err
95120}
96121
97122// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits
@@ -103,8 +128,8 @@ func GetPushMirrorsSyncedOnCommit(repoID int64) ([]*PushMirror, error) {
103128}
104129
105130// PushMirrorsIterate iterates all push-mirror repositories.
106- func PushMirrorsIterate (limit int , f func (idx int , bean interface {}) error ) error {
107- return db .GetEngine (db . DefaultContext ).
131+ func PushMirrorsIterate (ctx context. Context , limit int , f func (idx int , bean interface {}) error ) error {
132+ return db .GetEngine (ctx ).
108133Where ("last_update + (`interval` / ?) <= ?" , time .Second , time .Now ().Unix ()).
109134And ("`interval` != 0" ).
110135OrderBy ("last_update ASC" ).
0 commit comments