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