gitea源码

task_state.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 is the first step that has run or running, not include preStep.
  17. // For example,
  18. // 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): firstStep is step1.
  19. // 2. preStep(Success) -> step1(Skipped) -> step2(Success) -> postStep(Success): firstStep is step2.
  20. // 3. preStep(Success) -> step1(Running) -> step2(Waiting) -> postStep(Waiting): firstStep is step1.
  21. // 4. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil.
  22. // 5. preStep(Success) -> step1(Cancelled) -> step2(Cancelled) -> postStep(Cancelled): firstStep is nil.
  23. var firstStep *actions_model.ActionTaskStep
  24. // lastHasRunStep is the last step that has run.
  25. // For example,
  26. // 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): lastHasRunStep is step1.
  27. // 2. preStep(Success) -> step1(Success) -> step2(Success) -> step3(Success) -> postStep(Success): lastHasRunStep is step3.
  28. // 3. preStep(Success) -> step1(Success) -> step2(Failure) -> step3 -> postStep(Waiting): lastHasRunStep is step2.
  29. // So its Stopped is the Started of postStep when there are no more steps to run.
  30. var lastHasRunStep *actions_model.ActionTaskStep
  31. var logIndex int64
  32. for _, step := range task.Steps {
  33. if firstStep == nil && (step.Status.HasRun() || step.Status.IsRunning()) {
  34. firstStep = step
  35. }
  36. if step.Status.HasRun() {
  37. lastHasRunStep = step
  38. }
  39. logIndex += step.LogLength
  40. }
  41. preStep := &actions_model.ActionTaskStep{
  42. Name: preStepName,
  43. LogLength: task.LogLength,
  44. Started: task.Started,
  45. Status: actions_model.StatusRunning,
  46. }
  47. // No step has run or is running, so preStep is equal to the task
  48. if firstStep == nil {
  49. preStep.Stopped = task.Stopped
  50. preStep.Status = task.Status
  51. } else {
  52. preStep.LogLength = firstStep.LogIndex
  53. preStep.Stopped = firstStep.Started
  54. preStep.Status = actions_model.StatusSuccess
  55. }
  56. logIndex += preStep.LogLength
  57. if lastHasRunStep == nil {
  58. lastHasRunStep = preStep
  59. }
  60. postStep := &actions_model.ActionTaskStep{
  61. Name: postStepName,
  62. Status: actions_model.StatusWaiting,
  63. }
  64. // If the lastHasRunStep is the last step, or it has failed, postStep has started.
  65. if lastHasRunStep.Status.IsFailure() || lastHasRunStep == task.Steps[len(task.Steps)-1] {
  66. postStep.LogIndex = logIndex
  67. postStep.LogLength = task.LogLength - postStep.LogIndex
  68. postStep.Started = lastHasRunStep.Stopped
  69. postStep.Status = actions_model.StatusRunning
  70. }
  71. if task.Status.IsDone() {
  72. postStep.Status = task.Status
  73. postStep.Stopped = task.Stopped
  74. }
  75. ret := make([]*actions_model.ActionTaskStep, 0, len(task.Steps)+2)
  76. ret = append(ret, preStep)
  77. ret = append(ret, task.Steps...)
  78. ret = append(ret, postStep)
  79. return ret
  80. }
  81. func fullStepsOfEmptySteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
  82. preStep := &actions_model.ActionTaskStep{
  83. Name: preStepName,
  84. LogLength: task.LogLength,
  85. Started: task.Started,
  86. Stopped: task.Stopped,
  87. Status: actions_model.StatusRunning,
  88. }
  89. postStep := &actions_model.ActionTaskStep{
  90. Name: postStepName,
  91. LogIndex: task.LogLength,
  92. Started: task.Stopped,
  93. Stopped: task.Stopped,
  94. Status: actions_model.StatusWaiting,
  95. }
  96. if task.Status.IsDone() {
  97. preStep.Status = task.Status
  98. if preStep.Status.IsSuccess() {
  99. postStep.Status = actions_model.StatusSuccess
  100. } else {
  101. postStep.Status = actions_model.StatusCancelled
  102. }
  103. }
  104. return []*actions_model.ActionTaskStep{
  105. preStep,
  106. postStep,
  107. }
  108. }