@@ -9,15 +9,13 @@ import (
99
1010 "code.gitea.io/gitea/models/db"
1111 "code.gitea.io/gitea/modules/log"
12+ "code.gitea.io/gitea/modules/optional"
1213 "code.gitea.io/gitea/modules/timeutil"
1314 "code.gitea.io/gitea/modules/util"
1415
1516 "xorm.io/builder"
1617)
1718
18- // ErrPushMirrorNotExist mirror does not exist error
19- var ErrPushMirrorNotExist = util .NewNotExistErrorf ("PushMirror does not exist" )
20-
2119// PushMirror represents mirror information of a repository.
2220type PushMirror struct {
2321 ID int64 `xorm:"pk autoincr"`
@@ -96,26 +94,46 @@ func DeletePushMirrors(ctx context.Context, opts PushMirrorOptions) error {
9694 return util .NewInvalidArgumentErrorf ("repoID required and must be set" )
9795}
9896
97+ type findPushMirrorOptions struct {
98+ db.ListOptions
99+ RepoID int64
100+ SyncOnCommit optional.Option [bool ]
101+ }
102+
103+ func (opts findPushMirrorOptions ) ToConds () builder.Cond {
104+ cond := builder .NewCond ()
105+ if opts .RepoID > 0 {
106+ cond = cond .And (builder.Eq {"repo_id" : opts .RepoID })
107+ }
108+ if opts .SyncOnCommit .Has () {
109+ cond = cond .And (builder.Eq {"sync_on_commit" : opts .SyncOnCommit .Value ()})
110+ }
111+ return cond
112+ }
113+
99114// GetPushMirrorsByRepoID returns push-mirror information of a repository.
100115func 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
116+ return db .FindAndCount [PushMirror ](ctx , findPushMirrorOptions {
117+ ListOptions : listOptions ,
118+ RepoID : repoID ,
119+ })
120+ }
121+
122+ func GetPushMirrorByIDAndRepoID (ctx context.Context , id , repoID int64 ) (* PushMirror , bool , error ) {
123+ var pushMirror PushMirror
124+ has , err := db .GetEngine (ctx ).Where ("id = ?" , id ).And ("repo_id = ?" , repoID ).Get (& pushMirror )
125+ if ! has || err != nil {
126+ return nil , has , err
107127 }
108- mirrors := make ([]* PushMirror , 0 , 10 )
109- count , err := sess .FindAndCount (& mirrors )
110- return mirrors , count , err
128+ return & pushMirror , true , nil
111129}
112130
113131// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits
114132func 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 )
133+ return db . Find [ PushMirror ]( ctx , findPushMirrorOptions {
134+ RepoID : repoID ,
135+ SyncOnCommit : optional . Some ( true ),
136+ } )
119137}
120138
121139// PushMirrorsIterate iterates all push-mirror repositories.
0 commit comments