Skip to content

Commit 5ffec84

Browse files
fix: if condition in composite action misbehaves (#2473)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent bb9f36d commit 5ffec84

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

pkg/runner/expression.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ var hashfiles string
108108

109109
// NewStepExpressionEvaluator creates a new evaluator
110110
func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step) ExpressionEvaluator {
111+
return rc.NewStepExpressionEvaluatorExt(ctx, step, false)
112+
}
113+
114+
// NewStepExpressionEvaluatorExt creates a new evaluator
115+
func (rc *RunContext) NewStepExpressionEvaluatorExt(ctx context.Context, step step, rcInputs bool) ExpressionEvaluator {
116+
ghc := rc.getGithubContext(ctx)
117+
if rcInputs {
118+
return rc.newStepExpressionEvaluator(ctx, step, ghc, getEvaluatorInputs(ctx, rc, nil, ghc))
119+
}
120+
return rc.newStepExpressionEvaluator(ctx, step, ghc, getEvaluatorInputs(ctx, rc, step, ghc))
121+
}
122+
123+
func (rc *RunContext) newStepExpressionEvaluator(ctx context.Context, step step, ghc *model.GithubContext, inputs map[string]interface{}) ExpressionEvaluator {
111124
// todo: cleanup EvaluationEnvironment creation
112125
job := rc.Run.Job()
113126
strategy := make(map[string]interface{})
@@ -127,9 +140,6 @@ func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step)
127140
}
128141
}
129142

130-
ghc := rc.getGithubContext(ctx)
131-
inputs := getEvaluatorInputs(ctx, rc, step, ghc)
132-
133143
ee := &exprparser.EvaluationEnvironment{
134144
Github: step.getGithubContext(ctx),
135145
Env: *step.getEnv(),

pkg/runner/step.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func isStepEnabled(ctx context.Context, expr string, step step, stage stepStage)
261261
defaultStatusCheck = exprparser.DefaultStatusCheckSuccess
262262
}
263263

264-
runStep, err := EvalBool(ctx, rc.NewStepExpressionEvaluator(ctx, step), expr, defaultStatusCheck)
264+
runStep, err := EvalBool(ctx, rc.NewStepExpressionEvaluatorExt(ctx, step, stage == stepStageMain), expr, defaultStatusCheck)
265265
if err != nil {
266266
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", expr, err)
267267
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: "Test Composite Action"
2+
description: "Test action uses composite"
3+
4+
inputs:
5+
b:
6+
default: true
7+
b2: {}
8+
9+
runs:
10+
using: "composite"
11+
steps:
12+
- uses: actions/github-script@v7
13+
if: inputs.b == 'true'
14+
with:
15+
script: |
16+
console.log(${{ tojson(inputs) }})
17+
if( ${{ tojson(inputs.b) }} != 'true' ) {
18+
process.exit(-1);
19+
}
20+
github-token: noop
21+
- uses: actions/github-script@v7
22+
if: inputs.b != 'true'
23+
with:
24+
script: |
25+
console.log(${{ tojson(inputs) }})
26+
if( ${{ tojson(inputs.b) }} == 'true' ) {
27+
process.exit(-1);
28+
}
29+
github-token: noop
30+
- uses: actions/github-script@v7
31+
if: inputs.b2 == 'false'
32+
with:
33+
script: |
34+
console.log(${{ tojson(inputs) }})
35+
if( ${{ tojson(inputs.b2) }} != 'false' ) {
36+
process.exit(-1);
37+
}
38+
github-token: noop
39+
- uses: actions/github-script@v7
40+
if: inputs.b2 != 'false'
41+
with:
42+
script: |
43+
console.log(${{ tojson(inputs) }})
44+
if( ${{ tojson(inputs.b2) }} == 'false' ) {
45+
process.exit(-1);
46+
}
47+
github-token: noop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: uses-composite-check-for-input-in-if-uses
2+
on: push
3+
4+
jobs:
5+
test:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4
9+
- uses: ./uses-composite-check-for-input-in-if-uses/composite_action
10+
with:
11+
b: true
12+
b2: true
13+
- uses: ./uses-composite-check-for-input-in-if-uses/composite_action
14+
with:
15+
b: false
16+
b2: false
17+
- uses: ./uses-composite-check-for-input-in-if-uses/composite_action
18+
with:
19+
b: true
20+
b2: false
21+
- uses: ./uses-composite-check-for-input-in-if-uses/composite_action
22+
with:
23+
b: false
24+
b2: true

0 commit comments

Comments
 (0)