From 64cae197a46ba9f5d4dd9d40a313a89169515258 Mon Sep 17 00:00:00 2001
From: Jason Song <wolfogre@noreply.gitea.io>
Date: Thu, 22 Sep 2022 21:56:43 +0800
Subject: [PATCH] Support step number

---
 pkg/model/workflow.go          | 1 +
 pkg/runner/action_composite.go | 1 +
 pkg/runner/job_executor.go     | 3 ++-
 pkg/runner/logger.go           | 9 +++++----
 4 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go
index 214927e..0a6183a 100644
--- a/pkg/model/workflow.go
+++ b/pkg/model/workflow.go
@@ -396,6 +396,7 @@ type ContainerSpec struct {
 
 // Step is the structure of one step in a job
 type Step struct {
+	Number             int               `yaml:"-"`
 	ID                 string            `yaml:"id"`
 	If                 yaml.Node         `yaml:"if"`
 	Name               string            `yaml:"name"`
diff --git a/pkg/runner/action_composite.go b/pkg/runner/action_composite.go
index 9001b48..f2cd560 100644
--- a/pkg/runner/action_composite.go
+++ b/pkg/runner/action_composite.go
@@ -120,6 +120,7 @@ func (rc *RunContext) compositeExecutor(action *model.Action) *compositeSteps {
 		if step.ID == "" {
 			step.ID = fmt.Sprintf("%d", i)
 		}
+		step.Number = i
 
 		// create a copy of the step, since this composite action could
 		// run multiple times and we might modify the instance
diff --git a/pkg/runner/job_executor.go b/pkg/runner/job_executor.go
index 5c3e3b1..3e175c0 100644
--- a/pkg/runner/job_executor.go
+++ b/pkg/runner/job_executor.go
@@ -48,6 +48,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
 		if stepModel.ID == "" {
 			stepModel.ID = fmt.Sprintf("%d", i)
 		}
+		stepModel.Number = i
 
 		step, err := sf.newStep(stepModel, rc)
 
@@ -119,7 +120,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
 
 func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
 	return func(ctx context.Context) error {
-		ctx = withStepLogger(ctx, stepModel.ID, stepModel.String(), stage.String())
+		ctx = withStepLogger(ctx, stepModel.Number, stepModel.ID, stepModel.String(), stage.String())
 
 		rawLogger := common.Logger(ctx).WithField("raw_output", true)
 		logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
diff --git a/pkg/runner/logger.go b/pkg/runner/logger.go
index 3bee6b7..7b97927 100644
--- a/pkg/runner/logger.go
+++ b/pkg/runner/logger.go
@@ -117,11 +117,12 @@ func WithCompositeStepLogger(ctx context.Context, stepID string) context.Context
 	}).WithContext(ctx))
 }
 
-func withStepLogger(ctx context.Context, stepID string, stepName string, stageName string) context.Context {
+func withStepLogger(ctx context.Context, stepNumber int, stepID, stepName, stageName string) context.Context {
 	rtn := common.Logger(ctx).WithFields(logrus.Fields{
-		"step":   stepName,
-		"stepID": []string{stepID},
-		"stage":  stageName,
+		"stepNumber": stepNumber,
+		"step":       stepName,
+		"stepID":     []string{stepID},
+		"stage":      stageName,
 	})
 	return common.WithLogger(ctx, rtn)
 }