You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

task_state.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. actions_model "code.gitea.io/gitea/models/actions"
  6. )
  7. const (
  8. preStepName = "Set up job"
  9. postStepName = "Complete job"
  10. )
  11. // FullSteps returns steps with "Set up job" and "Complete job"
  12. func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
  13. if len(task.Steps) == 0 {
  14. return fullStepsOfEmptySteps(task)
  15. }
  16. firstStep := task.Steps[0]
  17. var logIndex int64
  18. preStep := &actions_model.ActionTaskStep{
  19. Name: preStepName,
  20. LogLength: task.LogLength,
  21. Started: task.Started,
  22. Status: actions_model.StatusRunning,
  23. }
  24. if firstStep.Status.HasRun() || firstStep.Status.IsRunning() {
  25. preStep.LogLength = firstStep.LogIndex
  26. preStep.Stopped = firstStep.Started
  27. preStep.Status = actions_model.StatusSuccess
  28. } else if task.Status.IsDone() {
  29. preStep.Stopped = task.Stopped
  30. preStep.Status = actions_model.StatusFailure
  31. }
  32. logIndex += preStep.LogLength
  33. var lastHasRunStep *actions_model.ActionTaskStep
  34. for _, step := range task.Steps {
  35. if step.Status.HasRun() {
  36. lastHasRunStep = step
  37. }
  38. logIndex += step.LogLength
  39. }
  40. if lastHasRunStep == nil {
  41. lastHasRunStep = preStep
  42. }
  43. postStep := &actions_model.ActionTaskStep{
  44. Name: postStepName,
  45. Status: actions_model.StatusWaiting,
  46. }
  47. if task.Status.IsDone() {
  48. postStep.LogIndex = logIndex
  49. postStep.LogLength = task.LogLength - postStep.LogIndex
  50. postStep.Status = task.Status
  51. postStep.Started = lastHasRunStep.Stopped
  52. postStep.Stopped = task.Stopped
  53. }
  54. ret := make([]*actions_model.ActionTaskStep, 0, len(task.Steps)+2)
  55. ret = append(ret, preStep)
  56. ret = append(ret, task.Steps...)
  57. ret = append(ret, postStep)
  58. return ret
  59. }
  60. func fullStepsOfEmptySteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
  61. preStep := &actions_model.ActionTaskStep{
  62. Name: preStepName,
  63. LogLength: task.LogLength,
  64. Started: task.Started,
  65. Stopped: task.Stopped,
  66. Status: actions_model.StatusRunning,
  67. }
  68. postStep := &actions_model.ActionTaskStep{
  69. Name: postStepName,
  70. LogIndex: task.LogLength,
  71. Started: task.Stopped,
  72. Stopped: task.Stopped,
  73. Status: actions_model.StatusWaiting,
  74. }
  75. if task.Status.IsDone() {
  76. preStep.Status = task.Status
  77. if preStep.Status.IsSuccess() {
  78. postStep.Status = actions_model.StatusSuccess
  79. } else {
  80. postStep.Status = actions_model.StatusCancelled
  81. }
  82. }
  83. return []*actions_model.ActionTaskStep{
  84. preStep,
  85. postStep,
  86. }
  87. }