diff --git a/models/bots/run.go b/models/bots/run.go new file mode 100644 index 0000000000..8aac6de23e --- /dev/null +++ b/models/bots/run.go @@ -0,0 +1,43 @@ +// 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/core" + "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/models/webhook" + "code.gitea.io/gitea/modules/timeutil" +) + +// Run represents a run of a workflow file +type Run struct { + ID int64 + Name string + RepoID int64 `xorm:"index unique(repo_workflow_number)"` + WorkflowID string `xorm:"index unique(repo_workflow_number)"` // the name of workflow file + Number int64 `xorm:"index unique(repo_workflow_number)"` // a unique number for each run of a particular workflow in a repository + 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"` + Status core.BuildStatus `xorm:"index"` + StartTime timeutil.TimeStamp + EndTime timeutil.TimeStamp + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` +} + +func init() { + db.RegisterModel(new(Run)) +} + +func (Run) TableName() string { + return "bots_run" +} diff --git a/models/bots/run_job.go b/models/bots/run_job.go new file mode 100644 index 0000000000..4760478ce2 --- /dev/null +++ b/models/bots/run_job.go @@ -0,0 +1,34 @@ +// 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/core" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/timeutil" +) + +// RunJob represents a job of a run +type RunJob struct { + ID int64 + RunID int64 + Name string + WorkflowPayload string `xorm:"LONGTEXT"` + Needs []int64 `xorm:"JSON TEXT"` + TaskID int64 // the latest task of the job + Status core.BuildStatus `xorm:"index"` + StartTime timeutil.TimeStamp + EndTime timeutil.TimeStamp + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` +} + +func init() { + db.RegisterModel(new(RunJob)) +} + +func (RunJob) TableName() string { + return "bots_run_job" +} diff --git a/models/bots/task.go b/models/bots/task.go new file mode 100644 index 0000000000..1fa99758bb --- /dev/null +++ b/models/bots/task.go @@ -0,0 +1,33 @@ +// 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" +) + +// Task represents a distribution of job +type Task struct { + ID int64 + JobID int64 + Attempt int64 + RunnerID int64 `xorm:"index"` + LogToFile bool // read log from database or from storage + LogUrl string // url of the log file in storage + Result int64 // TODO: use runnerv1.Result + Started timeutil.TimeStamp + Stopped timeutil.TimeStamp + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` +} + +func init() { + db.RegisterModel(new(Task)) +} + +func (Task) TableName() string { + return "bots_task" +} diff --git a/models/bots/task_log.go b/models/bots/task_log.go new file mode 100644 index 0000000000..41949a8308 --- /dev/null +++ b/models/bots/task_log.go @@ -0,0 +1,47 @@ +// 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" +) + +// TaskLog represents a task's log, every task has a standalone table +type TaskLog struct { + ID int64 + Content string `xorm:"BINARY"` + Created timeutil.TimeStamp `xorm:"created"` +} + +func init() { + db.RegisterModel(new(TaskLog)) +} + +func GetTaskLogTableName(taskID int64) string { + return fmt.Sprintf("bots_task_log_%d", taskID) +} + +// CreateTaskLog table for a build +func CreateTaskLog(buildID int64) error { + return db.GetEngine(db.DefaultContext). + Table(GetBuildLogTableName(buildID)). + Sync2(new(BuildLog)) +} + +func GetTaskLogs(taskID, index, length int64) (logs []*TaskLog, err error) { + sess := db.GetEngine(db.DefaultContext).Table(GetBuildLogTableName(taskID)). + Where("id>=?", index) + + if length > 0 { + sess.Limit(int(length)) + } + + err = sess.Find(&logs) + + return +} diff --git a/models/bots/task_step.go b/models/bots/task_step.go new file mode 100644 index 0000000000..f8319cbde0 --- /dev/null +++ b/models/bots/task_step.go @@ -0,0 +1,32 @@ +// 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" +) + +// TaskStep represents a step of Task +type TaskStep struct { + ID int64 + TaskID int64 + Number int64 + Result int64 // TODO: use runnerv1.Result + LogIndex int64 + LogLength int64 + Started timeutil.TimeStamp + Stopped timeutil.TimeStamp + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` +} + +func (TaskStep) TableName() string { + return "bots_task_step" +} + +func init() { + db.RegisterModel(new(TaskStep)) +}