use crate::candidate_record::CandidateChange;
use crate::candidate_version::CandidateVersion;
use crate::event_path::EventPath;
use crate::policy_content::{ContentState, LifecycleContent};
use crate::policy_engine::{PolicyFinding, PolicyOutcome};
use crate::repository_facts::RepositoryFacts;
use std::rc::Rc;
pub const ROOT_CONTEXT_PATH: &str = "CONTEXT.md";
pub const ROOT_CONTEXT_CODE: &str = "root-context-forbidden";
pub const ROOT_CONTEXT_MESSAGE: &str = "Root CONTEXT.md is forbidden; read source code directly.";
pub fn check_root_context<F: RepositoryFacts>(
content: &mut ContentState,
lifecycle: &LifecycleContent,
facts: &mut F,
) -> PolicyOutcome {
let version: Rc<CandidateVersion> = match content.version(lifecycle, facts) {
Ok(Some(found)) => found,
Ok(None) => return PolicyOutcome::Findings(Vec::new()),
Err(outcome) => return outcome,
};
if let Some(candidate) = version.candidate_at_path(ROOT_CONTEXT_PATH.as_bytes())
&& candidate.change != CandidateChange::Deleted
{
return PolicyOutcome::Findings(vec![PolicyFinding {
code: ROOT_CONTEXT_CODE,
message: String::from(ROOT_CONTEXT_MESSAGE),
path: Some(EventPath::from_git_bytes(ROOT_CONTEXT_PATH.as_bytes())),
location: None,
fix_available: false,
}]);
}
return PolicyOutcome::Findings(Vec::new());
}
#[cfg(test)]
#[path = "policy_root_context_tests.rs"]
mod tests;