|
| 1 | +use super::types::Tag; |
| 2 | +use crate::utils::error::{Error, Result}; |
| 3 | +use ignore::overrides::{Override, OverrideBuilder}; |
| 4 | + |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +use super::types::{CodeownersEntry, Owner}; |
| 8 | + |
| 9 | +/// Find both owners and tags for a specific file based on all parsed CODEOWNERS entries |
| 10 | +pub fn find_owners_and_tags_for_file( |
| 11 | + file_path: &Path, entries: &[CodeownersEntry], |
| 12 | +) -> Result<(Vec<Owner>, Vec<Tag>)> { |
| 13 | + // Early return if no entries |
| 14 | + if entries.is_empty() { |
| 15 | + return Ok((Vec::new(), Vec::new())); |
| 16 | + } |
| 17 | + let target_dir = file_path.parent().ok_or_else(|| { |
| 18 | + std::io::Error::new( |
| 19 | + std::io::ErrorKind::InvalidInput, |
| 20 | + "file path has no parent directory", |
| 21 | + ) |
| 22 | + })?; |
| 23 | + |
| 24 | + let mut candidates: Vec<_> = entries |
| 25 | + .iter() |
| 26 | + .filter_map(|entry| { |
| 27 | + let codeowners_dir = match entry.source_file.parent() { |
| 28 | + Some(dir) => dir, |
| 29 | + None => { |
| 30 | + eprintln!( |
| 31 | + "CODEOWNERS entry has no parent directory: {}", |
| 32 | + entry.source_file.display() |
| 33 | + ); |
| 34 | + return None; |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + // Check if the CODEOWNERS directory is an ancestor of the target directory |
| 39 | + if !target_dir.starts_with(codeowners_dir) { |
| 40 | + return None; |
| 41 | + } |
| 42 | + |
| 43 | + // Calculate the depth as the number of components in the relative path from codeowners_dir to target_dir |
| 44 | + let rel_path = match target_dir.strip_prefix(codeowners_dir) { |
| 45 | + Ok(p) => p, |
| 46 | + Err(_) => return None, // Should not happen due to starts_with check |
| 47 | + }; |
| 48 | + let depth = rel_path.components().count(); |
| 49 | + |
| 50 | + // Check if the pattern matches the target file |
| 51 | + let matches = { |
| 52 | + let mut builder = OverrideBuilder::new(codeowners_dir); |
| 53 | + if let Err(e) = builder.add(&entry.pattern) { |
| 54 | + eprintln!( |
| 55 | + "Invalid pattern '{}' in {}: {}", |
| 56 | + entry.pattern, |
| 57 | + entry.source_file.display(), |
| 58 | + e |
| 59 | + ); |
| 60 | + return None; |
| 61 | + } |
| 62 | + let over: Override = match builder.build() { |
| 63 | + Ok(o) => o, |
| 64 | + Err(e) => { |
| 65 | + eprintln!( |
| 66 | + "Failed to build override for pattern '{}': {}", |
| 67 | + entry.pattern, e |
| 68 | + ); |
| 69 | + return None; |
| 70 | + } |
| 71 | + }; |
| 72 | + over.matched(file_path, false).is_whitelist() |
| 73 | + }; |
| 74 | + |
| 75 | + if matches { Some((entry, depth)) } else { None } |
| 76 | + }) |
| 77 | + .collect(); |
| 78 | + |
| 79 | + // Sort the candidates by depth, source file, and line number |
| 80 | + candidates.sort_unstable_by(|a, b| { |
| 81 | + let a_entry = a.0; |
| 82 | + let a_depth = a.1; |
| 83 | + let b_entry = b.0; |
| 84 | + let b_depth = b.1; |
| 85 | + |
| 86 | + // Primary sort by depth (ascending) |
| 87 | + a_depth |
| 88 | + .cmp(&b_depth) |
| 89 | + // Then by source file (to group entries from the same CODEOWNERS file) |
| 90 | + .then_with(|| a_entry.source_file.cmp(&b_entry.source_file)) |
| 91 | + // Then by line number (descending) to prioritize later entries in the same file |
| 92 | + .then_with(|| b_entry.line_number.cmp(&a_entry.line_number)) |
| 93 | + }); |
| 94 | + |
| 95 | + // Extract both owners and tags from the highest priority entry, if any |
| 96 | + Ok(candidates |
| 97 | + .first() |
| 98 | + .map(|(entry, _)| (entry.owners.clone(), entry.tags.clone())) |
| 99 | + .unwrap_or_default()) |
| 100 | +} |
0 commit comments