From fb738c41920392038ff3188d7bb589520ed465f6 Mon Sep 17 00:00:00 2001 From: "Bo-Yi.Wu" Date: Sun, 4 Sep 2022 15:18:32 +0800 Subject: [PATCH] chore(runner): support fetch detail stage data Signed-off-by: Bo-Yi.Wu --- core/scheduler.go | 3 ++ go.sum | 2 + models/bots/build.go | 28 +++++++++++++- models/bots/build_stage.go | 31 ++++++++++++++++ modules/notification/bots/bots.go | 2 +- routers/api/bots/runner/runner.go | 61 +++++++++++++++++++++++++++++++ routers/web/repo/builds/builds.go | 2 +- 7 files changed, 125 insertions(+), 4 deletions(-) diff --git a/core/scheduler.go b/core/scheduler.go index 167d2b3b62..afbceecc5f 100644 --- a/core/scheduler.go +++ b/core/scheduler.go @@ -2,10 +2,13 @@ package core import ( "context" + "errors" runnerv1 "gitea.com/gitea/proto-go/runner/v1" ) +var ErrDataLock = errors.New("Data Lock Error") + type Filter struct { Kind string Type string diff --git a/go.sum b/go.sum index a5721c4dcd..3693d28d72 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,8 @@ git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:cliQ4H git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078/go.mod h1:g/V2Hjas6Z1UHUp4yIx6bATpNzJ7DYtD0FG3+xARWxs= gitea.com/gitea/proto-go v0.0.0-20220901135226-82a982903134 h1:5ofH0FGEkIj/P9a6oFDgkdmGSWow1yD1uubiftMA2Kw= gitea.com/gitea/proto-go v0.0.0-20220901135226-82a982903134/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= +gitea.com/gitea/proto-go v0.0.0-20220903092234-20f71c2df67e h1:xlNITjAs+Ce6QR4mo0BvHTzTdkpqgS7zwfLpEi31mIM= +gitea.com/gitea/proto-go v0.0.0-20220903092234-20f71c2df67e/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb h1:Yy0Bxzc8R2wxiwXoG/rECGplJUSpXqCsog9PuJFgiHs= gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc= gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0= diff --git a/models/bots/build.go b/models/bots/build.go index 1333fa586f..9899d88199 100644 --- a/models/bots/build.go +++ b/models/bots/build.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "strconv" "code.gitea.io/gitea/core" "code.gitea.io/gitea/models/db" @@ -30,7 +31,7 @@ func init() { // Build represnets bot build task type Build struct { ID int64 - Title string + Name string UUID string `xorm:"CHAR(36)"` Index int64 `xorm:"index unique(repo_index)"` RepoID int64 `xorm:"index unique(repo_index)"` @@ -138,11 +139,20 @@ func UpdateBuild(t *Build, cols ...string) error { type ErrBuildNotExist struct { RepoID int64 Index int64 + ID int64 UUID string } func (err ErrBuildNotExist) Error() string { - return fmt.Sprintf("Bot build [%s] is not exist", err.UUID) + uuid := "" + if err.UUID != "" { + uuid = err.UUID + } + + if err.ID != 0 { + uuid = strconv.FormatInt(err.ID, 10) + } + return fmt.Sprintf("build [%s] is not exist", uuid) } // GetBuildByUUID gets bot build by uuid @@ -159,6 +169,20 @@ func GetBuildByUUID(buildUUID string) (*Build, error) { return &build, nil } +func GetBuildByID(id int64) (*Build, error) { + var build Build + has, err := db.GetEngine(db.DefaultContext).Where("id=?", id).Get(&build) + if err != nil { + return nil, err + } else if !has { + return nil, ErrBuildNotExist{ + ID: id, + } + } + + return &build, nil +} + // GetCurBuildByID return the build for the bot func GetCurBuildByID(runnerID int64) (*Build, error) { var builds []Build diff --git a/models/bots/build_stage.go b/models/bots/build_stage.go index c6c6e12b10..d0c5827436 100644 --- a/models/bots/build_stage.go +++ b/models/bots/build_stage.go @@ -6,6 +6,7 @@ package bots import ( "context" + "fmt" "code.gitea.io/gitea/core" "code.gitea.io/gitea/models/db" @@ -29,7 +30,9 @@ type BuildStage struct { Started timeutil.TimeStamp Stopped timeutil.TimeStamp LogToFile bool // read log from database or from storage + Version int `xorm:"version"` Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` } func (bj BuildStage) TableName() string { @@ -69,6 +72,34 @@ func FindStages(ctx context.Context, opts FindStageOptions) (BuildStageList, err return rows, sess.Find(&rows) } +// GetStageByID gets build stage by id +func GetStageByID(id int64) (*BuildStage, error) { + var build BuildStage + has, err := db.GetEngine(db.DefaultContext).Where("id=?", id).Get(&build) + if err != nil { + return nil, err + } else if !has { + return nil, ErrBuildStageNotExist{ + ID: id, + } + } + return &build, nil +} + +// ErrBuildNotExist represents an error for bot build not exist +type ErrBuildStageNotExist struct { + ID int64 +} + +func (err ErrBuildStageNotExist) Error() string { + return fmt.Sprintf("build stage [%d] is not exist", err.ID) +} + +// UpdateBuildStage updates build stage +func UpdateBuildStage(t *BuildStage, cols ...string) (int64, error) { + return db.GetEngine(db.DefaultContext).ID(t.ID).Cols(cols...).Update(t) +} + func GetBuildWorkflows(buildID int64) (map[string]map[string]*BuildStage, error) { jobs := make(map[string]map[string]*BuildStage) err := db.GetEngine(db.DefaultContext).Iterate(new(BuildStage), func(idx int, bean interface{}) error { diff --git a/modules/notification/bots/bots.go b/modules/notification/bots/bots.go index 7ceab1e70a..6c25dbd481 100644 --- a/modules/notification/bots/bots.go +++ b/modules/notification/bots/bots.go @@ -94,7 +94,7 @@ func notify(repo *repo_model.Repository, doer *user_model.User, payload, ref str } build := bots_model.Build{ - Title: commit.Message(), + Name: commit.Message(), RepoID: repo.ID, TriggerUserID: doer.ID, Event: evt, diff --git a/routers/api/bots/runner/runner.go b/routers/api/bots/runner/runner.go index c1f8083519..abb7caa949 100644 --- a/routers/api/bots/runner/runner.go +++ b/routers/api/bots/runner/runner.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/core" bots_model "code.gitea.io/gitea/models/bots" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/log" runnerv1 "gitea.com/gitea/proto-go/runner/v1" "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" @@ -90,6 +91,66 @@ func (s *Service) Request( return res, nil } +// Details fetches build details +func (s *Service) Detail( + ctx context.Context, + req *connect.Request[runnerv1.DetailRequest], +) (*connect.Response[runnerv1.DetailResponse], error) { + log.Info("stag id %d", req.Msg.Stage.Id) + + // fetch stage data + stage, err := bots_model.GetStageByID(req.Msg.Stage.Id) + if err != nil { + return nil, err + } + + stage.Machine = req.Msg.Stage.Machine + stage.Status = core.StatusPending + + count, err := bots_model.UpdateBuildStage(stage, "machine", "status") + if err != nil { + return nil, err + } + if count != 1 { + return nil, core.ErrDataLock + } + + // fetch build data + build, err := bots_model.GetBuildByID(stage.BuildID) + if err != nil { + return nil, err + } + + // fetch repo data + repo, err := repo_model.GetRepositoryByID(build.RepoID) + if err != nil { + return nil, err + } + + res := connect.NewResponse(&runnerv1.DetailResponse{ + Stage: &runnerv1.Stage{ + Id: stage.ID, + BuildId: stage.BuildID, + Name: stage.Name, + Kind: stage.Kind, + Type: stage.Type, + Status: string(stage.Status), + Started: int64(stage.Started), + Stopped: int64(stage.Stopped), + Machine: stage.Machine, + }, + Build: &runnerv1.Build{ + Id: build.ID, + Name: build.Name, + }, + Repo: &runnerv1.Repo{ + Id: repo.ID, + Name: repo.Name, + }, + }) + return res, nil +} + // Update updates the build stage. func (s *Service) Update( ctx context.Context, diff --git a/routers/web/repo/builds/builds.go b/routers/web/repo/builds/builds.go index 47c5ef82c3..7058661dba 100644 --- a/routers/web/repo/builds/builds.go +++ b/routers/web/repo/builds/builds.go @@ -92,7 +92,7 @@ func ViewBuild(ctx *context.Context) { return } - ctx.Data["Title"] = build.Title + " - " + ctx.Tr("repo.builds") + ctx.Data["Name"] = build.Name + " - " + ctx.Tr("repo.builds") ctx.Data["PageIsBuildList"] = true ctx.Data["Build"] = build statuses, err := bots_model.GetBuildWorkflows(build.ID)