Skip to content
Closed
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
36 changes: 36 additions & 0 deletions aap_policy_examples/project_scm_branch_validation.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package aap_policy_examples

import rego.v1

# Define allowed values for project.scm_branch
valid_project_scm_branch_values := ["main", "v1"]

# Default policy response indicating allowed status with no violations
default project_scm_branch_validation := {
"allowed": true,
"violations": [],
}

# Evaluate branch_validation to check if project.scm_branch value is allowed
project_scm_branch_validation := result if {
# Extract project.scm_branch from input
branch := object.get(input, ["project", "scm_branch"], "")

# Check if branch value is not in the allowed list
not allowed_branch(branch)

result := {
"allowed": false,
"violations": [sprintf("Invalid branch: %v. Only named 'main' or 'v1' branches are allowed.", [branch])],
}
}

# Check if a given branch value is allowed
allowed_branch(branch) if {
branch == ""
}

allowed_branch(branch) if {
some allowed_value in valid_project_scm_branch_values
branch == allowed_value
}
39 changes: 39 additions & 0 deletions test_aap_policy_examples/project_scm_branch_validation_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test_aap_policy_examples

import data.aap_policy_examples

test_valid_main_branch_allowed if {
test_input := {
"project": {
"scm_branch": "main"
}
}
aap_policy_examples.project_scm_branch_validation.allowed == true with input as test_input
}

test_valid_v1_branch_allowed if {
test_input := {
"project": {
"scm_branch": "v1"
}
}
aap_policy_examples.project_scm_branch_validation.allowed == true with input as test_input
}

test_invalid_branch_blocked if {
test_input := {
"project": {
"scm_branch": "develop"
}
}
aap_policy_examples.project_scm_branch_validation.allowed == false with input as test_input
}

test_invalid_branch_violation_message if {
test_input := {
"project": {
"scm_branch": "develop"
}
}
aap_policy_examples.project_scm_branch_validation.violations[0] == "Invalid branch: develop. Only named 'main' or 'v1' branches are allowed." with input as test_input
}