Skip to content

Commit 6b15f3c

Browse files
committed
chore: more debug logging
1 parent 3bfa579 commit 6b15f3c

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

pkg/config/action.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
package config
22

33
import (
4+
"context"
5+
"log/slog"
6+
47
"github.com/expr-lang/expr"
58
"github.com/expr-lang/expr/patcher"
69
"github.com/expr-lang/expr/vm"
710
"github.com/jippi/scm-engine/pkg/scm"
811
"github.com/jippi/scm-engine/pkg/stdlib"
12+
slogctx "github.com/veqryn/slog-context"
913
)
1014

1115
type Actions []Action
1216

13-
func (actions Actions) Evaluate(evalContext scm.EvalContext) ([]Action, error) {
17+
func (actions Actions) Evaluate(ctx context.Context, evalContext scm.EvalContext) ([]Action, error) {
1418
results := []Action{}
1519

1620
// Evaluate actions
1721
for _, action := range actions {
18-
ok, err := action.Evaluate(evalContext)
22+
ctx := slogctx.With(ctx, slog.String("action_name", action.Name))
23+
24+
slogctx.Debug(ctx, "Evaluating action")
25+
26+
ok, err := action.Evaluate(ctx, evalContext)
1927
if err != nil {
2028
return nil, err
2129
}
2230

2331
if !ok {
32+
slogctx.Debug(ctx, "Action evaluated negatively, skipping")
33+
2434
continue
2535
}
2636

37+
slogctx.Debug(ctx, "Action evaluated positively")
38+
2739
results = append(results, action)
2840
}
2941

@@ -32,14 +44,14 @@ func (actions Actions) Evaluate(evalContext scm.EvalContext) ([]Action, error) {
3244

3345
type Action scm.EvaluationActionResult
3446

35-
func (p *Action) Evaluate(evalContext scm.EvalContext) (bool, error) {
47+
func (p *Action) Evaluate(ctx context.Context, evalContext scm.EvalContext) (bool, error) {
3648
program, err := p.initialize(evalContext)
3749
if err != nil {
3850
return false, err
3951
}
4052

4153
// Run the compiled expr-lang script
42-
return runAndCheckBool(program, evalContext)
54+
return runAndCheckBool(ctx, program, evalContext)
4355
}
4456

4557
func (p *Action) initialize(evalContext scm.EvalContext) (*vm.Program, error) {

pkg/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ type Config struct {
1717
func (c Config) Evaluate(ctx context.Context, evalContext scm.EvalContext) ([]scm.EvaluationResult, []Action, error) {
1818
slogctx.Info(ctx, "Evaluating labels")
1919

20-
labels, err := c.Labels.Evaluate(evalContext)
20+
labels, err := c.Labels.Evaluate(ctx, evalContext)
2121
if err != nil {
2222
return nil, nil, fmt.Errorf("evaluation failed: %w", err)
2323
}
2424

2525
slogctx.Info(ctx, "Evaluating Actions")
2626

27-
actions, err := c.Actions.Evaluate(evalContext)
27+
actions, err := c.Actions.Evaluate(ctx, evalContext)
2828
if err != nil {
2929
return nil, nil, err
3030
}

pkg/config/label.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package config
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
7+
"log/slog"
68
"reflect"
79

810
"github.com/expr-lang/expr"
@@ -12,6 +14,7 @@ import (
1214
"github.com/jippi/scm-engine/pkg/stdlib"
1315
"github.com/jippi/scm-engine/pkg/tui"
1416
"github.com/jippi/scm-engine/pkg/types"
17+
slogctx "github.com/veqryn/slog-context"
1518
)
1619

1720
// labelType is a custom type for our enum
@@ -24,20 +27,28 @@ const (
2427

2528
type Labels []*Label
2629

27-
func (labels Labels) Evaluate(evalContext scm.EvalContext) ([]scm.EvaluationResult, error) {
30+
func (labels Labels) Evaluate(ctx context.Context, evalContext scm.EvalContext) ([]scm.EvaluationResult, error) {
2831
var results []scm.EvaluationResult
2932

3033
// Evaluate labels
3134
for _, label := range labels {
32-
evaluationResult, err := label.Evaluate(evalContext)
35+
ctx := slogctx.With(ctx, slog.String("label_name", label.Name))
36+
37+
slogctx.Debug(ctx, "Evaluating label")
38+
39+
evaluationResult, err := label.Evaluate(ctx, evalContext)
3340
if err != nil {
3441
return nil, fmt.Errorf("label: %s; %w", label.Name, err)
3542
}
3643

3744
if evaluationResult == nil {
45+
slogctx.Debug(ctx, "Label evaluated negatively, skipping")
46+
3847
continue
3948
}
4049

50+
slogctx.Debug(ctx, "Label evaluation done", slog.Any("label_eval_result", evaluationResult))
51+
4152
results = append(results, evaluationResult...)
4253
}
4354

@@ -183,21 +194,21 @@ func (p *Label) initialize(evalContext scm.EvalContext) error {
183194
return nil
184195
}
185196

186-
func (p *Label) ShouldSkip(evalContext scm.EvalContext) (bool, error) {
197+
func (p *Label) ShouldSkip(ctx context.Context, evalContext scm.EvalContext) (bool, error) {
187198
if err := p.initialize(evalContext); err != nil {
188199
return true, err
189200
}
190201

191-
return runAndCheckBool(p.skipIfCompiled, evalContext)
202+
return runAndCheckBool(ctx, p.skipIfCompiled, evalContext)
192203
}
193204

194-
func (p *Label) Evaluate(evalContext scm.EvalContext) ([]scm.EvaluationResult, error) {
205+
func (p *Label) Evaluate(ctx context.Context, evalContext scm.EvalContext) ([]scm.EvaluationResult, error) {
195206
if err := p.initialize(evalContext); err != nil {
196207
return nil, fmt.Errorf("failed to initialize expr script engine: %w", err)
197208
}
198209

199210
// Check if the label should be skipped
200-
if skip, err := p.ShouldSkip(evalContext); err != nil || skip {
211+
if skip, err := p.ShouldSkip(ctx, evalContext); err != nil || skip {
201212
return nil, err
202213
}
203214

@@ -260,7 +271,7 @@ func (p Label) resultForLabel(name string, matched bool) scm.EvaluationResult {
260271
}
261272
}
262273

263-
func runAndCheckBool(program *vm.Program, evalContext scm.EvalContext) (bool, error) {
274+
func runAndCheckBool(ctx context.Context, program *vm.Program, evalContext scm.EvalContext) (bool, error) {
264275
if program == nil {
265276
return false, nil
266277
}
@@ -272,6 +283,8 @@ func runAndCheckBool(program *vm.Program, evalContext scm.EvalContext) (bool, er
272283

273284
switch outputValue := output.(type) {
274285
case bool:
286+
slogctx.Debug(ctx, "script eval done", slog.Bool("script_outcome", outputValue))
287+
275288
return outputValue, nil
276289

277290
default:

0 commit comments

Comments
 (0)