chore(model): create build, stage, step and logs table

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu 2022-09-01 11:03:50 +08:00 committed by Jason Song
parent e05f224d19
commit e07d416366
7 changed files with 66 additions and 31 deletions

View File

@ -85,7 +85,7 @@ func (Build) TableName() string {
} }
func (t *Build) HTMLURL() string { func (t *Build) HTMLURL() string {
return fmt.Sprintf("") return ""
} }
func updateRepoBuildsNumbers(ctx context.Context, repo *repo_model.Repository) error { func updateRepoBuildsNumbers(ctx context.Context, repo *repo_model.Repository) error {
@ -132,18 +132,18 @@ func InsertBuild(t *Build, workflowsStatuses map[string]map[string]BuildStatus)
return err return err
} }
var buildJobs []BuildJob var buildStages []BuildStage
for filename, workflow := range workflowsStatuses { for filename, workflow := range workflowsStatuses {
for job, status := range workflow { for job, status := range workflow {
buildJobs = append(buildJobs, BuildJob{ buildStages = append(buildStages, BuildStage{
BuildID: t.ID, BuildID: t.ID,
Filename: filename, Filename: filename,
Jobname: job, Name: job,
Status: status, Status: status,
}) })
} }
} }
if err := db.Insert(ctx, buildJobs); err != nil { if err := db.Insert(ctx, buildStages); err != nil {
return err return err
} }

View File

@ -13,11 +13,10 @@ import (
// BuildLog represents a build's log, every build has a standalone table // BuildLog represents a build's log, every build has a standalone table
type BuildLog struct { type BuildLog struct {
ID int64 ID int64
BuildJobID int64 `xorm:"index"` StepID int64 `xorm:"index"`
LineNumber int Content string `xorm:"BINARY"`
Content string `xorm:"LONGTEXT"` Created timeutil.TimeStamp `xorm:"created"`
Created timeutil.TimeStamp `xorm:"created"`
} }
func init() { func init() {
@ -37,7 +36,7 @@ func CreateBuildLog(buildID int64) error {
func GetBuildLogs(buildID, jobID int64) (logs []BuildLog, err error) { func GetBuildLogs(buildID, jobID int64) (logs []BuildLog, err error) {
err = db.GetEngine(db.DefaultContext).Table(GetBuildLogTableName(buildID)). err = db.GetEngine(db.DefaultContext).Table(GetBuildLogTableName(buildID)).
Where("build_job_id=?", jobID). Where("build_step_id=?", jobID).
Find(&logs) Find(&logs)
return return
} }

View File

@ -9,33 +9,38 @@ import (
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
) )
type BuildJob struct { type BuildStage struct {
ID int64 ID int64
BuildID int64 `xorm:"index"` BuildID int64 `xorm:"index"`
Number int64
Name string
Kind string
Type string
Filename string Filename string
Jobname string
Status BuildStatus Status BuildStatus
Started timeutil.TimeStamp
Stopped timeutil.TimeStamp
LogToFile bool // read log from database or from storage LogToFile bool // read log from database or from storage
Created timeutil.TimeStamp `xorm:"created"` Created timeutil.TimeStamp `xorm:"created"`
} }
func (bj BuildJob) TableName() string { func (bj BuildStage) TableName() string {
return "bots_build_job" return "bots_build_stage"
} }
func init() { func init() {
db.RegisterModel(new(BuildJob)) db.RegisterModel(new(BuildStage))
} }
func GetBuildWorkflows(buildID int64) (map[string]map[string]*BuildJob, error) { func GetBuildWorkflows(buildID int64) (map[string]map[string]*BuildStage, error) {
jobs := make(map[string]map[string]*BuildJob) jobs := make(map[string]map[string]*BuildStage)
err := db.GetEngine(db.DefaultContext).Iterate(new(BuildJob), func(idx int, bean interface{}) error { err := db.GetEngine(db.DefaultContext).Iterate(new(BuildStage), func(idx int, bean interface{}) error {
job := bean.(*BuildJob) job := bean.(*BuildStage)
_, ok := jobs[job.Filename] _, ok := jobs[job.Filename]
if !ok { if !ok {
jobs[job.Filename] = make(map[string]*BuildJob) jobs[job.Filename] = make(map[string]*BuildStage)
} }
jobs[job.Filename][job.Jobname] = job jobs[job.Filename][job.Name] = job
return nil return nil
}) })
return jobs, err return jobs, err

31
models/bots/build_step.go Normal file
View File

@ -0,0 +1,31 @@
// 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 (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
)
type BuildStep struct {
ID int64
StageID int64 `xorm:"index"`
Number int64
Name string
Kind string
Type string
Status BuildStatus
Started timeutil.TimeStamp
Stopped timeutil.TimeStamp
Created timeutil.TimeStamp `xorm:"created"`
}
func (bj BuildStep) TableName() string {
return "bots_build_step"
}
func init() {
db.RegisterModel(new(BuildStep))
}

View File

@ -77,8 +77,6 @@ func (runners RunnerList) LoadAttributes(ctx context.Context) error {
if err := runners.LoadOwners(ctx); err != nil { if err := runners.LoadOwners(ctx); err != nil {
return err return err
} }
if err := runners.LoadRepos(ctx); err != nil {
return err return runners.LoadRepos(ctx)
}
return nil
} }

View File

@ -10,11 +10,14 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
pingv1 "gitea.com/gitea/proto-go/ping/v1" pingv1 "gitea.com/gitea/proto-go/ping/v1"
"gitea.com/gitea/proto-go/ping/v1/pingv1connect"
"github.com/bufbuild/connect-go" "github.com/bufbuild/connect-go"
) )
type Service struct{} type Service struct {
pingv1connect.UnimplementedPingServiceHandler
}
func (s *Service) Ping( func (s *Service) Ping(
ctx context.Context, ctx context.Context,
@ -22,10 +25,9 @@ func (s *Service) Ping(
) (*connect.Response[pingv1.PingResponse], error) { ) (*connect.Response[pingv1.PingResponse], error) {
log.Info("Content-Type: %s", req.Header().Get("Content-Type")) log.Info("Content-Type: %s", req.Header().Get("Content-Type"))
log.Info("User-Agent: %s", req.Header().Get("User-Agent")) log.Info("User-Agent: %s", req.Header().Get("User-Agent"))
log.Info("X-Gitea-Token: %s", req.Header().Get("X-Gitea-Token")) log.Info("X-Runner-Token: %s", req.Header().Get("X-Runner-Token"))
res := connect.NewResponse(&pingv1.PingResponse{ res := connect.NewResponse(&pingv1.PingResponse{
Data: fmt.Sprintf("Hello, %s!", req.Msg.Data), Data: fmt.Sprintf("Hello, %s!", req.Msg.Data),
}) })
res.Header().Set("Gitea-Version", "v1")
return res, nil return res, nil
} }

View File

@ -118,14 +118,14 @@ func GetBuildJobLogs(ctx *context.Context) {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
return return
} }
var buildJob *bots_model.BuildJob var buildJob *bots_model.BuildStage
wf := ctx.Params("workflow") wf := ctx.Params("workflow")
jobname := ctx.Params("jobname") jobname := ctx.Params("jobname")
LOOP_WORKFLOWS: LOOP_WORKFLOWS:
for workflow, jobs := range workflows { for workflow, jobs := range workflows {
if workflow == wf { if workflow == wf {
for _, job := range jobs { for _, job := range jobs {
if jobname == job.Jobname { if jobname == job.Name {
buildJob = job buildJob = job
break LOOP_WORKFLOWS break LOOP_WORKFLOWS
} }