1
1
mirror of https://github.com/go-gitea/gitea synced 2025-10-28 01:48:25 +00:00

chore(runner): support register

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu
2022-08-28 13:30:30 +08:00
committed by Jason Song
parent c10414a018
commit e05f224d19
4 changed files with 78 additions and 17 deletions

View File

@@ -5,6 +5,7 @@
package bots
import (
"context"
"fmt"
"code.gitea.io/gitea/models/db"
@@ -17,11 +18,16 @@ import (
// ErrRunnerNotExist represents an error for bot runner not exist
type ErrRunnerNotExist struct {
UUID string
UUID string
Token string
}
func (err ErrRunnerNotExist) Error() string {
return fmt.Sprintf("Bot runner [%s] is not exist", err.UUID)
if err.UUID != "" {
return fmt.Sprintf("Bot runner ID [%s] is not exist", err.UUID)
}
return fmt.Sprintf("Bot runner token [%s] is not exist", err.Token)
}
// Runner represents runner machines
@@ -40,6 +46,7 @@ type Runner struct {
Base int // 0 native 1 docker 2 virtual machine
RepoRange string // glob match which repositories could use this runner
Token string
Capacity int64
LastOnline timeutil.TimeStamp `xorm:"index"`
Created timeutil.TimeStamp `xorm:"created"`
}
@@ -128,6 +135,32 @@ func GetRunnerByUUID(uuid string) (*Runner, error) {
return &runner, nil
}
// GetRunnerByToken returns a bot runner via token
func GetRunnerByToken(token string) (*Runner, error) {
var runner Runner
has, err := db.GetEngine(db.DefaultContext).Where("token=?", token).Get(&runner)
if err != nil {
return nil, err
} else if !has {
return nil, ErrRunnerNotExist{
UUID: "",
}
}
return &runner, nil
}
// UpdateRunner updates runner's information.
func UpdateRunner(ctx context.Context, r *Runner, cols ...string) (err error) {
e := db.GetEngine(ctx)
if len(cols) == 0 {
_, err = e.ID(r.ID).AllCols().Update(r)
} else {
_, err = e.ID(r.ID).Cols(cols...).Update(r)
}
return err
}
// FindRunnersByRepoID returns all workers for the repository
func FindRunnersByRepoID(repoID int64) ([]*Runner, error) {
var runners []*Runner