Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump roast to v0.3.0 and live free from annotations on module #1140

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bundle/regal/ast/comments.rego
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ comments["annotation_match"](str) if regex.match(
str,
)

# METADATA
# description: array containing all annotations from the module
annotations := array.concat(
[annotation | some annotation in input["package"].annotations],
[annotation | annotation := input.rules[_].annotations[_]],
)

# METADATA
# description: |
# map of all ignore directive comments, like ("# regal ignore:line-length")
Expand Down
26 changes: 10 additions & 16 deletions bundle/regal/rules/custom/missing-metadata/missing_metadata.rego
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,28 @@ aggregate contains result.aggregate(rego.metadata.chain(), {

default _package_annotated := false

_package_annotated if {
some annotation in input.annotations
annotation.scope in {"package", "subpackages"}
}
_package_annotated if input["package"].annotations

_rule_annotations[path] contains annotated if {
_rule_annotations[rule_path] contains annotated if {
some rule in ast.public_rules_and_functions
every part in rule.head.ref {
not startswith(part.value, "_")
}

path := concat(".", [ast.package_name, ast.ref_static_to_string(rule.head.ref)])

rule_path := concat(".", [ast.package_name, ast.ref_static_to_string(rule.head.ref)])
annotated := count(object.get(rule, "annotations", [])) > 0
}

_rule_locations[path] := location if {
head := ast.public_rules_and_functions[_].head
rref := ast.ref_static_to_string(head.ref)
_rule_locations[rule_path] := location if {
some rule_path, annotated in _rule_annotations

# we only care about locations of non-annotated rules
not true in annotated

location := [h.location |
h := ast.public_rules_and_functions[_].head
ast.ref_static_to_string(h.ref) == rref
concat(".", [ast.package_name, ast.ref_static_to_string(h.ref)]) == rule_path
][0]

path := concat(".", [ast.package_name, rref])
}

# METADATA
Expand Down Expand Up @@ -116,14 +112,12 @@ _package_path_aggs[pkg_path] contains item if {
# - input: schema.regal.aggregate
_rule_path_aggs[rule_path] contains agg if {
some item in input.aggregate

some rule_path, annotations in item.aggregate_data.rule_annotations
annotated := annotations != {false}

agg := {
"file": item.aggregate_source.file,
"location": item.aggregate_data.rule_locations[rule_path],
"annotated": annotated,
"annotated": true in annotations,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ none := false
"foo.bar.none": {false},
"foo.bar.rule": {true},
},
"rule_locations": {
"foo.bar.none": "9:1:bm9uZSA6PSBmYWxzZQ==",
"foo.bar.rule": "7:1:cnVsZSA6PSB0cnVl",
},
"rule_locations": {"foo.bar.none": "9:1:bm9uZSA6PSBmYWxzZQ=="},
},
"aggregate_source": {
"file": "p.rego",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ package regal.rules.idiomatic["no-defined-entrypoint"]

import rego.v1

import data.regal.ast
import data.regal.result
import data.regal.util

# METADATA
# description: |
# collects `entrypoint: true` annotations from any given module
aggregate contains entry if {
some annotation in input.annotations
some annotation in ast.annotations
annotation.entrypoint == true

entry := result.aggregate(rego.metadata.chain(), {"entrypoint": util.to_location_object(annotation.location)})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ report contains violation if {
}

_annotation_at_row(row) := annotation if {
some annotation in input.annotations
some annotation in ast.annotations

util.to_location_object(annotation.location).row == row
}
2 changes: 1 addition & 1 deletion e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func TestTestRegalTestWithExtendedASTTypeChecking(t *testing.T) {

expStart := "1 error occurred: "
expEnd := "rego_type_error: undefined ref: input.foo\n\tinput.foo\n\t ^\n\t " +
"have: \"foo\"\n\t want (one of): [\"annotations\" \"comments\" \"imports\" \"package\" \"regal\" \"rules\"]\n"
"have: \"foo\"\n\t want (one of): [\"comments\" \"imports\" \"package\" \"regal\" \"rules\"]\n"

if !strings.HasPrefix(stderr.String(), expStart) {
t.Errorf("expected stdout error message starting with %q, got %q", expStart, stderr.String())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.5

require (
dario.cat/mergo v1.0.1
github.com/anderseknert/roast v0.2.0
github.com/anderseknert/roast v0.3.0
github.com/coreos/go-semver v0.3.1
github.com/fatih/color v1.17.0
github.com/fsnotify/fsnotify v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0k
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/anderseknert/roast v0.2.0 h1:Hi+noJBe+rLhCzApy8Xo5IE6pkAPBGVFLMJ8EecoghA=
github.com/anderseknert/roast v0.2.0/go.mod h1:+VbTe/Fj1AUhtafMwz9JBdoPqFCS4rkNwsAhJwMgr1Y=
github.com/anderseknert/roast v0.3.0 h1:LJ9zT0yB9xpS01QzR8g2FkP9x8TBPJVvEY08zTypHRs=
github.com/anderseknert/roast v0.3.0/go.mod h1:+VbTe/Fj1AUhtafMwz9JBdoPqFCS4rkNwsAhJwMgr1Y=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
Expand Down
12 changes: 6 additions & 6 deletions internal/embeds/schemas/regal-ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
},
"type": "array"
},
"annotations": {
"items": {
"$ref": "#/$defs/annotations"
},
"type": "array"
},
"rules": {
"items": {
"$ref": "#/$defs/rule"
Expand Down Expand Up @@ -273,6 +267,12 @@
},
"location": {
"$ref": "#/$defs/location"
},
"annotations": {
"items": {
"$ref": "#/$defs/annotations"
},
"type": "array"
}
},
"additionalProperties": false,
Expand Down