5
5
package repo
6
6
7
7
import (
8
+ "context"
8
9
"errors"
9
10
"time"
10
11
11
12
"code.gitea.io/gitea/models/db"
12
13
"code.gitea.io/gitea/modules/log"
13
14
"code.gitea.io/gitea/modules/timeutil"
15
+
16
+ "xorm.io/builder"
14
17
)
15
18
16
19
// ErrPushMirrorNotExist mirror does not exist error
@@ -29,6 +32,25 @@ type PushMirror struct {
29
32
LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
30
33
LastError string `xorm:"text"`
31
34
}
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
+ }
32
54
33
55
func init () {
34
56
db .RegisterModel (new (PushMirror ))
@@ -53,45 +75,48 @@ func (m *PushMirror) GetRemoteName() string {
53
75
}
54
76
55
77
// 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 )
58
80
return err
59
81
}
60
82
61
83
// 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 )
70
86
return err
71
87
}
72
88
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" )
77
95
}
78
96
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 )
83
100
if err != nil {
84
101
return nil , err
85
- } else if ! has {
102
+ } else if ! exist {
86
103
return nil , ErrPushMirrorNotExist
87
104
}
88
- return m , nil
105
+ return mirror , nil
89
106
}
90
107
91
108
// 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
+ }
93
117
mirrors := 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
95
120
}
96
121
97
122
// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits
@@ -103,8 +128,8 @@ func GetPushMirrorsSyncedOnCommit(repoID int64) ([]*PushMirror, error) {
103
128
}
104
129
105
130
// 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 ).
108
133
Where ("last_update + (`interval` / ?) <= ?" , time .Second , time .Now ().Unix ()).
109
134
And ("`interval` != 0" ).
110
135
OrderBy ("last_update ASC" ).
0 commit comments