Skip to content

Commit

Permalink
feat(rulesets): add instance key to ruleset (#1157)
Browse files Browse the repository at this point in the history
* Add instance key to rule data for evaluation

* Upgrade types version

* Update types version

* Update tests

---------

Co-authored-by: wsan3 <[email protected]>
Co-authored-by: David May <[email protected]>
Co-authored-by: ecrupper <[email protected]>
  • Loading branch information
4 people committed Sep 12, 2024
1 parent d83c78e commit b6e5d75
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
17 changes: 9 additions & 8 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
92 changes: 88 additions & 4 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit b6e5d75

Please sign in to comment.