code.gitea.io/gitea@v1.22.3/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  	return container.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
    20  		return j.RunID, j.RunID != 0
    21  	})
    22  }
    23  
    24  func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
    25  	runIDs := jobs.GetRunIDs()
    26  	runs := make(map[int64]*ActionRun, len(runIDs))
    27  	if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
    28  		return err
    29  	}
    30  	for _, j := range jobs {
    31  		if j.RunID > 0 && j.Run == nil {
    32  			j.Run = runs[j.RunID]
    33  		}
    34  	}
    35  	if withRepo {
    36  		var runsList RunList = make([]*ActionRun, 0, len(runs))
    37  		for _, r := range runs {
    38  			runsList = append(runsList, r)
    39  		}
    40  		return runsList.LoadRepos(ctx)
    41  	}
    42  	return nil
    43  }
    44  
    45  func (jobs ActionJobList) LoadAttributes(ctx context.Context, withRepo bool) error {
    46  	return jobs.LoadRuns(ctx, withRepo)
    47  }
    48  
    49  type FindRunJobOptions struct {
    50  	db.ListOptions
    51  	RunID         int64
    52  	RepoID        int64
    53  	OwnerID       int64
    54  	CommitSHA     string
    55  	Statuses      []Status
    56  	UpdatedBefore timeutil.TimeStamp
    57  }
    58  
    59  func (opts FindRunJobOptions) ToConds() builder.Cond {
    60  	cond := builder.NewCond()
    61  	if opts.RunID > 0 {
    62  		cond = cond.And(builder.Eq{"run_id": opts.RunID})
    63  	}
    64  	if opts.RepoID > 0 {
    65  		cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
    66  	}
    67  	if opts.OwnerID > 0 {
    68  		cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
    69  	}
    70  	if opts.CommitSHA != "" {
    71  		cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
    72  	}
    73  	if len(opts.Statuses) > 0 {
    74  		cond = cond.And(builder.In("status", opts.Statuses))
    75  	}
    76  	if opts.UpdatedBefore > 0 {
    77  		cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
    78  	}
    79  	return cond
    80  }