1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-04-18 00:47:48 -04:00
gitea/modules/bots/task_state.go
2022-11-25 17:48:47 +08:00

103 lines
2.4 KiB
Go

// 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 (
bots_model "code.gitea.io/gitea/models/bots"
)
const (
preStepName = "Set up job"
postStepName = "Complete job"
)
// FullSteps returns steps with "Set up job" and "Complete job"
func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
if len(task.Steps) == 0 {
return fullStepsOfEmptySteps(task)
}
firstStep := task.Steps[0]
var logIndex int64
preStep := &bots_model.TaskStep{
Name: preStepName,
LogLength: task.LogLength,
Started: task.Started,
Status: bots_model.StatusRunning,
}
if firstStep.Status.HasRun() || firstStep.Status.IsRunning() {
preStep.LogLength = firstStep.LogIndex
preStep.Stopped = firstStep.Started
preStep.Status = bots_model.StatusSuccess
} else if task.Status.IsDone() {
preStep.Stopped = task.Stopped
preStep.Status = bots_model.StatusFailure
}
logIndex += preStep.LogLength
var lastHasRunStep *bots_model.TaskStep
for _, step := range task.Steps {
if step.Status.HasRun() {
lastHasRunStep = step
}
logIndex += step.LogLength
}
if lastHasRunStep == nil {
lastHasRunStep = preStep
}
postStep := &bots_model.TaskStep{
Name: postStepName,
Status: bots_model.StatusWaiting,
}
if task.Status.IsDone() {
postStep.LogIndex = logIndex
postStep.LogLength = task.LogLength - postStep.LogIndex
postStep.Status = task.Status
postStep.Started = lastHasRunStep.Stopped
postStep.Stopped = task.Stopped
}
ret := make([]*bots_model.TaskStep, 0, len(task.Steps)+2)
ret = append(ret, preStep)
ret = append(ret, task.Steps...)
ret = append(ret, postStep)
return ret
}
func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
preStep := &bots_model.TaskStep{
Name: preStepName,
LogLength: task.LogLength,
Started: task.Started,
Stopped: task.Stopped,
Status: bots_model.StatusRunning,
}
postStep := &bots_model.TaskStep{
Name: postStepName,
LogIndex: task.LogLength,
Started: task.Stopped,
Stopped: task.Stopped,
Status: bots_model.StatusWaiting,
}
if task.Status.IsDone() {
preStep.Status = task.Status
if preStep.Status.IsSuccess() {
postStep.Status = bots_model.StatusSuccess
} else {
postStep.Status = bots_model.StatusCancelled
}
}
return []*bots_model.TaskStep{
preStep,
postStep,
}
}