From b6e5d752221df0e749f4ee941faa9666eaf78dd2 Mon Sep 17 00:00:00 2001 From: Win San Date: Thu, 12 Sep 2024 08:53:59 -0500 Subject: [PATCH] feat(rulesets): add instance key to ruleset (#1157) * Add instance key to rule data for evaluation * Upgrade types version * Update types version * Update tests --------- Co-authored-by: wsan3 Co-authored-by: David May <49894298+wass3rw3rk@users.noreply.github.com> Co-authored-by: ecrupper --- compiler/native/compile.go | 17 +++--- compiler/native/compile_test.go | 92 +++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 12 deletions(-) diff --git a/compiler/native/compile.go b/compiler/native/compile.go index 6b246f0fe..f73ff505e 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -63,14 +63,15 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, * // create the ruledata to purge steps r := &pipeline.RuleData{ - Branch: c.build.GetBranch(), - Comment: c.comment, - Event: event, - Path: c.files, - Repo: c.repo.GetFullName(), - Tag: strings.TrimPrefix(c.build.GetRef(), "refs/tags/"), - Target: c.build.GetDeploy(), - Label: c.labels, + Branch: c.build.GetBranch(), + Comment: c.comment, + Event: event, + Path: c.files, + Repo: c.repo.GetFullName(), + Tag: strings.TrimPrefix(c.build.GetRef(), "refs/tags/"), + Target: c.build.GetDeploy(), + Label: c.labels, + Instance: c.metadata.Vela.Address, } switch { diff --git a/compiler/native/compile_test.go b/compiler/native/compile_test.go index 84f5d8158..7b4907d45 100644 --- a/compiler/native/compile_test.go +++ b/compiler/native/compile_test.go @@ -294,6 +294,26 @@ func TestNative_Compile_StagesPipeline_Modification(t *testing.T) { author := "author" number := 1 + m := &internal.Metadata{ + Database: &internal.Database{ + Driver: "foo", + Host: "foo", + }, + Queue: &internal.Queue{ + Channel: "foo", + Driver: "foo", + Host: "foo", + }, + Source: &internal.Source{ + Driver: "foo", + Host: "foo", + }, + Vela: &internal.Vela{ + Address: "foo", + WebAddress: "foo", + }, + } + // run test yaml, err := os.ReadFile("testdata/stages_pipeline.yml") if err != nil { @@ -330,8 +350,9 @@ func TestNative_Compile_StagesPipeline_Modification(t *testing.T) { Timeout: 1 * time.Second, Endpoint: tt.args.endpoint, }, - repo: &api.Repo{Name: &author}, - build: &api.Build{Author: &name, Number: &number}, + metadata: m, + repo: &api.Repo{Name: &author}, + build: &api.Build{Author: &name, Number: &number}, } _, _, err := compiler.Compile(context.Background(), yaml) if (err != nil) != tt.wantErr { @@ -362,6 +383,26 @@ func TestNative_Compile_StepsPipeline_Modification(t *testing.T) { author := "author" number := 1 + m := &internal.Metadata{ + Database: &internal.Database{ + Driver: "foo", + Host: "foo", + }, + Queue: &internal.Queue{ + Channel: "foo", + Driver: "foo", + Host: "foo", + }, + Source: &internal.Source{ + Driver: "foo", + Host: "foo", + }, + Vela: &internal.Vela{ + Address: "foo", + WebAddress: "foo", + }, + } + // run test yaml, err := os.ReadFile("testdata/steps_pipeline.yml") if err != nil { @@ -398,8 +439,9 @@ func TestNative_Compile_StepsPipeline_Modification(t *testing.T) { Timeout: 1 * time.Second, Endpoint: tt.args.endpoint, }, - repo: tt.args.repo, - build: tt.args.libraryBuild, + repo: tt.args.repo, + build: tt.args.libraryBuild, + metadata: m, } _, _, err := compiler.Compile(context.Background(), yaml) if (err != nil) != tt.wantErr { @@ -1836,6 +1878,26 @@ func TestNative_Compile_NoStepsorStages(t *testing.T) { author := "author" number := 1 + m := &internal.Metadata{ + Database: &internal.Database{ + Driver: "foo", + Host: "foo", + }, + Queue: &internal.Queue{ + Channel: "foo", + Driver: "foo", + Host: "foo", + }, + Source: &internal.Source{ + Driver: "foo", + Host: "foo", + }, + Vela: &internal.Vela{ + Address: "foo", + WebAddress: "foo", + }, + } + // run test yaml, err := os.ReadFile("testdata/metadata.yml") if err != nil { @@ -1850,6 +1912,7 @@ func TestNative_Compile_NoStepsorStages(t *testing.T) { // todo: this needs to be fixed in compiler validation // this is a dirty hack to make this test pass compiler.SetCloneImage("") + compiler.WithMetadata(m) compiler.repo = &api.Repo{Name: &author} compiler.build = &api.Build{Author: &name, Number: &number} @@ -1873,6 +1936,26 @@ func TestNative_Compile_StepsandStages(t *testing.T) { author := "author" number := 1 + m := &internal.Metadata{ + Database: &internal.Database{ + Driver: "foo", + Host: "foo", + }, + Queue: &internal.Queue{ + Channel: "foo", + Driver: "foo", + Host: "foo", + }, + Source: &internal.Source{ + Driver: "foo", + Host: "foo", + }, + Vela: &internal.Vela{ + Address: "foo", + WebAddress: "foo", + }, + } + // run test yaml, err := os.ReadFile("testdata/steps_and_stages.yml") if err != nil { @@ -1886,6 +1969,7 @@ func TestNative_Compile_StepsandStages(t *testing.T) { compiler.repo = &api.Repo{Name: &author} compiler.build = &api.Build{Author: &name, Number: &number} + compiler.WithMetadata(m) got, _, err := compiler.Compile(context.Background(), yaml) if err == nil {