mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-27 07:44:43 +02:00
chore(runner): support fetch detail stage data
Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
db02603882
commit
fb738c4192
@ -2,10 +2,13 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrDataLock = errors.New("Data Lock Error")
|
||||||
|
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
Kind string
|
Kind string
|
||||||
Type string
|
Type string
|
||||||
|
2
go.sum
2
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=
|
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 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-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 h1:Yy0Bxzc8R2wxiwXoG/rECGplJUSpXqCsog9PuJFgiHs=
|
||||||
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc=
|
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=
|
gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0=
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"code.gitea.io/gitea/core"
|
"code.gitea.io/gitea/core"
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
@ -30,7 +31,7 @@ func init() {
|
|||||||
// Build represnets bot build task
|
// Build represnets bot build task
|
||||||
type Build struct {
|
type Build struct {
|
||||||
ID int64
|
ID int64
|
||||||
Title string
|
Name string
|
||||||
UUID string `xorm:"CHAR(36)"`
|
UUID string `xorm:"CHAR(36)"`
|
||||||
Index int64 `xorm:"index unique(repo_index)"`
|
Index int64 `xorm:"index unique(repo_index)"`
|
||||||
RepoID 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 {
|
type ErrBuildNotExist struct {
|
||||||
RepoID int64
|
RepoID int64
|
||||||
Index int64
|
Index int64
|
||||||
|
ID int64
|
||||||
UUID string
|
UUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err ErrBuildNotExist) Error() 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
|
// GetBuildByUUID gets bot build by uuid
|
||||||
@ -159,6 +169,20 @@ func GetBuildByUUID(buildUUID string) (*Build, error) {
|
|||||||
return &build, nil
|
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
|
// GetCurBuildByID return the build for the bot
|
||||||
func GetCurBuildByID(runnerID int64) (*Build, error) {
|
func GetCurBuildByID(runnerID int64) (*Build, error) {
|
||||||
var builds []Build
|
var builds []Build
|
||||||
|
@ -6,6 +6,7 @@ package bots
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/gitea/core"
|
"code.gitea.io/gitea/core"
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
@ -29,7 +30,9 @@ type BuildStage struct {
|
|||||||
Started timeutil.TimeStamp
|
Started timeutil.TimeStamp
|
||||||
Stopped timeutil.TimeStamp
|
Stopped timeutil.TimeStamp
|
||||||
LogToFile bool // read log from database or from storage
|
LogToFile bool // read log from database or from storage
|
||||||
|
Version int `xorm:"version"`
|
||||||
Created timeutil.TimeStamp `xorm:"created"`
|
Created timeutil.TimeStamp `xorm:"created"`
|
||||||
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bj BuildStage) TableName() string {
|
func (bj BuildStage) TableName() string {
|
||||||
@ -69,6 +72,34 @@ func FindStages(ctx context.Context, opts FindStageOptions) (BuildStageList, err
|
|||||||
return rows, sess.Find(&rows)
|
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) {
|
func GetBuildWorkflows(buildID int64) (map[string]map[string]*BuildStage, error) {
|
||||||
jobs := make(map[string]map[string]*BuildStage)
|
jobs := make(map[string]map[string]*BuildStage)
|
||||||
err := db.GetEngine(db.DefaultContext).Iterate(new(BuildStage), func(idx int, bean interface{}) error {
|
err := db.GetEngine(db.DefaultContext).Iterate(new(BuildStage), func(idx int, bean interface{}) error {
|
||||||
|
@ -94,7 +94,7 @@ func notify(repo *repo_model.Repository, doer *user_model.User, payload, ref str
|
|||||||
}
|
}
|
||||||
|
|
||||||
build := bots_model.Build{
|
build := bots_model.Build{
|
||||||
Title: commit.Message(),
|
Name: commit.Message(),
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
TriggerUserID: doer.ID,
|
TriggerUserID: doer.ID,
|
||||||
Event: evt,
|
Event: evt,
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"code.gitea.io/gitea/core"
|
"code.gitea.io/gitea/core"
|
||||||
bots_model "code.gitea.io/gitea/models/bots"
|
bots_model "code.gitea.io/gitea/models/bots"
|
||||||
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
||||||
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
|
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
|
||||||
@ -90,6 +91,66 @@ func (s *Service) Request(
|
|||||||
return res, nil
|
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.
|
// Update updates the build stage.
|
||||||
func (s *Service) Update(
|
func (s *Service) Update(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
@ -92,7 +92,7 @@ func ViewBuild(ctx *context.Context) {
|
|||||||
return
|
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["PageIsBuildList"] = true
|
||||||
ctx.Data["Build"] = build
|
ctx.Data["Build"] = build
|
||||||
statuses, err := bots_model.GetBuildWorkflows(build.ID)
|
statuses, err := bots_model.GetBuildWorkflows(build.ID)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user