code.gitea.io/gitea@v1.22.3/models/actions/runner_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  )
    14  
    15  type RunnerList []*ActionRunner
    16  
    17  // GetUserIDs returns a slice of user's id
    18  func (runners RunnerList) GetUserIDs() []int64 {
    19  	return container.FilterSlice(runners, func(runner *ActionRunner) (int64, bool) {
    20  		return runner.OwnerID, runner.OwnerID != 0
    21  	})
    22  }
    23  
    24  func (runners RunnerList) LoadOwners(ctx context.Context) error {
    25  	userIDs := runners.GetUserIDs()
    26  	users := make(map[int64]*user_model.User, len(userIDs))
    27  	if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
    28  		return err
    29  	}
    30  	for _, runner := range runners {
    31  		if runner.OwnerID > 0 && runner.Owner == nil {
    32  			runner.Owner = users[runner.OwnerID]
    33  		}
    34  	}
    35  	return nil
    36  }
    37  
    38  func (runners RunnerList) getRepoIDs() []int64 {
    39  	return container.FilterSlice(runners, func(runner *ActionRunner) (int64, bool) {
    40  		return runner.RepoID, runner.RepoID > 0
    41  	})
    42  }
    43  
    44  func (runners RunnerList) LoadRepos(ctx context.Context) error {
    45  	repoIDs := runners.getRepoIDs()
    46  	repos := make(map[int64]*repo_model.Repository, len(repoIDs))
    47  	if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
    48  		return err
    49  	}
    50  
    51  	for _, runner := range runners {
    52  		if runner.RepoID > 0 && runner.Repo == nil {
    53  			runner.Repo = repos[runner.RepoID]
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  func (runners RunnerList) LoadAttributes(ctx context.Context) error {
    60  	if err := runners.LoadOwners(ctx); err != nil {
    61  		return err
    62  	}
    63  
    64  	return runners.LoadRepos(ctx)
    65  }