Vet condition prior to evaluation

This commit is contained in:
Luc Perkins 2024-06-17 22:59:55 -07:00
parent 65526b1897
commit 0d16f33018
No known key found for this signature in database
GPG Key ID: 16DB1108FB591835
3 changed files with 25 additions and 3 deletions

View File

@ -46,7 +46,11 @@ pub(super) fn evaluate_condition(
}); });
} }
Value::Bool(b) if b => continue, Value::Bool(b) if b => continue,
result => return Err(FlakeCheckerError::InvalidCelCondition(format!("CEL conditions must return a Boolean but your supplied condition returned a {}", result.type_of()))), result => {
return Err(FlakeCheckerError::NonBooleanCondition(
result.type_of().to_string(),
))
}
}, },
Err(e) => return Err(FlakeCheckerError::CelExecution(e)), Err(e) => return Err(FlakeCheckerError::CelExecution(e)),
} }
@ -71,3 +75,19 @@ fn nixpkgs_cel_values(repo: Box<RepoNode>) -> Vec<(&'static str, Value)> {
(KEY_OWNER, Value::from(repo.original.owner)), (KEY_OWNER, Value::from(repo.original.owner)),
] ]
} }
pub(super) fn vet_condition(condition: &str) -> Result<(), FlakeCheckerError> {
let mut ctx = Context::default();
ctx.add_variable_from_value(KEY_SUPPORTED_REFS, Value::List(Vec::<Value>::new().into()));
ctx.add_variable_from_value(KEY_GIT_REF, Value::from("some-ref"));
ctx.add_variable_from_value(KEY_NUM_DAYS_OLD, Value::from(27));
ctx.add_variable_from_value(KEY_OWNER, Value::from("some-og"));
match Program::compile(condition)?.execute(&ctx) {
Ok(value) if matches!(value, Value::Bool(_)) => Ok(()),
Ok(value) => Err(FlakeCheckerError::NonBooleanCondition(
value.type_of().to_string(),
)),
Err(e) => Err(FlakeCheckerError::CelExecution(e)),
}
}

View File

@ -10,8 +10,8 @@ pub enum FlakeCheckerError {
FlakeLock(#[from] parse_flake_lock::FlakeLockParseError), FlakeLock(#[from] parse_flake_lock::FlakeLockParseError),
#[error("http client error: {0}")] #[error("http client error: {0}")]
Http(#[from] reqwest::Error), Http(#[from] reqwest::Error),
#[error("invalid CEL condition: {0}")] #[error("CEL conditions must return a Boolean but returned {0} instead")]
InvalidCelCondition(String), NonBooleanCondition(String),
#[error("couldn't access flake.lock: {0}")] #[error("couldn't access flake.lock: {0}")]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error("couldn't parse flake.lock: {0}")] #[error("couldn't parse flake.lock: {0}")]

View File

@ -7,6 +7,7 @@ mod issue;
mod summary; mod summary;
mod telemetry; mod telemetry;
use condition::vet_condition;
use error::FlakeCheckerError; use error::FlakeCheckerError;
use flake::{check_flake_lock, FlakeCheckConfig}; use flake::{check_flake_lock, FlakeCheckConfig};
use summary::Summary; use summary::Summary;
@ -179,6 +180,7 @@ fn main() -> Result<ExitCode, FlakeCheckerError> {
}; };
let issues = if let Some(condition) = &condition { let issues = if let Some(condition) = &condition {
vet_condition(condition)?;
evaluate_condition(&flake_lock, &nixpkgs_keys, condition, allowed_refs.clone())? evaluate_condition(&flake_lock, &nixpkgs_keys, condition, allowed_refs.clone())?
} else { } else {
check_flake_lock(&flake_lock, &flake_check_config, allowed_refs.clone())? check_flake_lock(&flake_lock, &flake_check_config, allowed_refs.clone())?