From 00acf681880b0fe7cea27a29d61c261d624966c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Brauer?= <bjoern.brauer@new-work.se>
Date: Tue, 27 Sep 2022 17:49:22 +0200
Subject: [PATCH] feat: pass current matrix information to job logger (#1364)

For log processing of the JSON logs, we want to be able to know which
keys/values of the matrices were used.
This commit adds the current matrix map to the job logger.

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>
---
 pkg/runner/command_test.go | 2 +-
 pkg/runner/logger.go       | 3 ++-
 pkg/runner/runner.go       | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/pkg/runner/command_test.go b/pkg/runner/command_test.go
index 0f3d32b..0b6ec8c 100644
--- a/pkg/runner/command_test.go
+++ b/pkg/runner/command_test.go
@@ -164,7 +164,7 @@ func TestAddmaskUsemask(t *testing.T) {
 
 	re := captureOutput(t, func() {
 		ctx := context.Background()
-		ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks)
+		ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks, map[string]interface{}{})
 
 		handler := rc.commandHandler(ctx)
 		handler("::add-mask::secret\n")
diff --git a/pkg/runner/logger.go b/pkg/runner/logger.go
index 4505f59..81cae21 100644
--- a/pkg/runner/logger.go
+++ b/pkg/runner/logger.go
@@ -58,7 +58,7 @@ func WithMasks(ctx context.Context, masks *[]string) context.Context {
 }
 
 // WithJobLogger attaches a new logger to context that is aware of steps
-func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Config, masks *[]string) context.Context {
+func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Config, masks *[]string, matrix map[string]interface{}) context.Context {
 	mux.Lock()
 	defer mux.Unlock()
 
@@ -86,6 +86,7 @@ func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Co
 		"job":    jobName,
 		"jobID":  jobID,
 		"dryrun": common.Dryrun(ctx),
+		"matrix": matrix,
 	}).WithContext(ctx)
 
 	return common.WithLogger(ctx, rtn)
diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go
index aa66af7..e5646c2 100644
--- a/pkg/runner/runner.go
+++ b/pkg/runner/runner.go
@@ -192,7 +192,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
 							}
 
 							return nil
-						})(common.WithJobErrorContainer(WithJobLogger(ctx, rc.Run.JobID, jobName, rc.Config, &rc.Masks)))
+						})(common.WithJobErrorContainer(WithJobLogger(ctx, rc.Run.JobID, jobName, rc.Config, &rc.Masks, matrix)))
 					})
 				}
 				pipeline = append(pipeline, common.NewParallelExecutor(maxParallel, stageExecutor...))