@@ -25,6 +25,7 @@ import (
25
25
"code.gitea.io/gitea/modules/translation"
26
26
27
27
"xorm.io/builder"
28
+ "xorm.io/xorm"
28
29
)
29
30
30
31
// CommitStatus holds a single Status of a single Commit
@@ -269,10 +270,12 @@ type CommitStatusIndex struct {
269
270
270
271
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
271
272
func GetLatestCommitStatus (ctx context.Context , repoID int64 , sha string , listOptions db.ListOptions ) ([]* CommitStatus , int64 , error ) {
272
- base := db .GetEngine (ctx ).Table (& CommitStatus {}).
273
- Where ("repo_id = ?" , repoID ).And ("sha = ?" , sha )
273
+ getBase := func () * xorm.Session {
274
+ return db .GetEngine (ctx ).Table (& CommitStatus {}).
275
+ Where ("repo_id = ?" , repoID ).And ("sha = ?" , sha )
276
+ }
274
277
indices := make ([]int64 , 0 , 10 )
275
- sess := base .Select ("max( `index` ) as `index`" ).
278
+ sess := getBase () .Select ("max( `index` ) as `index`" ).
276
279
GroupBy ("context_hash" ).OrderBy ("max( `index` ) desc" )
277
280
if ! listOptions .IsListAll () {
278
281
sess = db .SetSessionPagination (sess , & listOptions )
@@ -285,7 +288,7 @@ func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOp
285
288
if len (indices ) == 0 {
286
289
return statuses , count , nil
287
290
}
288
- return statuses , count , base .And (builder .In ("`index`" , indices )).Find (& statuses )
291
+ return statuses , count , getBase () .And (builder .In ("`index`" , indices )).Find (& statuses )
289
292
}
290
293
291
294
// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
@@ -297,14 +300,16 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHA
297
300
298
301
results := make ([]result , 0 , len (repoIDsToLatestCommitSHAs ))
299
302
300
- base := db .GetEngine (ctx ).Table (& CommitStatus {})
303
+ getBase := func () * xorm.Session {
304
+ return db .GetEngine (ctx ).Table (& CommitStatus {})
305
+ }
301
306
302
307
// Create a disjunction of conditions for each repoID and SHA pair
303
308
conds := make ([]builder.Cond , 0 , len (repoIDsToLatestCommitSHAs ))
304
309
for repoID , sha := range repoIDsToLatestCommitSHAs {
305
310
conds = append (conds , builder.Eq {"repo_id" : repoID , "sha" : sha })
306
311
}
307
- sess := base .Where (builder .Or (conds ... )).
312
+ sess := getBase () .Where (builder .Or (conds ... )).
308
313
Select ("max( `index` ) as `index`, repo_id" ).
309
314
GroupBy ("context_hash, repo_id" ).OrderBy ("max( `index` ) desc" )
310
315
@@ -331,7 +336,7 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHA
331
336
}
332
337
conds = append (conds , cond )
333
338
}
334
- err = base .Where (builder .Or (conds ... )).Find (& statuses )
339
+ err = getBase () .Where (builder .Or (conds ... )).Find (& statuses )
335
340
if err != nil {
336
341
return nil , err
337
342
}
@@ -351,14 +356,17 @@ func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, co
351
356
Index int64
352
357
SHA string
353
358
}
354
- base := db .GetEngine (ctx ).Table (& CommitStatus {}).Where ("repo_id = ?" , repoID )
359
+
360
+ getBase := func () * xorm.Session {
361
+ return db .GetEngine (ctx ).Table (& CommitStatus {}).Where ("repo_id = ?" , repoID )
362
+ }
355
363
results := make ([]result , 0 , len (commitIDs ))
356
364
357
365
conds := make ([]builder.Cond , 0 , len (commitIDs ))
358
366
for _ , sha := range commitIDs {
359
367
conds = append (conds , builder.Eq {"sha" : sha })
360
368
}
361
- sess := base .And (builder .Or (conds ... )).
369
+ sess := getBase () .And (builder .Or (conds ... )).
362
370
Select ("max( `index` ) as `index`, sha" ).
363
371
GroupBy ("context_hash, sha" ).OrderBy ("max( `index` ) desc" )
364
372
@@ -376,7 +384,7 @@ func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, co
376
384
for _ , result := range results {
377
385
conds = append (conds , builder.Eq {"`index`" : result .Index , "sha" : result .SHA })
378
386
}
379
- err = base .And (builder .Or (conds ... )).Find (& statuses )
387
+ err = getBase () .And (builder .Or (conds ... )).Find (& statuses )
380
388
if err != nil {
381
389
return nil , err
382
390
}
@@ -396,11 +404,14 @@ func FindRepoRecentCommitStatusContexts(ctx context.Context, repoID int64, befor
396
404
Index int64
397
405
SHA string
398
406
}
407
+ getBase := func () * xorm.Session {
408
+ return db .GetEngine (ctx ).Table (& CommitStatus {}).Where ("repo_id = ?" , repoID )
409
+ }
410
+
399
411
start := timeutil .TimeStampNow ().AddDuration (- before )
400
- base := db .GetEngine (ctx ).Table (& CommitStatus {}).Where ("repo_id = ?" , repoID )
401
412
results := make ([]result , 0 , 10 )
402
413
403
- sess := base .And ("updated_unix >= ?" , start ).
414
+ sess := getBase () .And ("updated_unix >= ?" , start ).
404
415
Select ("max( `index` ) as `index`, sha" ).
405
416
GroupBy ("context_hash, sha" ).OrderBy ("max( `index` ) desc" )
406
417
@@ -418,7 +429,7 @@ func FindRepoRecentCommitStatusContexts(ctx context.Context, repoID int64, befor
418
429
for _ , result := range results {
419
430
conds = append (conds , builder.Eq {"`index`" : result .Index , "sha" : result .SHA })
420
431
}
421
- return contexts , base .And (builder .Or (conds ... )).Select ("context" ).Find (& contexts )
432
+ return contexts , getBase () .And (builder .Or (conds ... )).Select ("context" ).Find (& contexts )
422
433
}
423
434
424
435
// NewCommitStatusOptions holds options for creating a CommitStatus
0 commit comments