mirror of
https://github.com/DeterminateSystems/flake-checker.git
synced 2024-08-17 10:00:44 +03:00
Streamline evaluation
This commit is contained in:
parent
380af3cbc0
commit
65526b1897
@ -7,6 +7,11 @@ use crate::{
|
||||
issue::{Issue, IssueKind},
|
||||
};
|
||||
|
||||
const KEY_GIT_REF: &str = "gitRef";
|
||||
const KEY_NUM_DAYS_OLD: &str = "numDaysOld";
|
||||
const KEY_OWNER: &str = "owner";
|
||||
const KEY_SUPPORTED_REFS: &str = "supportedRefs";
|
||||
|
||||
pub(super) fn evaluate_condition(
|
||||
flake_lock: &FlakeLock,
|
||||
nixpkgs_keys: &[String],
|
||||
@ -14,27 +19,25 @@ pub(super) fn evaluate_condition(
|
||||
allowed_refs: Vec<String>,
|
||||
) -> Result<Vec<Issue>, FlakeCheckerError> {
|
||||
let mut issues: Vec<Issue> = vec![];
|
||||
let mut ctx = Context::default();
|
||||
|
||||
let allowed_refs: Value = Value::from(
|
||||
allowed_refs
|
||||
.iter()
|
||||
.map(|r| Value::from(r.to_string()))
|
||||
.collect::<Vec<Value>>(),
|
||||
);
|
||||
|
||||
let deps = nixpkgs_deps(flake_lock, nixpkgs_keys)?;
|
||||
|
||||
for (name, dep) in deps {
|
||||
if let Node::Repo(repo) = dep {
|
||||
let allowed_refs: Value = Value::from(
|
||||
allowed_refs
|
||||
.iter()
|
||||
.map(|r| Value::from(r.to_string()))
|
||||
.collect::<Vec<Value>>(),
|
||||
);
|
||||
|
||||
ctx.add_variable_from_value("supportedRefs", allowed_refs);
|
||||
|
||||
let mut ctx = Context::default();
|
||||
ctx.add_variable_from_value(KEY_SUPPORTED_REFS, allowed_refs.clone());
|
||||
for (k, v) in nixpkgs_cel_values(repo) {
|
||||
ctx.add_variable_from_value(k, v);
|
||||
}
|
||||
|
||||
let program = Program::compile(condition)?;
|
||||
match program.execute(&ctx) {
|
||||
match Program::compile(condition)?.execute(&ctx) {
|
||||
Ok(result) => match result {
|
||||
Value::Bool(b) if !b => {
|
||||
issues.push(Issue {
|
||||
@ -56,15 +59,15 @@ pub(super) fn evaluate_condition(
|
||||
fn nixpkgs_cel_values(repo: Box<RepoNode>) -> Vec<(&'static str, Value)> {
|
||||
vec![
|
||||
(
|
||||
"gitRef",
|
||||
KEY_GIT_REF,
|
||||
repo.original
|
||||
.git_ref
|
||||
.map_or_else(|| Value::Null, Value::from),
|
||||
),
|
||||
(
|
||||
"numDaysOld",
|
||||
KEY_NUM_DAYS_OLD,
|
||||
Value::from(num_days_old(repo.locked.last_modified)),
|
||||
),
|
||||
("owner", Value::from(repo.original.owner)),
|
||||
(KEY_OWNER, Value::from(repo.original.owner)),
|
||||
]
|
||||
}
|
||||
|
13
src/flake.rs
13
src/flake.rs
@ -154,6 +154,15 @@ mod test {
|
||||
0,
|
||||
"has(gitRef) && has(numDaysOld) && has(owner) && has(supportedRefs) && supportedRefs.contains(gitRef) && owner == 'NixOS'",
|
||||
true,
|
||||
), (
|
||||
0,
|
||||
"has(gitRef) && has(numDaysOld) && has(owner) && has(supportedRefs) && supportedRefs.contains(gitRef) && owner != 'NixOS'",
|
||||
false,
|
||||
),
|
||||
(
|
||||
0,
|
||||
"has(gitRef) && has(numDaysOld) && has(owner) && has(supportedRefs) && supportedRefs.contains(gitRef) && owner != 'NixOS'",
|
||||
false,
|
||||
)];
|
||||
|
||||
for (n, condition, expected) in cases {
|
||||
@ -173,9 +182,9 @@ mod test {
|
||||
|
||||
if expected {
|
||||
assert!(result.is_ok());
|
||||
assert!(result.unwrap().len() == 0);
|
||||
assert!(result.unwrap().is_empty());
|
||||
} else {
|
||||
assert!(result.unwrap().len() > 0);
|
||||
assert!(!result.unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user