use std::path::{Path, PathBuf};
pub use fallow_output::{RoutingFacts, RoutingUnit};
use rustc_hash::FxHashSet;
use fallow_config::ResolvedConfig;
use fallow_engine::churn::{ChurnResult, ChurnWindowUnit, SinceDuration, analyze_churn_cached};
use fallow_engine::health::ownership::{
ContributorId, OwnershipContext, OwnershipWithIdentities, compile_bot_globs,
compute_ownership_with_identities,
};
const AUDIT_CHURN_WINDOW_MONTHS: u64 = 12;
#[must_use]
pub fn audit_churn(root: &Path, config: &ResolvedConfig) -> Option<ChurnResult> {
let since = SinceDuration::relative(
AUDIT_CHURN_WINDOW_MONTHS,
ChurnWindowUnit::Months,
format!("{AUDIT_CHURN_WINDOW_MONTHS} months"),
);
analyze_churn_cached(root, &since, &config.cache_dir, config.no_cache).map(|(churn, _)| churn)
}
#[must_use]
#[expect(
clippy::implicit_hasher,
reason = "callers always pass the audit changed-file FxHashSet; generalizing the hasher adds noise"
)]
pub fn compute_routing(
root: &Path,
config: &ResolvedConfig,
changed_files: &FxHashSet<PathBuf>,
) -> RoutingFacts {
audit_churn(root, config).map_or_else(RoutingFacts::default, |churn| {
compute_routing_from_churn(root, config, changed_files, &churn)
})
}
#[must_use]
#[expect(
clippy::implicit_hasher,
reason = "callers always pass the audit changed-file FxHashSet; generalizing the hasher adds noise"
)]
pub fn compute_routing_from_churn(
root: &Path,
config: &ResolvedConfig,
changed_files: &FxHashSet<PathBuf>,
churn_result: &ChurnResult,
) -> RoutingFacts {
let ownership_cfg = &config.health.ownership;
let Ok(bot_globs) = compile_bot_globs(&ownership_cfg.bot_patterns) else {
return RoutingFacts::default();
};
let codeowners = crate::ownership::load_codeowners(root, config)
.ok()
.flatten();
let now_secs = churn_result.clock.epoch_secs();
let ctx = OwnershipContext {
author_pool: &churn_result.author_pool,
bot_globs: &bot_globs,
codeowners: codeowners.as_ref(),
email_mode: ownership_cfg.email_mode,
now_secs,
};
let reviewer = Reviewer {
ids: fallow_engine::repo_refs::current_user_identities(root),
email: fallow_engine::repo_refs::current_user_email(root),
};
let mut units: Vec<RoutingUnit> = changed_files
.iter()
.filter_map(|abs| route_one(abs, root, churn_result, &ctx, &reviewer))
.collect();
units.sort_by(|a, b| a.file.cmp(&b.file));
RoutingFacts { units }
}
fn expert_is_self(expert: &str, self_ids: &[String]) -> bool {
let normalized = expert.trim_start_matches('@').to_ascii_lowercase();
self_ids
.iter()
.any(|id| id.trim_start_matches('@').to_ascii_lowercase() == normalized)
}
struct Reviewer {
ids: Vec<String>,
email: Option<String>,
}
impl Reviewer {
fn is_contributor(&self, id: ContributorId<'_>, rendered: &str) -> bool {
expert_is_self(rendered, &self.ids)
|| self
.email
.as_deref()
.is_some_and(|email| ContributorId::new(email).same_person(id))
}
}
fn select_experts(owned: &OwnershipWithIdentities<'_>, reviewer: &Reviewer) -> Vec<String> {
let metrics = &owned.metrics;
if let Some(owner) = &metrics.declared_owner {
if expert_is_self(owner, &reviewer.ids) {
return Vec::new();
}
return vec![owner.clone()];
}
let top = (&metrics.top_contributor, owned.top_contributor);
let reviewers = metrics
.suggested_reviewers
.iter()
.zip(owned.suggested_reviewers.iter().copied())
.take(2);
let mut expert: Vec<String> = Vec::new();
for (entry, id) in std::iter::once(top).chain(reviewers) {
if reviewer.is_contributor(id, &entry.identifier) || expert.contains(&entry.identifier) {
continue;
}
expert.push(entry.identifier.clone());
}
expert
}
fn route_one(
abs: &Path,
root: &Path,
churn_result: &ChurnResult,
ctx: &OwnershipContext<'_>,
reviewer: &Reviewer,
) -> Option<RoutingUnit> {
let file_churn = churn_result.files.get(abs)?;
let relative = abs.strip_prefix(root).unwrap_or(abs);
let owned = compute_ownership_with_identities(file_churn, relative, ctx)?;
let expert = select_experts(&owned, reviewer);
if expert.is_empty() {
return None;
}
Some(RoutingUnit {
file: relative.to_string_lossy().replace('\\', "/"),
expert,
bus_factor_one: owned.metrics.bus_factor == 1,
})
}
#[cfg(test)]
mod tests {
use fallow_engine::health::ownership::{ContributorId, OwnershipWithIdentities};
use fallow_output::{
ContributorEntry, ContributorIdentifierFormat, OwnershipMetrics, OwnershipState,
};
fn contributor(id: &str) -> ContributorEntry {
ContributorEntry {
identifier: id.to_string(),
format: ContributorIdentifierFormat::Handle,
share: 1.0,
stale_days: 1,
commits: 5,
}
}
fn metrics(declared: Option<&str>, bus_factor: u32) -> OwnershipMetrics {
OwnershipMetrics {
bus_factor,
contributor_count: 1,
top_contributor: contributor("alice"),
recent_contributors: vec![],
suggested_reviewers: vec![contributor("bob")],
declared_owner: declared.map(str::to_string),
unowned: None,
ownership_state: OwnershipState::Active,
drift: false,
drift_reason: None,
}
}
#[test]
fn current_reviewer_is_excluded_from_routing() {
let self_ids = vec![
"bart".to_string(),
"bart@waardenburg.dev".to_string(),
"Bart Waardenburg".to_string(),
];
assert!(super::expert_is_self("bart", &self_ids));
assert!(super::expert_is_self("Bart", &self_ids));
assert!(super::expert_is_self("@bart", &self_ids));
assert!(super::expert_is_self("bart@waardenburg.dev", &self_ids));
assert!(!super::expert_is_self("alice", &self_ids));
assert!(!super::expert_is_self("@team/ui", &self_ids));
assert!(!super::expert_is_self("bart", &[]));
}
fn owned<'a>(
metrics: OwnershipMetrics,
top: &'a str,
reviewers: &[&'a str],
) -> OwnershipWithIdentities<'a> {
OwnershipWithIdentities {
metrics,
top_contributor: ContributorId::new(top),
suggested_reviewers: reviewers.iter().copied().map(ContributorId::new).collect(),
}
}
fn reviewer(ids: &[&str], email: Option<&str>) -> super::Reviewer {
super::Reviewer {
ids: ids.iter().map(|id| (*id).to_string()).collect(),
email: email.map(str::to_string),
}
}
#[test]
fn declared_owner_wins() {
let owned = owned(
metrics(Some("@team/web"), 3),
"alice@x.test",
&["bob@x.test"],
);
assert_eq!(
super::select_experts(&owned, &reviewer(&[], None)),
vec!["@team/web".to_string()]
);
}
#[test]
fn falls_back_to_git_contributors() {
let owned = owned(metrics(None, 2), "alice@x.test", &["bob@x.test"]);
assert_eq!(
super::select_experts(&owned, &reviewer(&[], None)),
vec!["alice".to_string(), "bob".to_string()]
);
}
#[test]
fn anonymized_reviewer_is_not_routed_to_self() {
let mut m = metrics(None, 1);
m.top_contributor = contributor("xxh3:0123456789abcdef");
m.suggested_reviewers = vec![];
let experts = super::select_experts(
&owned(m, "bart@corp.com", &[]),
&reviewer(&["bart", "bart@corp.com"], Some("bart@corp.com")),
);
assert!(experts.is_empty(), "{experts:?}");
}
#[test]
fn reviewer_with_a_second_email_is_not_routed_to_self() {
let mut m = metrics(None, 1);
m.top_contributor = contributor("bart~corp.com");
m.suggested_reviewers = vec![contributor("bart~gmail.com")];
let experts = super::select_experts(
&owned(m, "bart@corp.com", &["bart@gmail.com"]),
&reviewer(&["bart", "bart@gmail.com"], Some("bart@gmail.com")),
);
assert!(experts.is_empty(), "{experts:?}");
}
#[test]
fn other_contributor_stays_an_expert_in_each_mode() {
let me = reviewer(&["bart", "bart@corp.com"], Some("bart@corp.com"));
for identifier in ["alice", "alice@corp.com", "xxh3:00000000000000aa"] {
let mut m = metrics(None, 1);
m.top_contributor = contributor(identifier);
m.suggested_reviewers = vec![];
assert_eq!(
super::select_experts(&owned(m, "alice@corp.com", &[]), &me),
vec![identifier.to_string()]
);
}
}
#[test]
fn declared_owner_that_is_the_reviewer_is_dropped() {
let me = reviewer(&["bart"], None);
let own = owned(metrics(Some("@bart"), 1), "alice@x.test", &["bob@x.test"]);
assert!(super::select_experts(&own, &me).is_empty());
let team = owned(
metrics(Some("@team/web"), 1),
"alice@x.test",
&["bob@x.test"],
);
assert_eq!(
super::select_experts(&team, &me),
vec!["@team/web".to_string()]
);
}
}