code.gitea.io/gitea@v1.22.3/models/actions/task_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 TaskList []*ActionTask
    17  
    18  func (tasks TaskList) GetJobIDs() []int64 {
    19  	return container.FilterSlice(tasks, func(t *ActionTask) (int64, bool) {
    20  		return t.JobID, t.JobID != 0
    21  	})
    22  }
    23  
    24  func (tasks TaskList) LoadJobs(ctx context.Context) error {
    25  	jobIDs := tasks.GetJobIDs()
    26  	jobs := make(map[int64]*ActionRunJob, len(jobIDs))
    27  	if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
    28  		return err
    29  	}
    30  	for _, t := range tasks {
    31  		if t.JobID > 0 && t.Job == nil {
    32  			t.Job = jobs[t.JobID]
    33  		}
    34  	}
    35  
    36  	// TODO: Replace with "ActionJobList(maps.Values(jobs))" once available
    37  	var jobsList ActionJobList = make([]*ActionRunJob, 0, len(jobs))
    38  	for _, j := range jobs {
    39  		jobsList = append(jobsList, j)
    40  	}
    41  	return jobsList.LoadAttributes(ctx, true)
    42  }
    43  
    44  func (tasks TaskList) LoadAttributes(ctx context.Context) error {
    45  	return tasks.LoadJobs(ctx)
    46  }
    47  
    48  type FindTaskOptions struct {
    49  	db.ListOptions
    50  	RepoID        int64
    51  	OwnerID       int64
    52  	CommitSHA     string
    53  	Status        Status
    54  	UpdatedBefore timeutil.TimeStamp
    55  	StartedBefore timeutil.TimeStamp
    56  	RunnerID      int64
    57  	IDOrderDesc   bool
    58  }
    59  
    60  func (opts FindTaskOptions) ToConds() builder.Cond {
    61  	cond := builder.NewCond()
    62  	if opts.RepoID > 0 {
    63  		cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
    64  	}
    65  	if opts.OwnerID > 0 {
    66  		cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
    67  	}
    68  	if opts.CommitSHA != "" {
    69  		cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
    70  	}
    71  	if opts.Status > StatusUnknown {
    72  		cond = cond.And(builder.Eq{"status": opts.Status})
    73  	}
    74  	if opts.UpdatedBefore > 0 {
    75  		cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
    76  	}
    77  	if opts.StartedBefore > 0 {
    78  		cond = cond.And(builder.Lt{"started": opts.StartedBefore})
    79  	}
    80  	if opts.RunnerID > 0 {
    81  		cond = cond.And(builder.Eq{"runner_id": opts.RunnerID})
    82  	}
    83  	return cond
    84  }
    85  
    86  func (opts FindTaskOptions) ToOrders() string {
    87  	if opts.IDOrderDesc {
    88  		return "`id` DESC"
    89  	}
    90  	return ""
    91  }