Skip to content

Commit 6fa962f

Browse files
authored
Fix wrong status of Set up Job when first step is skipped (#32120)
Fix #32089
1 parent 5a85684 commit 6fa962f

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

modules/actions/task_state.go

+30-21
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,32 @@ 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+
// 4. preStep(Success) -> step1(Skipped) -> step2(Skipped) -> postStep(Skipped): firstStep is nil.
27+
// 5. preStep(Success) -> step1(Cancelled) -> step2(Cancelled) -> postStep(Cancelled): firstStep is nil.
28+
var firstStep *actions_model.ActionTaskStep
29+
// lastHasRunStep is the last step that has run.
30+
// For example,
31+
// 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): lastHasRunStep is step1.
32+
// 2. preStep(Success) -> step1(Success) -> step2(Success) -> step3(Success) -> postStep(Success): lastHasRunStep is step3.
33+
// 3. preStep(Success) -> step1(Success) -> step2(Failure) -> step3 -> postStep(Waiting): lastHasRunStep is step2.
34+
// So its Stopped is the Started of postStep when there are no more steps to run.
35+
var lastHasRunStep *actions_model.ActionTaskStep
36+
2237
var logIndex int64
38+
for _, step := range task.Steps {
39+
if firstStep == nil && (step.Status.HasRun() || step.Status.IsRunning()) {
40+
firstStep = step
41+
}
42+
if step.Status.HasRun() {
43+
lastHasRunStep = step
44+
}
45+
logIndex += step.LogLength
46+
}
2347

2448
preStep := &actions_model.ActionTaskStep{
2549
Name: preStepName,
@@ -28,32 +52,17 @@ func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
2852
Status: actions_model.StatusRunning,
2953
}
3054

31-
if firstStep.Status.HasRun() || firstStep.Status.IsRunning() {
55+
// No step has run or is running, so preStep is equal to the task
56+
if firstStep == nil {
57+
preStep.Stopped = task.Stopped
58+
preStep.Status = task.Status
59+
} else {
3260
preStep.LogLength = firstStep.LogIndex
3361
preStep.Stopped = firstStep.Started
3462
preStep.Status = actions_model.StatusSuccess
35-
} else if task.Status.IsDone() {
36-
preStep.Stopped = task.Stopped
37-
preStep.Status = actions_model.StatusFailure
38-
if task.Status.IsSkipped() {
39-
preStep.Status = actions_model.StatusSkipped
40-
}
4163
}
4264
logIndex += preStep.LogLength
4365

44-
// lastHasRunStep is the last step that has run.
45-
// For example,
46-
// 1. preStep(Success) -> step1(Success) -> step2(Running) -> step3(Waiting) -> postStep(Waiting): lastHasRunStep is step1.
47-
// 2. preStep(Success) -> step1(Success) -> step2(Success) -> step3(Success) -> postStep(Success): lastHasRunStep is step3.
48-
// 3. preStep(Success) -> step1(Success) -> step2(Failure) -> step3 -> postStep(Waiting): lastHasRunStep is step2.
49-
// So its Stopped is the Started of postStep when there are no more steps to run.
50-
var lastHasRunStep *actions_model.ActionTaskStep
51-
for _, step := range task.Steps {
52-
if step.Status.HasRun() {
53-
lastHasRunStep = step
54-
}
55-
logIndex += step.LogLength
56-
}
5766
if lastHasRunStep == nil {
5867
lastHasRunStep = preStep
5968
}

modules/actions/task_state_test.go

+19
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)