feat: interpolate the step names (#1422)

* feat: interpolate the step names

Step names could contain expressions refering to event data.

Fixes #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 <cplee@nektos.com>
This commit is contained in:
Markus Wolf 2022-11-16 22:55:23 +01:00 committed by GitHub
parent e520382d2f
commit 809da7198c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 4 deletions

View file

@ -71,6 +71,7 @@ func newCompositeRunContext(ctx context.Context, parent *RunContext, step action
Parent: parent, Parent: parent,
EventJSON: parent.EventJSON, EventJSON: parent.EventJSON,
} }
compositerc.ExprEval = compositerc.NewExpressionEvaluator(ctx)
return compositerc return compositerc
} }

View file

@ -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 { func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
return func(ctx context.Context) error { 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) rawLogger := common.Logger(ctx).WithField("raw_output", true)
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool { logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {

View file

@ -249,7 +249,17 @@ func TestNewJobExecutor(t *testing.T) {
sfm := &stepFactoryMock{} sfm := &stepFactoryMock{}
rc := &RunContext{ rc := &RunContext{
JobContainer: &jobContainerMock{}, 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) executorOrder := make([]string, 0)
jim.On("steps").Return(tt.steps) jim.On("steps").Return(tt.steps)

View file

@ -90,7 +90,7 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
return nil return nil
} }
stepString := stepModel.String() stepString := rc.ExprEval.Interpolate(ctx, stepModel.String())
if strings.Contains(stepString, "::add-mask::") { if strings.Contains(stepString, "::add-mask::") {
stepString = "add-mask command" stepString = "add-mask command"
} }

View file

@ -275,6 +275,7 @@ func TestStepActionLocalPost(t *testing.T) {
Step: tt.stepModel, Step: tt.stepModel,
action: tt.actionModel, action: tt.actionModel,
} }
sal.RunContext.ExprEval = sal.RunContext.NewExpressionEvaluator(ctx)
if tt.mocks.env { if tt.mocks.env {
cm.On("UpdateFromImageEnv", &sal.env).Return(func(ctx context.Context) error { return nil }) cm.On("UpdateFromImageEnv", &sal.env).Return(func(ctx context.Context) error { return nil })

View file

@ -155,6 +155,7 @@ func TestStepActionRemote(t *testing.T) {
readAction: sarm.readAction, readAction: sarm.readAction,
runAction: sarm.runAction, runAction: sarm.runAction,
} }
sar.RunContext.ExprEval = sar.RunContext.NewExpressionEvaluator(ctx)
suffixMatcher := func(suffix string) interface{} { suffixMatcher := func(suffix string) interface{} {
return mock.MatchedBy(func(actionDir string) bool { return mock.MatchedBy(func(actionDir string) bool {
@ -586,6 +587,7 @@ func TestStepActionRemotePost(t *testing.T) {
Step: tt.stepModel, Step: tt.stepModel,
action: tt.actionModel, action: tt.actionModel,
} }
sar.RunContext.ExprEval = sar.RunContext.NewExpressionEvaluator(ctx)
if tt.mocks.env { if tt.mocks.env {
cm.On("UpdateFromImageEnv", &sar.env).Return(func(ctx context.Context) error { return nil }) cm.On("UpdateFromImageEnv", &sar.env).Return(func(ctx context.Context) error { return nil })

View file

@ -25,6 +25,8 @@ func TestStepDockerMain(t *testing.T) {
ContainerNewContainer = origContainerNewContainer ContainerNewContainer = origContainerNewContainer
})() })()
ctx := context.Background()
sd := &stepDocker{ sd := &stepDocker{
RunContext: &RunContext{ RunContext: &RunContext{
StepResults: map[string]*model.StepResult{}, StepResults: map[string]*model.StepResult{},
@ -51,8 +53,7 @@ func TestStepDockerMain(t *testing.T) {
WorkingDirectory: "workdir", WorkingDirectory: "workdir",
}, },
} }
sd.RunContext.ExprEval = sd.RunContext.NewExpressionEvaluator(ctx)
ctx := context.Background()
cm.On("UpdateFromImageEnv", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error { cm.On("UpdateFromImageEnv", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
return nil return nil