code.gitea.io/gitea@v1.21.7/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  	ids := make(container.Set[int64], len(runners))
    20  	for _, runner := range runners {
    21  		if runner.OwnerID == 0 {
    22  			continue
    23  		}
    24  		ids.Add(runner.OwnerID)
    25  	}
    26  	return ids.Values()
    27  }
    28  
    29  func (runners RunnerList) LoadOwners(ctx context.Context) error {
    30  	userIDs := runners.GetUserIDs()
    31  	users := make(map[int64]*user_model.User, len(userIDs))
    32  	if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
    33  		return err
    34  	}
    35  	for _, runner := range runners {
    36  		if runner.OwnerID > 0 && runner.Owner == nil {
    37  			runner.Owner = users[runner.OwnerID]
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  func (runners RunnerList) getRepoIDs() []int64 {
    44  	repoIDs := make(container.Set[int64], len(runners))
    45  	for _, runner := range runners {
    46  		if runner.RepoID == 0 {
    47  			continue
    48  		}
    49  		if _, ok := repoIDs[runner.RepoID]; !ok {
    50  			repoIDs[runner.RepoID] = struct{}{}
    51  		}
    52  	}
    53  	return repoIDs.Values()
    54  }
    55  
    56  func (runners RunnerList) LoadRepos(ctx context.Context) error {
    57  	repoIDs := runners.getRepoIDs()
    58  	repos := make(map[int64]*repo_model.Repository, len(repoIDs))
    59  	if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
    60  		return err
    61  	}
    62  
    63  	for _, runner := range runners {
    64  		if runner.RepoID > 0 && runner.Repo == nil {
    65  			runner.Repo = repos[runner.RepoID]
    66  		}
    67  	}
    68  	return nil
    69  }
    70  
    71  func (runners RunnerList) LoadAttributes(ctx context.Context) error {
    72  	if err := runners.LoadOwners(ctx); err != nil {
    73  		return err
    74  	}
    75  
    76  	return runners.LoadRepos(ctx)
    77  }