refactor: rename tables to bot_*

This commit is contained in:
Jason Song 2022-11-29 12:34:23 +08:00
parent 3ac6bf3db4
commit 04d72d3500
No known key found for this signature in database
GPG Key ID: 8402EEEE4511A8B5
20 changed files with 173 additions and 201 deletions

View File

@ -22,8 +22,8 @@ import (
"xorm.io/builder"
)
// Run represents a run of a workflow file
type Run struct {
// BotRun represents a run of a workflow file
type BotRun struct {
ID int64
Title string
RepoID int64 `xorm:"index unique(repo_index)"`
@ -46,20 +46,16 @@ type Run struct {
}
func init() {
db.RegisterModel(new(Run))
db.RegisterModel(new(RunIndex))
db.RegisterModel(new(BotRun))
db.RegisterModel(new(BotRunIndex))
}
func (Run) TableName() string {
return "bots_run"
}
func (run *Run) HTMLURL() string {
func (run *BotRun) HTMLURL() string {
return fmt.Sprintf("%s/bots/runs/%d", run.Repo.HTMLURL(), run.Index)
}
// LoadAttributes load Repo TriggerUser if not loaded
func (run *Run) LoadAttributes(ctx context.Context) error {
func (run *BotRun) LoadAttributes(ctx context.Context) error {
if run == nil {
return nil
}
@ -86,7 +82,7 @@ func (run *Run) LoadAttributes(ctx context.Context) error {
return nil
}
func (run *Run) TakeTime() time.Duration {
func (run *BotRun) TakeTime() time.Duration {
if run.Started == 0 {
return 0
}
@ -98,7 +94,7 @@ func (run *Run) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (run *Run) GetPushEventPayload() (*api.PushPayload, error) {
func (run *BotRun) GetPushEventPayload() (*api.PushPayload, error) {
if run.Event == webhook.HookEventPush {
var payload api.PushPayload
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
@ -134,7 +130,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
}
// InsertRun inserts a bot run
func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
func InsertRun(run *BotRun, jobs []*jobparser.SingleWorkflow) error {
ctx, commiter, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
@ -167,7 +163,7 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
return err
}
runJobs := make([]*RunJob, 0, len(jobs))
runJobs := make([]*BotRunJob, 0, len(jobs))
for _, v := range jobs {
id, job := v.Job()
needs := job.Needs()
@ -177,7 +173,7 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
if len(needs) > 0 {
status = StatusBlocked
}
runJobs = append(runJobs, &RunJob{
runJobs = append(runJobs, &BotRunJob{
RunID: run.ID,
RepoID: run.RepoID,
OwnerID: run.OwnerID,
@ -212,8 +208,8 @@ func (err ErrRunNotExist) Error() string {
return fmt.Sprintf("run [%d] is not exist", err.ID)
}
func GetRunByID(ctx context.Context, id int64) (*Run, error) {
var run Run
func GetRunByID(ctx context.Context, id int64) (*BotRun, error) {
var run BotRun
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run)
if err != nil {
return nil, err
@ -226,8 +222,8 @@ func GetRunByID(ctx context.Context, id int64) (*Run, error) {
return &run, nil
}
func GetRunByIndex(ctx context.Context, repoID, index int64) (*Run, error) {
run := &Run{
func GetRunByIndex(ctx context.Context, repoID, index int64) (*BotRun, error) {
run := &BotRun{
RepoID: repoID,
Index: index,
}
@ -244,7 +240,7 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*Run, error) {
return run, nil
}
func UpdateRun(ctx context.Context, run *Run, cols ...string) error {
func UpdateRun(ctx context.Context, run *BotRun, cols ...string) error {
sess := db.GetEngine(ctx).ID(run.ID)
if len(cols) > 0 {
sess.Cols(cols...)
@ -273,8 +269,4 @@ func UpdateRun(ctx context.Context, run *Run, cols ...string) error {
return err
}
type RunIndex db.ResourceIndex
func (RunIndex) TableName() string {
return "bots_run_index"
}
type BotRunIndex db.ResourceIndex

View File

@ -16,11 +16,11 @@ import (
"xorm.io/builder"
)
// RunJob represents a job of a run
type RunJob struct {
// BotRunJob represents a job of a run
type BotRunJob struct {
ID int64
RunID int64 `xorm:"index"`
Run *Run `xorm:"-"`
Run *BotRun `xorm:"-"`
RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"`
@ -40,14 +40,10 @@ type RunJob struct {
}
func init() {
db.RegisterModel(new(RunJob))
db.RegisterModel(new(BotRunJob))
}
func (RunJob) TableName() string {
return "bots_run_job"
}
func (job *RunJob) TakeTime() time.Duration {
func (job *BotRunJob) TakeTime() time.Duration {
if job.Started == 0 {
return 0
}
@ -59,7 +55,7 @@ func (job *RunJob) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (job *RunJob) LoadRun(ctx context.Context) error {
func (job *BotRunJob) LoadRun(ctx context.Context) error {
if job.Run == nil {
run, err := GetRunByID(ctx, job.RunID)
if err != nil {
@ -71,7 +67,7 @@ func (job *RunJob) LoadRun(ctx context.Context) error {
}
// LoadAttributes load Run if not loaded
func (job *RunJob) LoadAttributes(ctx context.Context) error {
func (job *BotRunJob) LoadAttributes(ctx context.Context) error {
if job == nil {
return nil
}
@ -92,8 +88,8 @@ func (err ErrRunJobNotExist) Error() string {
return fmt.Sprintf("run job [%d] is not exist", err.ID)
}
func GetRunJobByID(ctx context.Context, id int64) (*RunJob, error) {
var job RunJob
func GetRunJobByID(ctx context.Context, id int64) (*BotRunJob, error) {
var job BotRunJob
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
if err != nil {
return nil, err
@ -106,15 +102,15 @@ func GetRunJobByID(ctx context.Context, id int64) (*RunJob, error) {
return &job, nil
}
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*RunJob, error) {
var jobs []*RunJob
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*BotRunJob, error) {
var jobs []*BotRunJob
if err := db.GetEngine(ctx).Where("run_id=?", runID).OrderBy("id").Find(&jobs); err != nil {
return nil, err
}
return jobs, nil
}
func UpdateRunJob(ctx context.Context, job *RunJob, cond builder.Cond, cols ...string) (int64, error) {
func UpdateRunJob(ctx context.Context, job *BotRunJob, cond builder.Cond, cols ...string) (int64, error) {
e := db.GetEngine(ctx)
sess := e.ID(job.ID)
@ -149,7 +145,7 @@ func UpdateRunJob(ctx context.Context, job *RunJob, cond builder.Cond, cols ...s
runStatus := aggregateJobStatus(jobs)
run := &Run{
run := &BotRun{
ID: job.RunID,
Status: runStatus,
}
@ -159,7 +155,7 @@ func UpdateRunJob(ctx context.Context, job *RunJob, cond builder.Cond, cols ...s
return affected, UpdateRun(ctx, run)
}
func aggregateJobStatus(jobs []*RunJob) Status {
func aggregateJobStatus(jobs []*BotRunJob) Status {
allDone := true
allWaiting := true
hasFailure := false

View File

@ -13,7 +13,7 @@ import (
"xorm.io/builder"
)
type RunJobList []*RunJob
type RunJobList []*BotRunJob
func (jobs RunJobList) GetRunIDs() []int64 {
runIDsMap := make(map[int64]struct{})
@ -32,7 +32,7 @@ func (jobs RunJobList) GetRunIDs() []int64 {
func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
runIDs := jobs.GetRunIDs()
runs := make(map[int64]*Run, len(runIDs))
runs := make(map[int64]*BotRun, len(runIDs))
if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
return err
}
@ -42,7 +42,7 @@ func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
}
}
if withRepo {
var runsList RunList = make([]*Run, 0, len(runs))
var runsList RunList = make([]*BotRun, 0, len(runs))
for _, r := range runs {
runsList = append(runsList, r)
}
@ -99,5 +99,5 @@ func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (RunJobList, int64
}
func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(RunJob))
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotRunJob))
}

View File

@ -15,7 +15,7 @@ import (
"xorm.io/builder"
)
type RunList []*Run
type RunList []*BotRun
// GetUserIDs returns a slice of user's id
func (runs RunList) GetUserIDs() []int64 {
@ -111,5 +111,5 @@ func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error)
}
func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Run))
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotRun))
}

View File

@ -34,8 +34,8 @@ func (err ErrRunnerNotExist) Error() string {
return fmt.Sprintf("Bot runner token [%s] is not exist", err.Token)
}
// Runner represents runner machines
type Runner struct {
// BotRunner represents runner machines
type BotRunner struct {
ID int64
UUID string `xorm:"CHAR(36) UNIQUE"`
Name string `xorm:"VARCHAR(32)"`
@ -65,11 +65,7 @@ type Runner struct {
Deleted timeutil.TimeStamp `xorm:"deleted"`
}
func (Runner) TableName() string {
return "bots_runner"
}
func (r *Runner) OwnType() string {
func (r *BotRunner) OwnType() string {
if r.OwnerID == 0 {
return "Global"
}
@ -80,7 +76,7 @@ func (r *Runner) OwnType() string {
return r.Repo.FullName()
}
func (r *Runner) Status() runnerv1.RunnerStatus {
func (r *BotRunner) Status() runnerv1.RunnerStatus {
if time.Since(r.LastOnline.AsTime()) > time.Minute {
return runnerv1.RunnerStatus_RUNNER_STATUS_OFFLINE
}
@ -90,11 +86,11 @@ func (r *Runner) Status() runnerv1.RunnerStatus {
return runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE
}
func (r *Runner) StatusName() string {
func (r *BotRunner) StatusName() string {
return strings.ToLower(strings.TrimPrefix(r.Status().String(), "RUNNER_STATUS_"))
}
func (r *Runner) IsOnline() bool {
func (r *BotRunner) IsOnline() bool {
status := r.Status()
if status == runnerv1.RunnerStatus_RUNNER_STATUS_IDLE || status == runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE {
return true
@ -103,12 +99,12 @@ func (r *Runner) IsOnline() bool {
}
// AllLabels returns agent and custom labels
func (r *Runner) AllLabels() []string {
func (r *BotRunner) AllLabels() []string {
return append(r.AgentLabels, r.CustomLabels...)
}
// Editable checks if the runner is editable by the user
func (r *Runner) Editable(ownerID, repoID int64) bool {
func (r *BotRunner) Editable(ownerID, repoID int64) bool {
if ownerID == 0 && repoID == 0 {
return true
}
@ -119,7 +115,7 @@ func (r *Runner) Editable(ownerID, repoID int64) bool {
}
// LoadAttributes loads the attributes of the runner
func (r *Runner) LoadAttributes(ctx context.Context) error {
func (r *BotRunner) LoadAttributes(ctx context.Context) error {
if r.OwnerID > 0 {
var user user_model.User
has, err := db.GetEngine(ctx).ID(r.OwnerID).Get(&user)
@ -143,13 +139,13 @@ func (r *Runner) LoadAttributes(ctx context.Context) error {
return nil
}
func (r *Runner) GenerateToken() (err error) {
func (r *BotRunner) GenerateToken() (err error) {
r.Token, r.TokenSalt, r.TokenHash, _, err = generateSaltedToken()
return err
}
func init() {
db.RegisterModel(&Runner{})
db.RegisterModel(&BotRunner{})
}
type FindRunnerOptions struct {
@ -217,8 +213,8 @@ func FindRunners(opts FindRunnerOptions) (runners RunnerList, err error) {
}
// GetUsableRunner returns the usable runner
func GetUsableRunner(opts FindRunnerOptions) (*Runner, error) {
var runner Runner
func GetUsableRunner(opts FindRunnerOptions) (*BotRunner, error) {
var runner BotRunner
has, err := db.GetEngine(db.DefaultContext).
Where(opts.toCond()).
Asc("last_online").
@ -234,8 +230,8 @@ func GetUsableRunner(opts FindRunnerOptions) (*Runner, error) {
}
// GetRunnerByUUID returns a bot runner via uuid
func GetRunnerByUUID(uuid string) (*Runner, error) {
var runner Runner
func GetRunnerByUUID(uuid string) (*BotRunner, error) {
var runner BotRunner
has, err := db.GetEngine(db.DefaultContext).Where("uuid=?", uuid).Get(&runner)
if err != nil {
return nil, err
@ -248,8 +244,8 @@ func GetRunnerByUUID(uuid string) (*Runner, error) {
}
// GetRunnerByID returns a bot runner via id
func GetRunnerByID(id int64) (*Runner, error) {
var runner Runner
func GetRunnerByID(id int64) (*BotRunner, error) {
var runner BotRunner
has, err := db.GetEngine(db.DefaultContext).Where("id=?", id).Get(&runner)
if err != nil {
return nil, err
@ -262,7 +258,7 @@ func GetRunnerByID(id int64) (*Runner, error) {
}
// UpdateRunner updates runner's information.
func UpdateRunner(ctx context.Context, r *Runner, cols ...string) error {
func UpdateRunner(ctx context.Context, r *BotRunner, cols ...string) error {
e := db.GetEngine(ctx)
var err error
if len(cols) == 0 {
@ -274,15 +270,15 @@ func UpdateRunner(ctx context.Context, r *Runner, cols ...string) error {
}
// DeleteRunner deletes a runner by given ID.
func DeleteRunner(ctx context.Context, r *Runner) error {
func DeleteRunner(ctx context.Context, r *BotRunner) error {
e := db.GetEngine(ctx)
_, err := e.Delete(r)
return err
}
// FindRunnersByRepoID returns all workers for the repository
func FindRunnersByRepoID(repoID int64) ([]*Runner, error) {
var runners []*Runner
func FindRunnersByRepoID(repoID int64) ([]*BotRunner, error) {
var runners []*BotRunner
err := db.GetEngine(db.DefaultContext).Where("repo_id=? OR repo_id=0", repoID).
Find(&runners)
if err != nil {
@ -293,7 +289,7 @@ func FindRunnersByRepoID(repoID int64) ([]*Runner, error) {
}
// NewRunner creates new runner.
func NewRunner(ctx context.Context, t *Runner) error {
func NewRunner(ctx context.Context, t *BotRunner) error {
_, err := db.GetEngine(ctx).Insert(t)
return err
}

View File

@ -13,7 +13,7 @@ import (
"code.gitea.io/gitea/modules/container"
)
type RunnerList []*Runner
type RunnerList []*BotRunner
// GetUserIDs returns a slice of user's id
func (runners RunnerList) GetUserIDs() []int64 {

View File

@ -24,8 +24,8 @@ func (err ErrRunnerTokenNotExist) Error() string {
return fmt.Sprintf("runner token [%s] is not exist", err.Token)
}
// RunnerToken represents runner tokens
type RunnerToken struct {
// BotRunnerToken represents runner tokens
type BotRunnerToken struct {
ID int64
Token string `xorm:"UNIQUE"`
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
@ -39,17 +39,13 @@ type RunnerToken struct {
Deleted timeutil.TimeStamp `xorm:"deleted"`
}
func (RunnerToken) TableName() string {
return "bots_runner_token"
}
func init() {
db.RegisterModel(new(RunnerToken))
db.RegisterModel(new(BotRunnerToken))
}
// GetRunnerToken returns a bot runner via token
func GetRunnerToken(token string) (*RunnerToken, error) {
var runnerToken RunnerToken
func GetRunnerToken(token string) (*BotRunnerToken, error) {
var runnerToken BotRunnerToken
has, err := db.GetEngine(db.DefaultContext).Where("token=?", token).Get(&runnerToken)
if err != nil {
return nil, err
@ -62,7 +58,7 @@ func GetRunnerToken(token string) (*RunnerToken, error) {
}
// UpdateRunnerToken updates runner token information.
func UpdateRunnerToken(ctx context.Context, r *RunnerToken, cols ...string) (err error) {
func UpdateRunnerToken(ctx context.Context, r *BotRunnerToken, cols ...string) (err error) {
e := db.GetEngine(ctx)
if len(cols) == 0 {
@ -74,12 +70,12 @@ func UpdateRunnerToken(ctx context.Context, r *RunnerToken, cols ...string) (err
}
// NewRunnerToken creates a new runner token
func NewRunnerToken(ownerID, repoID int64) (*RunnerToken, error) {
func NewRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
token, err := util.CryptoRandomString(40)
if err != nil {
return nil, err
}
runnerToken := &RunnerToken{
runnerToken := &BotRunnerToken{
OwnerID: ownerID,
RepoID: repoID,
IsActive: false,
@ -90,8 +86,8 @@ func NewRunnerToken(ownerID, repoID int64) (*RunnerToken, error) {
}
// GetUnactivatedRunnerToken returns a unactivated runner token
func GetUnactivatedRunnerToken(ownerID, repoID int64) (*RunnerToken, error) {
var runnerToken RunnerToken
func GetUnactivatedRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
var runnerToken BotRunnerToken
has, err := db.GetEngine(db.DefaultContext).Where("owner_id=? AND repo_id=? AND is_active=?", ownerID, repoID, false).OrderBy("id DESC").Get(&runnerToken)
if err != nil {
return nil, err

View File

@ -6,7 +6,7 @@ package bots
import runnerv1 "code.gitea.io/bots-proto-go/runner/v1"
// Status represents the status of Run, RunJob, Task, or TaskStep
// Status represents the status of BotRun, BotRunJob, BotTask, or BotTaskStep
type Status int
const (

View File

@ -29,12 +29,12 @@ import (
"xorm.io/builder"
)
// Task represents a distribution of job
type Task struct {
// BotTask represents a distribution of job
type BotTask struct {
ID int64
JobID int64
Job *RunJob `xorm:"-"`
Steps []*TaskStep `xorm:"-"`
Job *BotRunJob `xorm:"-"`
Steps []*BotTaskStep `xorm:"-"`
Attempt int64
RunnerID int64 `xorm:"index"`
Status Status `xorm:"index"`
@ -65,7 +65,7 @@ type Task struct {
var successfulTokenTaskCache *lru.Cache
func init() {
db.RegisterModel(new(Task), func() error {
db.RegisterModel(new(BotTask), func() error {
if setting.SuccessfulTokensCacheSize > 0 {
var err error
successfulTokenTaskCache, err = lru.New(setting.SuccessfulTokensCacheSize)
@ -79,11 +79,7 @@ func init() {
})
}
func (Task) TableName() string {
return "bots_task"
}
func (task *Task) TakeTime() time.Duration {
func (task *BotTask) TakeTime() time.Duration {
if task.Started == 0 {
return 0
}
@ -95,15 +91,15 @@ func (task *Task) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (task *Task) IsStopped() bool {
func (task *BotTask) IsStopped() bool {
return task.Stopped > 0
}
func (task *Task) GetRepo() string {
func (task *BotTask) GetRepo() string {
return "xxxx"
}
func (task *Task) GetCommitSHA() string {
func (task *BotTask) GetCommitSHA() string {
if task.Job == nil {
return ""
}
@ -114,7 +110,7 @@ func (task *Task) GetCommitSHA() string {
return task.Job.Run.CommitSHA
}
func (task *Task) GetCommitSHAShort() string {
func (task *BotTask) GetCommitSHAShort() string {
commitSHA := task.GetCommitSHA()
if len(commitSHA) > 8 {
return commitSHA[:8]
@ -122,14 +118,14 @@ func (task *Task) GetCommitSHAShort() string {
return commitSHA
}
func (task *Task) GetBuildViewLink() string {
func (task *BotTask) GetBuildViewLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.Link() + "/bots/runs/" + strconv.FormatInt(task.ID, 10)
}
func (task *Task) GetCommitLink() string {
func (task *BotTask) GetCommitLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
@ -139,21 +135,21 @@ func (task *Task) GetCommitLink() string {
return ""
}
func (task *Task) GetRepoName() string {
func (task *BotTask) GetRepoName() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.FullName()
}
func (task *Task) GetRepoLink() string {
func (task *BotTask) GetRepoLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.Link()
}
func (task *Task) LoadJob(ctx context.Context) error {
func (task *BotTask) LoadJob(ctx context.Context) error {
if task.Job == nil {
job, err := GetRunJobByID(ctx, task.JobID)
if err != nil {
@ -165,7 +161,7 @@ func (task *Task) LoadJob(ctx context.Context) error {
}
// LoadAttributes load Job Steps if not loaded
func (task *Task) LoadAttributes(ctx context.Context) error {
func (task *BotTask) LoadAttributes(ctx context.Context) error {
if task == nil {
return nil
}
@ -188,7 +184,7 @@ func (task *Task) LoadAttributes(ctx context.Context) error {
return nil
}
func (task *Task) GenerateToken() (err error) {
func (task *BotTask) GenerateToken() (err error) {
task.Token, task.TokenSalt, task.TokenHash, task.TokenLastEight, err = generateSaltedToken()
return err
}
@ -218,8 +214,8 @@ func (indexes *LogIndexes) ToDB() ([]byte, error) {
return buf[:i], nil
}
func GetTaskByID(ctx context.Context, id int64) (*Task, error) {
var task Task
func GetTaskByID(ctx context.Context, id int64) (*BotTask, error) {
var task BotTask
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&task)
if err != nil {
return nil, err
@ -230,7 +226,7 @@ func GetTaskByID(ctx context.Context, id int64) (*Task, error) {
return &task, nil
}
func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
func GetRunningTaskByToken(ctx context.Context, token string) (*BotTask, error) {
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
if token == "" {
return nil, errNotExist
@ -248,7 +244,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
lastEight := token[len(token)-8:]
if id := getTaskIDFromCache(token); id > 0 {
task := &Task{
task := &BotTask{
TokenLastEight: lastEight,
}
// Re-get the task from the db in case it has been deleted in the intervening period
@ -262,7 +258,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
successfulTokenTaskCache.Remove(token)
}
var tasks []*Task
var tasks []*BotTask
err := db.GetEngine(ctx).Where("token_last_eight = ? AND status = ?", lastEight, StatusRunning).Find(&tasks)
if err != nil {
return nil, err
@ -282,7 +278,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
return nil, errNotExist
}
func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, error) {
func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool, error) {
dbCtx, commiter, err := db.TxContext(ctx)
if err != nil {
return nil, false, err
@ -299,16 +295,16 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
jobCond = builder.In("repo_id", builder.Select("id").From("repository").Where(builder.Eq{"owner_id": runner.OwnerID}))
}
if jobCond.IsValid() {
jobCond = builder.In("run_id", builder.Select("id").From(Run{}.TableName()).Where(jobCond))
jobCond = builder.In("run_id", builder.Select("id").From("bot_run").Where(jobCond))
}
var jobs []*RunJob
var jobs []*BotRunJob
if err := e.Where("task_id=? AND status=?", 0, StatusWaiting).And(jobCond).Asc("id").Find(&jobs); err != nil {
return nil, false, err
}
// TODO: a more efficient way to filter labels
var job *RunJob
var job *BotRunJob
labels := runner.AgentLabels
labels = append(labels, runner.CustomLabels...)
log.Trace("runner labels: %v", labels)
@ -330,7 +326,7 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
job.Started = now
job.Status = StatusRunning
task := &Task{
task := &BotTask{
JobID: job.ID,
Attempt: job.Attempt,
RunnerID: runner.ID,
@ -363,9 +359,9 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
return nil, false, err
}
steps := make([]*TaskStep, len(workflowJob.Steps))
steps := make([]*BotTaskStep, len(workflowJob.Steps))
for i, v := range workflowJob.Steps {
steps[i] = &TaskStep{
steps[i] = &BotTaskStep{
Name: v.String(),
TaskID: task.ID,
Number: int64(i),
@ -401,7 +397,7 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
return task, true, nil
}
func UpdateTask(ctx context.Context, task *Task, cols ...string) error {
func UpdateTask(ctx context.Context, task *BotTask, cols ...string) error {
sess := db.GetEngine(ctx).ID(task.ID)
if len(cols) > 0 {
sess.Cols(cols...)
@ -410,7 +406,7 @@ func UpdateTask(ctx context.Context, task *Task, cols ...string) error {
return err
}
func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
func UpdateTaskByState(state *runnerv1.TaskState) (*BotTask, error) {
stepStates := map[int64]*runnerv1.StepState{}
for _, v := range state.Steps {
stepStates[v.Id] = v
@ -424,7 +420,7 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
e := db.GetEngine(ctx)
task := &Task{}
task := &BotTask{}
if has, err := e.ID(state.Id).Get(task); err != nil {
return nil, err
} else if !has {
@ -434,7 +430,7 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
if state.Result != runnerv1.Result_RESULT_UNSPECIFIED {
task.Status = Status(state.Result)
task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix())
if _, err := UpdateRunJob(ctx, &RunJob{
if _, err := UpdateRunJob(ctx, &BotRunJob{
ID: task.JobID,
Status: task.Status,
Stopped: task.Stopped,
@ -486,7 +482,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
}
e := db.GetEngine(ctx)
task := &Task{}
task := &BotTask{}
if has, err := e.ID(taskID).Get(task); err != nil {
return err
} else if !has {
@ -499,7 +495,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
now := timeutil.TimeStampNow()
task.Status = status
task.Stopped = now
if _, err := UpdateRunJob(ctx, &RunJob{
if _, err := UpdateRunJob(ctx, &BotRunJob{
ID: task.JobID,
Status: task.Status,
Stopped: task.Stopped,

View File

@ -13,7 +13,7 @@ import (
"xorm.io/builder"
)
type TaskList []*Task
type TaskList []*BotTask
func (tasks TaskList) GetJobIDs() []int64 {
jobIDsMap := make(map[int64]struct{})
@ -32,7 +32,7 @@ func (tasks TaskList) GetJobIDs() []int64 {
func (tasks TaskList) LoadJobs(ctx context.Context) error {
jobIDs := tasks.GetJobIDs()
jobs := make(map[int64]*RunJob, len(jobIDs))
jobs := make(map[int64]*BotRunJob, len(jobIDs))
if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
return err
}
@ -42,7 +42,7 @@ func (tasks TaskList) LoadJobs(ctx context.Context) error {
}
}
var jobsList RunJobList = make([]*RunJob, 0, len(jobs))
var jobsList RunJobList = make([]*BotRunJob, 0, len(jobs))
for _, j := range jobs {
jobsList = append(jobsList, j)
}
@ -105,5 +105,5 @@ func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, int64, erro
}
func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Task))
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotTask))
}

View File

@ -12,8 +12,8 @@ import (
"code.gitea.io/gitea/modules/timeutil"
)
// TaskStep represents a step of Task
type TaskStep struct {
// BotTaskStep represents a step of BotTask
type BotTaskStep struct {
ID int64
Name string
TaskID int64 `xorm:"index unique(task_number)"`
@ -27,11 +27,7 @@ type TaskStep struct {
Updated timeutil.TimeStamp `xorm:"updated"`
}
func (TaskStep) TableName() string {
return "bots_task_step"
}
func (step *TaskStep) TakeTime() time.Duration {
func (step *BotTaskStep) TakeTime() time.Duration {
if step.Started == 0 {
return 0
}
@ -44,10 +40,10 @@ func (step *TaskStep) TakeTime() time.Duration {
}
func init() {
db.RegisterModel(new(TaskStep))
db.RegisterModel(new(BotTaskStep))
}
func GetTaskStepsByTaskID(ctx context.Context, taskID int64) ([]*TaskStep, error) {
var steps []*TaskStep
func GetTaskStepsByTaskID(ctx context.Context, taskID int64) ([]*BotTaskStep, error) {
var steps []*BotTaskStep
return steps, db.GetEngine(ctx).Where("task_id=?", taskID).OrderBy("number").Find(&steps)
}

View File

@ -12,7 +12,7 @@ import (
)
func addBotTables(x *xorm.Engine) error {
type BotsRunner struct {
type BotRunner struct {
ID int64
UUID string `xorm:"CHAR(36) UNIQUE"`
Name string `xorm:"VARCHAR(32)"`
@ -40,7 +40,7 @@ func addBotTables(x *xorm.Engine) error {
Deleted timeutil.TimeStamp `xorm:"deleted"`
}
type BotsRunnerToken struct {
type BotRunnerToken struct {
ID int64
Token string `xorm:"UNIQUE"`
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
@ -52,7 +52,7 @@ func addBotTables(x *xorm.Engine) error {
Deleted timeutil.TimeStamp `xorm:"deleted"`
}
type BotsRun struct {
type BotRun struct {
ID int64
Title string
RepoID int64 `xorm:"index unique(repo_index)"`
@ -72,7 +72,7 @@ func addBotTables(x *xorm.Engine) error {
Updated timeutil.TimeStamp `xorm:"updated"`
}
type BotsRunJob struct {
type BotRunJob struct {
ID int64
RunID int64 `xorm:"index"`
RepoID int64 `xorm:"index"`
@ -98,9 +98,9 @@ func addBotTables(x *xorm.Engine) error {
NumClosedRuns int `xorm:"NOT NULL DEFAULT 0"`
}
type BotsRunIndex db.ResourceIndex
type BotRunIndex db.ResourceIndex
type BotsTask struct {
type BotTask struct {
ID int64
JobID int64
Attempt int64
@ -129,7 +129,7 @@ func addBotTables(x *xorm.Engine) error {
Updated timeutil.TimeStamp `xorm:"updated index"`
}
type BotsTaskStep struct {
type BotTaskStep struct {
ID int64
Name string
TaskID int64 `xorm:"index unique(task_number)"`
@ -143,14 +143,14 @@ func addBotTables(x *xorm.Engine) error {
Updated timeutil.TimeStamp `xorm:"updated"`
}
return x.Sync2(
new(BotsRunner),
new(BotsRunnerToken),
new(BotsRun),
new(BotsRunJob),
return x.Sync(
new(BotRunner),
new(BotRunnerToken),
new(BotRun),
new(BotRunJob),
new(Repository),
new(BotsRunIndex),
new(BotsTask),
new(BotsTaskStep),
new(BotRunIndex),
new(BotTask),
new(BotTaskStep),
)
}

View File

@ -14,7 +14,7 @@ const (
)
// FullSteps returns steps with "Set up job" and "Complete job"
func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
func FullSteps(task *bots_model.BotTask) []*bots_model.BotTaskStep {
if len(task.Steps) == 0 {
return fullStepsOfEmptySteps(task)
}
@ -22,7 +22,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
firstStep := task.Steps[0]
var logIndex int64
preStep := &bots_model.TaskStep{
preStep := &bots_model.BotTaskStep{
Name: preStepName,
LogLength: task.LogLength,
Started: task.Started,
@ -39,7 +39,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
}
logIndex += preStep.LogLength
var lastHasRunStep *bots_model.TaskStep
var lastHasRunStep *bots_model.BotTaskStep
for _, step := range task.Steps {
if step.Status.HasRun() {
lastHasRunStep = step
@ -50,7 +50,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
lastHasRunStep = preStep
}
postStep := &bots_model.TaskStep{
postStep := &bots_model.BotTaskStep{
Name: postStepName,
Status: bots_model.StatusWaiting,
}
@ -61,7 +61,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
postStep.Started = lastHasRunStep.Stopped
postStep.Stopped = task.Stopped
}
ret := make([]*bots_model.TaskStep, 0, len(task.Steps)+2)
ret := make([]*bots_model.BotTaskStep, 0, len(task.Steps)+2)
ret = append(ret, preStep)
ret = append(ret, task.Steps...)
ret = append(ret, postStep)
@ -69,8 +69,8 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
return ret
}
func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
preStep := &bots_model.TaskStep{
func fullStepsOfEmptySteps(task *bots_model.BotTask) []*bots_model.BotTaskStep {
preStep := &bots_model.BotTaskStep{
Name: preStepName,
LogLength: task.LogLength,
Started: task.Started,
@ -78,7 +78,7 @@ func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
Status: bots_model.StatusRunning,
}
postStep := &bots_model.TaskStep{
postStep := &bots_model.BotTaskStep{
Name: postStepName,
LogIndex: task.LogLength,
Started: task.Stopped,
@ -95,7 +95,7 @@ func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
}
}
return []*bots_model.TaskStep{
return []*bots_model.BotTaskStep{
preStep,
postStep,
}

View File

@ -15,13 +15,13 @@ import (
func TestFullSteps(t *testing.T) {
tests := []struct {
name string
task *bots_model.Task
want []*bots_model.TaskStep
task *bots_model.BotTask
want []*bots_model.BotTaskStep
}{
{
name: "regular",
task: &bots_model.Task{
Steps: []*bots_model.TaskStep{
task: &bots_model.BotTask{
Steps: []*bots_model.BotTaskStep{
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
},
Status: bots_model.StatusSuccess,
@ -29,7 +29,7 @@ func TestFullSteps(t *testing.T) {
Stopped: 10100,
LogLength: 100,
},
want: []*bots_model.TaskStep{
want: []*bots_model.BotTaskStep{
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
{Name: postStepName, Status: bots_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
@ -37,8 +37,8 @@ func TestFullSteps(t *testing.T) {
},
{
name: "failed step",
task: &bots_model.Task{
Steps: []*bots_model.TaskStep{
task: &bots_model.BotTask{
Steps: []*bots_model.BotTaskStep{
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
{Status: bots_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
@ -48,7 +48,7 @@ func TestFullSteps(t *testing.T) {
Stopped: 10100,
LogLength: 100,
},
want: []*bots_model.TaskStep{
want: []*bots_model.BotTaskStep{
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
{Status: bots_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
@ -58,8 +58,8 @@ func TestFullSteps(t *testing.T) {
},
{
name: "first step is running",
task: &bots_model.Task{
Steps: []*bots_model.TaskStep{
task: &bots_model.BotTask{
Steps: []*bots_model.BotTaskStep{
{Status: bots_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
},
Status: bots_model.StatusRunning,
@ -67,7 +67,7 @@ func TestFullSteps(t *testing.T) {
Stopped: 10100,
LogLength: 100,
},
want: []*bots_model.TaskStep{
want: []*bots_model.BotTaskStep{
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
{Status: bots_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
{Name: postStepName, Status: bots_model.StatusWaiting, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
@ -75,8 +75,8 @@ func TestFullSteps(t *testing.T) {
},
{
name: "first step has canceled",
task: &bots_model.Task{
Steps: []*bots_model.TaskStep{
task: &bots_model.BotTask{
Steps: []*bots_model.BotTaskStep{
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
},
Status: bots_model.StatusFailure,
@ -84,7 +84,7 @@ func TestFullSteps(t *testing.T) {
Stopped: 10100,
LogLength: 100,
},
want: []*bots_model.TaskStep{
want: []*bots_model.BotTaskStep{
{Name: preStepName, Status: bots_model.StatusFailure, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
{Name: postStepName, Status: bots_model.StatusFailure, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
@ -92,14 +92,14 @@ func TestFullSteps(t *testing.T) {
},
{
name: "empty steps",
task: &bots_model.Task{
Steps: []*bots_model.TaskStep{},
task: &bots_model.BotTask{
Steps: []*bots_model.BotTaskStep{},
Status: bots_model.StatusSuccess,
Started: 10000,
Stopped: 10100,
LogLength: 100,
},
want: []*bots_model.TaskStep{
want: []*bots_model.BotTaskStep{
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
{Name: postStepName, Status: bots_model.StatusSuccess, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
},

View File

@ -86,7 +86,7 @@ func notifyWithPR(repo *repo_model.Repository, doer *user_model.User, ref string
}
for id, content := range workflows {
run := bots_model.Run{
run := bots_model.BotRun{
Title: commit.Message(),
RepoID: repo.ID,
OwnerID: repo.OwnerID,

View File

@ -56,7 +56,7 @@ func (s *Service) Register(
}
// create new runner
runner := &bots_model.Runner{
runner := &bots_model.BotRunner{
UUID: gouuid.New().String(),
Name: req.Msg.Name,
OwnerID: runnerToken.OwnerID,
@ -276,7 +276,7 @@ func (s *Service) UpdateLog(
return res, nil
}
func pickTask(ctx context.Context, runner *bots_model.Runner) (*runnerv1.Task, bool, error) {
func pickTask(ctx context.Context, runner *bots_model.BotRunner) (*runnerv1.Task, bool, error) {
t, ok, err := bots_model.CreateTaskForRunner(ctx, runner)
if err != nil {
return nil, false, fmt.Errorf("CreateTaskForRunner: %w", err)
@ -294,7 +294,7 @@ func pickTask(ctx context.Context, runner *bots_model.Runner) (*runnerv1.Task, b
return task, true, nil
}
func getSecretsOfTask(ctx context.Context, task *bots_model.Task) map[string]string {
func getSecretsOfTask(ctx context.Context, task *bots_model.BotTask) map[string]string {
// Returning an error is worse than returning empty secrets.
secrets := map[string]string{}
@ -334,7 +334,7 @@ func getSecretsOfTask(ctx context.Context, task *bots_model.Task) map[string]str
return secrets
}
func generateTaskContext(t *bots_model.Task) *structpb.Struct {
func generateTaskContext(t *bots_model.BotTask) *structpb.Struct {
event := map[string]interface{}{}
_ = json.Unmarshal([]byte(t.Job.Run.EventPayload), &event)

View File

@ -68,9 +68,9 @@ func getMethodName(req connect.AnyRequest) string {
type runnerCtxKey struct{}
func GetRunner(ctx context.Context) *bots_model.Runner {
func GetRunner(ctx context.Context) *bots_model.BotRunner {
if v := ctx.Value(runnerCtxKey{}); v != nil {
if r, ok := v.(*bots_model.Runner); ok {
if r, ok := v.(*bots_model.BotRunner); ok {
return r
}
}

View File

@ -37,7 +37,7 @@ func RunnersList(ctx *context.Context, tplName base.TplName, opts bots_model.Fin
}
// ownid=0,repo_id=0,means this token is used for global
var token *bots_model.RunnerToken
var token *bots_model.BotRunnerToken
token, err = bots_model.GetUnactivatedRunnerToken(opts.OwnerID, opts.RepoID)
if _, ok := err.(bots_model.ErrRunnerTokenNotExist); ok {
token, err = bots_model.NewRunnerToken(opts.OwnerID, opts.RepoID)

View File

@ -125,7 +125,7 @@ func ViewPost(ctx *context_module.Context) {
},
}
var task *bots_model.Task
var task *bots_model.BotTask
if current.TaskID > 0 {
var err error
task, err = bots_model.GetTaskByID(ctx, current.TaskID)
@ -263,7 +263,7 @@ func Cancel(ctx *context_module.Context) {
// getRunJobs gets the jobs of runIndex, and returns jobs[jobIndex], jobs.
// Any error will be written to the ctx.
// It never returns a nil job of an empty jobs, if the jobIndex is out of range, it will be treated as 0.
func getRunJobs(ctx *context_module.Context, runIndex, jobIndex int64) (*bots_model.RunJob, []*bots_model.RunJob) {
func getRunJobs(ctx *context_module.Context, runIndex, jobIndex int64) (*bots_model.BotRunJob, []*bots_model.BotRunJob) {
run, err := bots_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
if err != nil {
if _, ok := err.(bots_model.ErrRunNotExist); ok {

View File

@ -51,7 +51,7 @@ func checkJobsOfRun(ctx context.Context, runID int64) error {
if err != nil {
return err
}
idToJobs := make(map[string][]*bots_model.RunJob, len(jobs))
idToJobs := make(map[string][]*bots_model.BotRunJob, len(jobs))
for _, job := range jobs {
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
}
@ -77,7 +77,7 @@ type jobStatusResolver struct {
}
func newJobStatusResolver(jobs bots_model.RunJobList) *jobStatusResolver {
idToJobs := make(map[string][]*bots_model.RunJob, len(jobs))
idToJobs := make(map[string][]*bots_model.BotRunJob, len(jobs))
for _, job := range jobs {
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
}