From 928f35c219439ec65aa941abb32744a4299e0afc Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 24 Sep 2024 02:08:45 +0000 Subject: [PATCH 1/4] fix --- modules/actions/task_state.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/actions/task_state.go b/modules/actions/task_state.go index 31a74be3fd360..6fdc9edad7166 100644 --- a/modules/actions/task_state.go +++ b/modules/actions/task_state.go @@ -35,6 +35,10 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { } else if task.Status.IsDone() { preStep.Stopped = task.Stopped preStep.Status = actions_model.StatusFailure + // preStep(Success) -> step1(Skipped) -> step2(Success) + if firstStep.Status.IsSkipped() { + preStep.Status = actions_model.StatusSuccess + } if task.Status.IsSkipped() { preStep.Status = actions_model.StatusSkipped } From db2ea0d479a0d696feec21dc030a660704fbb6f3 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 24 Sep 2024 04:49:30 +0000 Subject: [PATCH 2/4] rewrite the logic --- modules/actions/task_state.go | 29 +++++++++++++++++------------ modules/actions/task_state_test.go | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/modules/actions/task_state.go b/modules/actions/task_state.go index 6fdc9edad7166..834d73b6752c0 100644 --- a/modules/actions/task_state.go +++ b/modules/actions/task_state.go @@ -18,8 +18,20 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { return fullStepsOfEmptySteps(task) } - firstStep := task.Steps[0] + // firstStep is the first step that has run or running, not include preStep. + // For example, + // 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): firstStep is step1. + // 2. preStep(Success) -> step1(Skipped) -> step2(Success) -> postStep(Success): firstStep is step2. + // 3. preStep(Success) -> step1(Running) -> step2(Waiting) -> postStep(Waiting): firstStep is step1. + // 3. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil. + var firstStep *actions_model.ActionTaskStep var logIndex int64 + for _, step := range task.Steps { + if step.Status.HasRun() || step.Status.IsRunning() { + firstStep = step + break + } + } preStep := &actions_model.ActionTaskStep{ Name: preStepName, @@ -28,20 +40,13 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { Status: actions_model.StatusRunning, } - if firstStep.Status.HasRun() || firstStep.Status.IsRunning() { + if firstStep == nil { + preStep.Stopped = task.Stopped + preStep.Status = task.Status + } else { preStep.LogLength = firstStep.LogIndex preStep.Stopped = firstStep.Started preStep.Status = actions_model.StatusSuccess - } else if task.Status.IsDone() { - preStep.Stopped = task.Stopped - preStep.Status = actions_model.StatusFailure - // preStep(Success) -> step1(Skipped) -> step2(Success) - if firstStep.Status.IsSkipped() { - preStep.Status = actions_model.StatusSuccess - } - if task.Status.IsSkipped() { - preStep.Status = actions_model.StatusSkipped - } } logIndex += preStep.LogLength diff --git a/modules/actions/task_state_test.go b/modules/actions/task_state_test.go index 28213d781befe..ff0fd5719511e 100644 --- a/modules/actions/task_state_test.go +++ b/modules/actions/task_state_test.go @@ -137,6 +137,25 @@ func TestFullSteps(t *testing.T) { {Name: postStepName, Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0}, }, }, + { + name: "first step is skipped", + task: &actions_model.ActionTask{ + Steps: []*actions_model.ActionTaskStep{ + {Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0}, + {Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090}, + }, + Status: actions_model.StatusSuccess, + Started: 10000, + Stopped: 10100, + LogLength: 100, + }, + want: []*actions_model.ActionTaskStep{ + {Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010}, + {Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0}, + {Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090}, + {Name: postStepName, Status: actions_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 096bd4a88825582ea90d171d5180b1fd0587937f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 24 Sep 2024 04:51:28 +0000 Subject: [PATCH 3/4] improve --- modules/actions/task_state.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/modules/actions/task_state.go b/modules/actions/task_state.go index 834d73b6752c0..04bf4fdbfe72b 100644 --- a/modules/actions/task_state.go +++ b/modules/actions/task_state.go @@ -25,12 +25,23 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { // 3. preStep(Success) -> step1(Running) -> step2(Waiting) -> postStep(Waiting): firstStep is step1. // 3. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil. var firstStep *actions_model.ActionTaskStep + // lastHasRunStep is the last step that has run. + // For example, + // 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): lastHasRunStep is step1. + // 2. preStep(Success) -> step1(Success) -> step2(Success) -> step3(Success) -> postStep(Success): lastHasRunStep is step3. + // 3. preStep(Success) -> step1(Success) -> step2(Failure) -> step3 -> postStep(Waiting): lastHasRunStep is step2. + // So its Stopped is the Started of postStep when there are no more steps to run. + var lastHasRunStep *actions_model.ActionTaskStep + var logIndex int64 for _, step := range task.Steps { - if step.Status.HasRun() || step.Status.IsRunning() { + if firstStep == nil && (step.Status.HasRun() || step.Status.IsRunning()) { firstStep = step - break } + if step.Status.HasRun() { + lastHasRunStep = step + } + logIndex += step.LogLength } preStep := &actions_model.ActionTaskStep{ @@ -50,19 +61,6 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { } logIndex += preStep.LogLength - // lastHasRunStep is the last step that has run. - // For example, - // 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): lastHasRunStep is step1. - // 2. preStep(Success) -> step1(Success) -> step2(Success) -> step3(Success) -> postStep(Success): lastHasRunStep is step3. - // 3. preStep(Success) -> step1(Success) -> step2(Failure) -> step3 -> postStep(Waiting): lastHasRunStep is step2. - // So its Stopped is the Started of postStep when there are no more steps to run. - var lastHasRunStep *actions_model.ActionTaskStep - for _, step := range task.Steps { - if step.Status.HasRun() { - lastHasRunStep = step - } - logIndex += step.LogLength - } if lastHasRunStep == nil { lastHasRunStep = preStep } From dcd801135961f4f6f37747afb48e0d0577b5d62f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 24 Sep 2024 04:57:25 +0000 Subject: [PATCH 4/4] fix comment --- modules/actions/task_state.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/actions/task_state.go b/modules/actions/task_state.go index 04bf4fdbfe72b..1f36e021a59aa 100644 --- a/modules/actions/task_state.go +++ b/modules/actions/task_state.go @@ -23,7 +23,8 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { // 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): firstStep is step1. // 2. preStep(Success) -> step1(Skipped) -> step2(Success) -> postStep(Success): firstStep is step2. // 3. preStep(Success) -> step1(Running) -> step2(Waiting) -> postStep(Waiting): firstStep is step1. - // 3. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil. + // 4. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil. + // 5. preStep(Success) -> step1(Cancelled) -> step2(Cancelled) -> postStep(Cancelled): firstStep is nil. var firstStep *actions_model.ActionTaskStep // lastHasRunStep is the last step that has run. // For example, @@ -51,6 +52,7 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep { Status: actions_model.StatusRunning, } + // No step has run or is running, so preStep is equal to the task if firstStep == nil { preStep.Stopped = task.Stopped preStep.Status = task.Status