code.gitea.io/gitea@v1.21.7/models/actions/run_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 repo_model "code.gitea.io/gitea/models/repo" 11 user_model "code.gitea.io/gitea/models/user" 12 "code.gitea.io/gitea/modules/container" 13 webhook_module "code.gitea.io/gitea/modules/webhook" 14 15 "xorm.io/builder" 16 ) 17 18 type RunList []*ActionRun 19 20 // GetUserIDs returns a slice of user's id 21 func (runs RunList) GetUserIDs() []int64 { 22 ids := make(container.Set[int64], len(runs)) 23 for _, run := range runs { 24 ids.Add(run.TriggerUserID) 25 } 26 return ids.Values() 27 } 28 29 func (runs RunList) GetRepoIDs() []int64 { 30 ids := make(container.Set[int64], len(runs)) 31 for _, run := range runs { 32 ids.Add(run.RepoID) 33 } 34 return ids.Values() 35 } 36 37 func (runs RunList) LoadTriggerUser(ctx context.Context) error { 38 userIDs := runs.GetUserIDs() 39 users := make(map[int64]*user_model.User, len(userIDs)) 40 if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil { 41 return err 42 } 43 for _, run := range runs { 44 if run.TriggerUserID == user_model.ActionsUserID { 45 run.TriggerUser = user_model.NewActionsUser() 46 } else { 47 run.TriggerUser = users[run.TriggerUserID] 48 if run.TriggerUser == nil { 49 run.TriggerUser = user_model.NewGhostUser() 50 } 51 } 52 } 53 return nil 54 } 55 56 func (runs RunList) LoadRepos() error { 57 repoIDs := runs.GetRepoIDs() 58 repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs) 59 if err != nil { 60 return err 61 } 62 for _, run := range runs { 63 run.Repo = repos[run.RepoID] 64 } 65 return nil 66 } 67 68 type FindRunOptions struct { 69 db.ListOptions 70 RepoID int64 71 OwnerID int64 72 WorkflowID string 73 Ref string // the commit/tag/… that caused this workflow 74 TriggerUserID int64 75 TriggerEvent webhook_module.HookEventType 76 Approved bool // not util.OptionalBool, it works only when it's true 77 Status []Status 78 } 79 80 func (opts FindRunOptions) toConds() builder.Cond { 81 cond := builder.NewCond() 82 if opts.RepoID > 0 { 83 cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) 84 } 85 if opts.OwnerID > 0 { 86 cond = cond.And(builder.Eq{"owner_id": opts.OwnerID}) 87 } 88 if opts.WorkflowID != "" { 89 cond = cond.And(builder.Eq{"workflow_id": opts.WorkflowID}) 90 } 91 if opts.TriggerUserID > 0 { 92 cond = cond.And(builder.Eq{"trigger_user_id": opts.TriggerUserID}) 93 } 94 if opts.Approved { 95 cond = cond.And(builder.Gt{"approved_by": 0}) 96 } 97 if len(opts.Status) > 0 { 98 cond = cond.And(builder.In("status", opts.Status)) 99 } 100 if opts.Ref != "" { 101 cond = cond.And(builder.Eq{"ref": opts.Ref}) 102 } 103 if opts.TriggerEvent != "" { 104 cond = cond.And(builder.Eq{"trigger_event": opts.TriggerEvent}) 105 } 106 return cond 107 } 108 109 func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) { 110 e := db.GetEngine(ctx).Where(opts.toConds()) 111 if opts.PageSize > 0 && opts.Page >= 1 { 112 e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) 113 } 114 var runs RunList 115 total, err := e.Desc("id").FindAndCount(&runs) 116 return runs, total, err 117 } 118 119 func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) { 120 return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRun)) 121 } 122 123 type StatusInfo struct { 124 Status int 125 DisplayedStatus string 126 } 127 128 // GetStatusInfoList returns a slice of StatusInfo 129 func GetStatusInfoList(ctx context.Context) []StatusInfo { 130 // same as those in aggregateJobStatus 131 allStatus := []Status{StatusSuccess, StatusFailure, StatusWaiting, StatusRunning} 132 statusInfoList := make([]StatusInfo, 0, 4) 133 for _, s := range allStatus { 134 statusInfoList = append(statusInfoList, StatusInfo{ 135 Status: int(s), 136 DisplayedStatus: s.String(), 137 }) 138 } 139 return statusInfoList 140 } 141 142 // GetActors returns a slice of Actors 143 func GetActors(ctx context.Context, repoID int64) ([]*user_model.User, error) { 144 actors := make([]*user_model.User, 0, 10) 145 146 return actors, db.GetEngine(ctx).Where(builder.In("id", builder.Select("`action_run`.trigger_user_id").From("`action_run`"). 147 GroupBy("`action_run`.trigger_user_id"). 148 Where(builder.Eq{"`action_run`.repo_id": repoID}))). 149 Cols("id", "name", "full_name", "avatar", "avatar_email", "use_custom_avatar"). 150 OrderBy(user_model.GetOrderByName()). 151 Find(&actors) 152 }