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