code.gitea.io/gitea@v1.21.7/models/actions/run_job_list.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package actions 5 6 import ( 7 "context" 8 9 "code.gitea.io/gitea/models/db" 10 "code.gitea.io/gitea/modules/container" 11 "code.gitea.io/gitea/modules/timeutil" 12 13 "xorm.io/builder" 14 ) 15 16 type ActionJobList []*ActionRunJob 17 18 func (jobs ActionJobList) GetRunIDs() []int64 { 19 ids := make(container.Set[int64], len(jobs)) 20 for _, j := range jobs { 21 if j.RunID == 0 { 22 continue 23 } 24 ids.Add(j.RunID) 25 } 26 return ids.Values() 27 } 28 29 func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error { 30 runIDs := jobs.GetRunIDs() 31 runs := make(map[int64]*ActionRun, len(runIDs)) 32 if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil { 33 return err 34 } 35 for _, j := range jobs { 36 if j.RunID > 0 && j.Run == nil { 37 j.Run = runs[j.RunID] 38 } 39 } 40 if withRepo { 41 var runsList RunList = make([]*ActionRun, 0, len(runs)) 42 for _, r := range runs { 43 runsList = append(runsList, r) 44 } 45 return runsList.LoadRepos() 46 } 47 return nil 48 } 49 50 func (jobs ActionJobList) LoadAttributes(ctx context.Context, withRepo bool) error { 51 return jobs.LoadRuns(ctx, withRepo) 52 } 53 54 type FindRunJobOptions struct { 55 db.ListOptions 56 RunID int64 57 RepoID int64 58 OwnerID int64 59 CommitSHA string 60 Statuses []Status 61 UpdatedBefore timeutil.TimeStamp 62 } 63 64 func (opts FindRunJobOptions) toConds() builder.Cond { 65 cond := builder.NewCond() 66 if opts.RunID > 0 { 67 cond = cond.And(builder.Eq{"run_id": opts.RunID}) 68 } 69 if opts.RepoID > 0 { 70 cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) 71 } 72 if opts.OwnerID > 0 { 73 cond = cond.And(builder.Eq{"owner_id": opts.OwnerID}) 74 } 75 if opts.CommitSHA != "" { 76 cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA}) 77 } 78 if len(opts.Statuses) > 0 { 79 cond = cond.And(builder.In("status", opts.Statuses)) 80 } 81 if opts.UpdatedBefore > 0 { 82 cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore}) 83 } 84 return cond 85 } 86 87 func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (ActionJobList, int64, error) { 88 e := db.GetEngine(ctx).Where(opts.toConds()) 89 if opts.PageSize > 0 && opts.Page >= 1 { 90 e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) 91 } 92 var tasks ActionJobList 93 total, err := e.FindAndCount(&tasks) 94 return tasks, total, err 95 } 96 97 func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) { 98 return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRunJob)) 99 }