Skip to content

Commit db2ea0d

Browse files
committed
rewrite the logic
1 parent 928f35c commit db2ea0d

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

modules/actions/task_state.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
1818
return fullStepsOfEmptySteps(task)
1919
}
2020

21-
firstStep := task.Steps[0]
21+
// firstStep is the first step that has run or running, not include preStep.
22+
// For example,
23+
// 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): firstStep is step1.
24+
// 2. preStep(Success) -> step1(Skipped) -> step2(Success) -> postStep(Success): firstStep is step2.
25+
// 3. preStep(Success) -> step1(Running) -> step2(Waiting) -> postStep(Waiting): firstStep is step1.
26+
// 3. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil.
27+
var firstStep *actions_model.ActionTaskStep
2228
var logIndex int64
29+
for _, step := range task.Steps {
30+
if step.Status.HasRun() || step.Status.IsRunning() {
31+
firstStep = step
32+
break
33+
}
34+
}
2335

2436
preStep := &actions_model.ActionTaskStep{
2537
Name: preStepName,
@@ -28,20 +40,13 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
2840
Status: actions_model.StatusRunning,
2941
}
3042

31-
if firstStep.Status.HasRun() || firstStep.Status.IsRunning() {
43+
if firstStep == nil {
44+
preStep.Stopped = task.Stopped
45+
preStep.Status = task.Status
46+
} else {
3247
preStep.LogLength = firstStep.LogIndex
3348
preStep.Stopped = firstStep.Started
3449
preStep.Status = actions_model.StatusSuccess
35-
} else if task.Status.IsDone() {
36-
preStep.Stopped = task.Stopped
37-
preStep.Status = actions_model.StatusFailure
38-
// preStep(Success) -> step1(Skipped) -> step2(Success)
39-
if firstStep.Status.IsSkipped() {
40-
preStep.Status = actions_model.StatusSuccess
41-
}
42-
if task.Status.IsSkipped() {
43-
preStep.Status = actions_model.StatusSkipped
44-
}
4550
}
4651
logIndex += preStep.LogLength
4752

modules/actions/task_state_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ func TestFullSteps(t *testing.T) {
137137
{Name: postStepName, Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
138138
},
139139
},
140+
{
141+
name: "first step is skipped",
142+
task: &actions_model.ActionTask{
143+
Steps: []*actions_model.ActionTaskStep{
144+
{Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
145+
{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
146+
},
147+
Status: actions_model.StatusSuccess,
148+
Started: 10000,
149+
Stopped: 10100,
150+
LogLength: 100,
151+
},
152+
want: []*actions_model.ActionTaskStep{
153+
{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
154+
{Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
155+
{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
156+
{Name: postStepName, Status: actions_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
157+
},
158+
},
140159
}
141160
for _, tt := range tests {
142161
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)