Skip to content

Commit

Permalink
feat: interpolate the step names (nektos#1422)
Browse files Browse the repository at this point in the history
* feat: interpolate the step names

Step names could contain expressions refering to event data.

Fixes nektos#1353

* test: add missing mock data

* fix: setup composite expression evaluator

The RunContext does contain a cached ExpressionEvaluator.
This should be the case the composite RunContext as well.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Casey Lee <[email protected]>
  • Loading branch information
3 people authored Nov 16, 2022
1 parent e520382 commit 809da71
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/runner/action_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func newCompositeRunContext(ctx context.Context, parent *RunContext, step action
Parent: parent,
EventJSON: parent.EventJSON,
}
compositerc.ExprEval = compositerc.NewExpressionEvaluator(ctx)

return compositerc
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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.ID, rc.ExprEval.Interpolate(ctx, stepModel.String()), stage.String())

rawLogger := common.Logger(ctx).WithField("raw_output", true)
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
Expand Down
10 changes: 10 additions & 0 deletions pkg/runner/job_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,17 @@ func TestNewJobExecutor(t *testing.T) {
sfm := &stepFactoryMock{}
rc := &RunContext{
JobContainer: &jobContainerMock{},
Run: &model.Run{
JobID: "test",
Workflow: &model.Workflow{
Jobs: map[string]*model.Job{
"test": {},
},
},
},
Config: &Config{},
}
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
executorOrder := make([]string, 0)

jim.On("steps").Return(tt.steps)
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
return nil
}

stepString := stepModel.String()
stepString := rc.ExprEval.Interpolate(ctx, stepModel.String())
if strings.Contains(stepString, "::add-mask::") {
stepString = "add-mask command"
}
Expand Down
1 change: 1 addition & 0 deletions pkg/runner/step_action_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func TestStepActionLocalPost(t *testing.T) {
Step: tt.stepModel,
action: tt.actionModel,
}
sal.RunContext.ExprEval = sal.RunContext.NewExpressionEvaluator(ctx)

if tt.mocks.env {
cm.On("UpdateFromImageEnv", &sal.env).Return(func(ctx context.Context) error { return nil })
Expand Down
2 changes: 2 additions & 0 deletions pkg/runner/step_action_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func TestStepActionRemote(t *testing.T) {
readAction: sarm.readAction,
runAction: sarm.runAction,
}
sar.RunContext.ExprEval = sar.RunContext.NewExpressionEvaluator(ctx)

suffixMatcher := func(suffix string) interface{} {
return mock.MatchedBy(func(actionDir string) bool {
Expand Down Expand Up @@ -586,6 +587,7 @@ func TestStepActionRemotePost(t *testing.T) {
Step: tt.stepModel,
action: tt.actionModel,
}
sar.RunContext.ExprEval = sar.RunContext.NewExpressionEvaluator(ctx)

if tt.mocks.env {
cm.On("UpdateFromImageEnv", &sar.env).Return(func(ctx context.Context) error { return nil })
Expand Down
5 changes: 3 additions & 2 deletions pkg/runner/step_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestStepDockerMain(t *testing.T) {
ContainerNewContainer = origContainerNewContainer
})()

ctx := context.Background()

sd := &stepDocker{
RunContext: &RunContext{
StepResults: map[string]*model.StepResult{},
Expand All @@ -51,8 +53,7 @@ func TestStepDockerMain(t *testing.T) {
WorkingDirectory: "workdir",
},
}

ctx := context.Background()
sd.RunContext.ExprEval = sd.RunContext.NewExpressionEvaluator(ctx)

cm.On("UpdateFromImageEnv", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
return nil
Expand Down

0 comments on commit 809da71

Please sign in to comment.