delete unused structs
This commit is contained in:
@@ -1,284 +0,0 @@
|
||||
// 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"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/gitea/core"
|
||||
"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/models/webhook"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(Build))
|
||||
db.RegisterModel(new(BuildIndex))
|
||||
}
|
||||
|
||||
// Build represnets bot build task
|
||||
type Build struct {
|
||||
ID int64
|
||||
Name string
|
||||
UUID string `xorm:"CHAR(36)"`
|
||||
Index int64 `xorm:"index unique(repo_index)"`
|
||||
RepoID int64 `xorm:"index unique(repo_index)"`
|
||||
TriggerUserID int64
|
||||
TriggerUser *user_model.User `xorm:"-"`
|
||||
Ref string
|
||||
CommitSHA string
|
||||
Event webhook.HookEventType
|
||||
Token string // token for this task
|
||||
Grant string // permissions for this task
|
||||
EventPayload string `xorm:"LONGTEXT"`
|
||||
RunnerID int64 `xorm:"index"`
|
||||
Status core.BuildStatus `xorm:"index"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
StartTime timeutil.TimeStamp
|
||||
EndTime timeutil.TimeStamp
|
||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||
}
|
||||
|
||||
// TableName represents a bot build
|
||||
func (Build) TableName() string {
|
||||
return "bots_build"
|
||||
}
|
||||
|
||||
func (t *Build) HTMLURL() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func updateRepoBuildsNumbers(ctx context.Context, repo *repo_model.Repository) error {
|
||||
_, err := db.GetEngine(ctx).ID(repo.ID).
|
||||
SetExpr("num_builds",
|
||||
builder.Select("count(*)").From("bots_build").
|
||||
Where(builder.Eq{"repo_id": repo.ID}),
|
||||
).
|
||||
SetExpr("num_closed_builds",
|
||||
builder.Select("count(*)").From("bots_build").
|
||||
Where(builder.Eq{
|
||||
"repo_id": repo.ID,
|
||||
}.And(
|
||||
builder.In("status", core.StatusFailing, core.StatusKilled, core.StatusPassing),
|
||||
),
|
||||
),
|
||||
).
|
||||
Update(repo)
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertBuild inserts a bot build task
|
||||
func InsertBuild(t *Build, workflowsStatuses map[string]map[string]core.BuildStatus) error {
|
||||
if t.UUID == "" {
|
||||
t.UUID = uuid.New().String()
|
||||
}
|
||||
index, err := db.GetNextResourceIndex("bots_build_index", t.RepoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Index = index
|
||||
|
||||
ctx, commiter, err := db.TxContext()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer commiter.Close()
|
||||
|
||||
if err := db.Insert(ctx, t); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := updateRepoBuildsNumbers(ctx, &repo_model.Repository{ID: t.RepoID}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buildStages []BuildStage
|
||||
for filename, workflow := range workflowsStatuses {
|
||||
for job, status := range workflow {
|
||||
buildStages = append(buildStages, BuildStage{
|
||||
BuildID: t.ID,
|
||||
Filename: filename,
|
||||
Name: job,
|
||||
Status: status,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := db.Insert(ctx, buildStages); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := commiter.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := CreateBuildLog(t.ID); err != nil {
|
||||
log.Error("create build log for %d table failed, will try it again when received logs", t.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateBuild updates bot build
|
||||
func UpdateBuild(t *Build, cols ...string) error {
|
||||
_, err := db.GetEngine(db.DefaultContext).ID(t.ID).Cols(cols...).Update(t)
|
||||
return err
|
||||
}
|
||||
|
||||
// ErrBuildNotExist represents an error for bot build not exist
|
||||
type ErrBuildNotExist struct {
|
||||
RepoID int64
|
||||
Index int64
|
||||
ID int64
|
||||
UUID string
|
||||
}
|
||||
|
||||
func (err ErrBuildNotExist) Error() string {
|
||||
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
|
||||
func GetBuildByUUID(buildUUID string) (*Build, error) {
|
||||
var build Build
|
||||
has, err := db.GetEngine(db.DefaultContext).Where("uuid=?", buildUUID).Get(&build)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrBuildNotExist{
|
||||
UUID: buildUUID,
|
||||
}
|
||||
}
|
||||
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
|
||||
err := db.GetEngine(db.DefaultContext).
|
||||
Where("runner_id=?", runnerID).
|
||||
And("status=?", core.StatusPending).
|
||||
Asc("created").
|
||||
Find(&builds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(builds) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return &builds[0], err
|
||||
}
|
||||
|
||||
// GetCurBuildByUUID return the task for the bot
|
||||
func GetCurBuildByUUID(runnerUUID string) (*Build, error) {
|
||||
runner, err := GetRunnerByUUID(runnerUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetCurBuildByID(runner.ID)
|
||||
}
|
||||
|
||||
func GetBuildByRepoAndIndex(repoID, index int64) (*Build, error) {
|
||||
var build Build
|
||||
has, err := db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).
|
||||
And("`index` = ?", index).
|
||||
Get(&build)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrBuildNotExist{
|
||||
RepoID: repoID,
|
||||
Index: index,
|
||||
}
|
||||
}
|
||||
return &build, nil
|
||||
}
|
||||
|
||||
// AssignBuildToRunner assign a build to a runner
|
||||
func AssignBuildToRunner(buildID, runnerID int64) error {
|
||||
cnt, err := db.GetEngine(db.DefaultContext).
|
||||
Where("runner_id=0").
|
||||
And("id=?", buildID).
|
||||
Cols("runner_id").
|
||||
Update(&Build{
|
||||
RunnerID: runnerID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cnt != 1 {
|
||||
return errors.New("assign faild")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type FindBuildOptions struct {
|
||||
db.ListOptions
|
||||
RepoID int64
|
||||
IsClosed util.OptionalBool
|
||||
}
|
||||
|
||||
func (opts FindBuildOptions) toConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.RepoID > 0 {
|
||||
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
||||
}
|
||||
if opts.IsClosed.IsTrue() {
|
||||
cond = cond.And(builder.Expr("status IN (?,?,?,?)", core.StatusError, core.StatusFailing, core.StatusPassing))
|
||||
} else if opts.IsClosed.IsFalse() {
|
||||
cond = cond.And(builder.Expr("status IN (?,?,?)", core.StatusWaiting, core.StatusPending, core.StatusRunning))
|
||||
}
|
||||
return cond
|
||||
}
|
||||
|
||||
func FindBuilds(opts FindBuildOptions) (BuildList, error) {
|
||||
sess := db.GetEngine(db.DefaultContext).Where(opts.toConds())
|
||||
if opts.ListOptions.PageSize > 0 {
|
||||
skip, take := opts.GetSkipTake()
|
||||
sess.Limit(take, skip)
|
||||
}
|
||||
var builds []*Build
|
||||
return builds, sess.Find(&builds)
|
||||
}
|
||||
|
||||
func CountBuilds(opts FindBuildOptions) (int64, error) {
|
||||
return db.GetEngine(db.DefaultContext).Table("bots_build").Where(opts.toConds()).Count()
|
||||
}
|
||||
|
||||
type BuildIndex db.ResourceIndex
|
||||
|
||||
// TableName represents a bot build index
|
||||
func (BuildIndex) TableName() string {
|
||||
return "bots_build_index"
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
// BuildLog represents a build's log, every build has a standalone table
|
||||
type BuildLog struct {
|
||||
ID int64
|
||||
StepID int64 `xorm:"index"`
|
||||
Content string `xorm:"BINARY"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(BuildLog))
|
||||
}
|
||||
|
||||
func GetBuildLogTableName(buildID int64) string {
|
||||
return fmt.Sprintf("bots_build_log_%d", buildID)
|
||||
}
|
||||
|
||||
// CreateBuildLog table for a build
|
||||
func CreateBuildLog(buildID int64) error {
|
||||
return db.GetEngine(db.DefaultContext).
|
||||
Table(GetBuildLogTableName(buildID)).
|
||||
Sync2(new(BuildLog))
|
||||
}
|
||||
|
||||
func GetBuildLogs(buildID, jobID int64) (logs []BuildLog, err error) {
|
||||
err = db.GetEngine(db.DefaultContext).Table(GetBuildLogTableName(buildID)).
|
||||
Where("build_step_id=?", jobID).
|
||||
Find(&logs)
|
||||
return
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
// 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"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/core"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
type BuildStage struct {
|
||||
ID int64
|
||||
BuildID int64 `xorm:"index"`
|
||||
Number int64
|
||||
Name string
|
||||
Kind string
|
||||
Type string
|
||||
Machine string
|
||||
OS string
|
||||
Arch string
|
||||
Filename string
|
||||
Status core.BuildStatus
|
||||
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 {
|
||||
return "bots_build_stage"
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(BuildStage))
|
||||
}
|
||||
|
||||
type FindStageOptions struct {
|
||||
db.ListOptions
|
||||
BuildID int64
|
||||
IsClosed util.OptionalBool
|
||||
}
|
||||
|
||||
func (opts FindStageOptions) toConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.BuildID > 0 {
|
||||
cond = cond.And(builder.Eq{"build_id": opts.BuildID})
|
||||
}
|
||||
if opts.IsClosed.IsTrue() {
|
||||
cond = cond.And(builder.Expr("status IN (?,?,?,?)", core.StatusError, core.StatusFailing, core.StatusPassing, core.StatusKilled))
|
||||
} else if opts.IsClosed.IsFalse() {
|
||||
cond = cond.And(builder.Expr("status IN (?,?,?)", core.StatusPending, core.StatusRunning))
|
||||
}
|
||||
return cond
|
||||
}
|
||||
|
||||
func FindStages(ctx context.Context, opts FindStageOptions) (BuildStageList, error) {
|
||||
sess := db.GetEngine(ctx).Where(opts.toConds())
|
||||
if opts.ListOptions.PageSize > 0 {
|
||||
skip, take := opts.GetSkipTake()
|
||||
sess.Limit(take, skip)
|
||||
}
|
||||
var rows []*BuildStage
|
||||
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 {
|
||||
job := bean.(*BuildStage)
|
||||
_, ok := jobs[job.Filename]
|
||||
if !ok {
|
||||
jobs[job.Filename] = make(map[string]*BuildStage)
|
||||
}
|
||||
jobs[job.Filename][job.Name] = job
|
||||
return nil
|
||||
})
|
||||
return jobs, err
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
// 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
|
||||
|
||||
type BuildStageList []*BuildStage
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"xorm.io/builder"
|
||||
|
||||
"github.com/nektos/act/pkg/jobparser"
|
||||
)
|
||||
@@ -79,6 +80,30 @@ func (r *Run) LoadAttributes(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) error {
|
||||
_, err := db.GetEngine(ctx).ID(repo.ID).
|
||||
SetExpr("num_runs",
|
||||
builder.Select("count(*)").From("bots_run").
|
||||
Where(builder.Eq{"repo_id": repo.ID}),
|
||||
).
|
||||
SetExpr("num_closed_runs",
|
||||
builder.Select("count(*)").From("bots_run").
|
||||
Where(builder.Eq{
|
||||
"repo_id": repo.ID,
|
||||
}.And(
|
||||
builder.In("status",
|
||||
core.StatusFailing,
|
||||
core.StatusKilled,
|
||||
core.StatusPassing,
|
||||
core.StatusError,
|
||||
),
|
||||
),
|
||||
),
|
||||
).
|
||||
Update(repo)
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertRun inserts a bot run
|
||||
func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
|
||||
var groupID int64
|
||||
@@ -105,6 +130,18 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if run.Repo == nil {
|
||||
repo, err := repo_model.GetRepositoryByIDCtx(ctx, run.RepoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
run.Repo = repo
|
||||
}
|
||||
|
||||
if err := updateRepoRunsNumbers(ctx, run.Repo); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
runJobs := make([]*RunJob, 0, len(jobs))
|
||||
for _, v := range jobs {
|
||||
id, job := v.Job()
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
// RunJob represents a job of a run
|
||||
type RunJob struct {
|
||||
ID int64
|
||||
RunID int64
|
||||
Run *Run `xorm:"-"`
|
||||
RunID int64 `xorm:"index"`
|
||||
Run *Run `xorm:"-"`
|
||||
Name string
|
||||
Ready bool // ready to be executed
|
||||
Attempt int64
|
||||
|
||||
@@ -5,14 +5,18 @@
|
||||
package bots
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
type BuildList []*Build
|
||||
type RunList []*Run
|
||||
|
||||
// GetUserIDs returns a slice of user's id
|
||||
func (builds BuildList) GetUserIDs() []int64 {
|
||||
func (builds RunList) GetUserIDs() []int64 {
|
||||
userIDsMap := make(map[int64]struct{})
|
||||
for _, build := range builds {
|
||||
userIDsMap[build.TriggerUserID] = struct{}{}
|
||||
@@ -24,7 +28,7 @@ func (builds BuildList) GetUserIDs() []int64 {
|
||||
return userIDs
|
||||
}
|
||||
|
||||
func (builds BuildList) LoadTriggerUser() error {
|
||||
func (builds RunList) LoadTriggerUser() error {
|
||||
userIDs := builds.GetUserIDs()
|
||||
users := make(map[int64]*user_model.User, len(userIDs))
|
||||
if err := db.GetEngine(db.DefaultContext).In("id", userIDs).Find(&users); err != nil {
|
||||
@@ -35,3 +39,32 @@ func (builds BuildList) LoadTriggerUser() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type FindRunOptions struct {
|
||||
db.ListOptions
|
||||
RepoID int64
|
||||
IsClosed util.OptionalBool
|
||||
}
|
||||
|
||||
func (opts FindRunOptions) toConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.RepoID > 0 {
|
||||
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
||||
}
|
||||
if opts.IsClosed.IsFalse() {
|
||||
|
||||
} else if opts.IsClosed.IsTrue() {
|
||||
|
||||
}
|
||||
return cond
|
||||
}
|
||||
|
||||
func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) {
|
||||
e := db.GetEngine(ctx).Where(opts.toConds())
|
||||
if opts.PageSize>0&&opts.Page >=1 {
|
||||
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||
}
|
||||
var runs RunList
|
||||
total, err := e.FindAndCount(&runs)
|
||||
return runs, total, err
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/core"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
||||
|
||||
@@ -141,6 +142,7 @@ func CreateTaskForRunner(runner *Runner) (*Task, bool, error) {
|
||||
// TODO: a more efficient way to filter labels
|
||||
var job *RunJob
|
||||
labels := append(runner.AgentLabels, runner.CustomLabels...)
|
||||
log.Trace("runner labels: %v", labels)
|
||||
for _, v := range jobs {
|
||||
if isSubset(labels, v.RunsOn) {
|
||||
job = v
|
||||
|
||||
@@ -19,10 +19,6 @@ type TaskLog struct {
|
||||
Content string `xorm:"LONGTEXT"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(TaskLog))
|
||||
}
|
||||
|
||||
func GetTaskLogTableName(taskID int64) string {
|
||||
return fmt.Sprintf("bots_task_log_%d", taskID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user