add runners management ui
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/builder"
|
||||
@@ -25,16 +27,18 @@ func (err ErrRunnerNotExist) Error() string {
|
||||
// Runner represents runner machines
|
||||
type Runner struct {
|
||||
ID int64
|
||||
UUID string `xorm:"CHAR(36) UNIQUE"`
|
||||
Name string `xorm:"VARCHAR(32) UNIQUE"`
|
||||
OS string `xorm:"VARCHAR(16) index"` // the runner running os
|
||||
Arch string `xorm:"VARCHAR(16) index"` // the runner running architecture
|
||||
Type string `xorm:"VARCHAR(16)"`
|
||||
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
|
||||
RepoID int64 `xorm:"index"` // repo level runner, if orgid also is zero, then it's a global
|
||||
Description string `xorm:"TEXT"`
|
||||
Base int // 0 native 1 docker 2 virtual machine
|
||||
RepoRange string // glob match which repositories could use this runner
|
||||
UUID string `xorm:"CHAR(36) UNIQUE"`
|
||||
Name string `xorm:"VARCHAR(32) UNIQUE"`
|
||||
OS string `xorm:"VARCHAR(16) index"` // the runner running os
|
||||
Arch string `xorm:"VARCHAR(16) index"` // the runner running architecture
|
||||
Type string `xorm:"VARCHAR(16)"`
|
||||
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
|
||||
Owner *user_model.User `xorm:"-"`
|
||||
RepoID int64 `xorm:"index"` // repo level runner, if orgid also is zero, then it's a global
|
||||
Repo *repo_model.Repository `xorm:"-"`
|
||||
Description string `xorm:"TEXT"`
|
||||
Base int // 0 native 1 docker 2 virtual machine
|
||||
RepoRange string // glob match which repositories could use this runner
|
||||
Token string
|
||||
LastOnline timeutil.TimeStamp `xorm:"index"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
@@ -44,16 +48,28 @@ func (Runner) TableName() string {
|
||||
return "bots_runner"
|
||||
}
|
||||
|
||||
func (r *Runner) OwnType() string {
|
||||
if r.OwnerID == 0 {
|
||||
return "Global Type"
|
||||
}
|
||||
if r.RepoID == 0 {
|
||||
return r.Owner.Name
|
||||
}
|
||||
|
||||
return r.Repo.FullName()
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(&Runner{})
|
||||
}
|
||||
|
||||
type GetRunnerOptions struct {
|
||||
type FindRunnerOptions struct {
|
||||
db.ListOptions
|
||||
RepoID int64
|
||||
OwnerID int64
|
||||
}
|
||||
|
||||
func (opts GetRunnerOptions) toCond() builder.Cond {
|
||||
func (opts FindRunnerOptions) toCond() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.RepoID > 0 {
|
||||
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
||||
@@ -65,8 +81,24 @@ func (opts GetRunnerOptions) toCond() builder.Cond {
|
||||
return cond
|
||||
}
|
||||
|
||||
func CountRunners(opts FindRunnerOptions) (int64, error) {
|
||||
return db.GetEngine(db.DefaultContext).
|
||||
Table("bots_runner").
|
||||
Where(opts.toCond()).
|
||||
Count()
|
||||
}
|
||||
|
||||
func FindRunners(opts FindRunnerOptions) (runners RunnerList, err error) {
|
||||
sess := db.GetEngine(db.DefaultContext).
|
||||
Where(opts.toCond())
|
||||
if opts.Page > 0 {
|
||||
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||
}
|
||||
return runners, sess.Find(&runners)
|
||||
}
|
||||
|
||||
// GetUsableRunner returns the usable runner
|
||||
func GetUsableRunner(opts GetRunnerOptions) (*Runner, error) {
|
||||
func GetUsableRunner(opts FindRunnerOptions) (*Runner, error) {
|
||||
var runner Runner
|
||||
has, err := db.GetEngine(db.DefaultContext).
|
||||
Where(opts.toCond()).
|
||||
|
||||
84
models/bots/runner_list.go
Normal file
84
models/bots/runner_list.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bots
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
)
|
||||
|
||||
type RunnerList []*Runner
|
||||
|
||||
// GetUserIDs returns a slice of user's id
|
||||
func (runners RunnerList) GetUserIDs() []int64 {
|
||||
userIDsMap := make(map[int64]struct{})
|
||||
for _, runner := range runners {
|
||||
if runner.OwnerID == 0 {
|
||||
continue
|
||||
}
|
||||
userIDsMap[runner.OwnerID] = struct{}{}
|
||||
}
|
||||
userIDs := make([]int64, 0, len(userIDsMap))
|
||||
for userID := range userIDsMap {
|
||||
userIDs = append(userIDs, userID)
|
||||
}
|
||||
return userIDs
|
||||
}
|
||||
|
||||
func (runners RunnerList) LoadOwners(ctx context.Context) error {
|
||||
userIDs := runners.GetUserIDs()
|
||||
users := make(map[int64]*user_model.User, len(userIDs))
|
||||
if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, runner := range runners {
|
||||
if runner.OwnerID > 0 && runner.Owner == nil {
|
||||
runner.Owner = users[runner.OwnerID]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (runners RunnerList) getRepoIDs() []int64 {
|
||||
repoIDs := make(map[int64]struct{}, len(runners))
|
||||
for _, runner := range runners {
|
||||
if runner.RepoID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := repoIDs[runner.RepoID]; !ok {
|
||||
repoIDs[runner.RepoID] = struct{}{}
|
||||
}
|
||||
}
|
||||
return container.KeysInt64(repoIDs)
|
||||
}
|
||||
|
||||
func (runners RunnerList) LoadRepos(ctx context.Context) error {
|
||||
repoIDs := runners.getRepoIDs()
|
||||
repos := make(map[int64]*repo_model.Repository, len(repoIDs))
|
||||
if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, runner := range runners {
|
||||
if runner.RepoID > 0 && runner.Repo == nil {
|
||||
runner.Repo = repos[runner.RepoID]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (runners RunnerList) LoadAttributes(ctx context.Context) error {
|
||||
if err := runners.LoadOwners(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runners.LoadRepos(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user