|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + cache::Cache, |
| 5 | + config::Config, |
| 6 | + ownership::for_file_fast::find_file_owners, |
| 7 | + project::Project, |
| 8 | + project_builder::ProjectBuilder, |
| 9 | + runner::{RunConfig, RunResult, config_from_path, team_for_file_from_codeowners}, |
| 10 | +}; |
| 11 | + |
| 12 | +pub fn crosscheck_owners(run_config: &RunConfig, cache: &Cache) -> RunResult { |
| 13 | + match do_crosscheck_owners(run_config, cache) { |
| 14 | + Ok(mismatches) if mismatches.is_empty() => RunResult { |
| 15 | + info_messages: vec!["Success! All files match between CODEOWNERS and for-file command.".to_string()], |
| 16 | + ..Default::default() |
| 17 | + }, |
| 18 | + Ok(mismatches) => RunResult { |
| 19 | + validation_errors: mismatches, |
| 20 | + ..Default::default() |
| 21 | + }, |
| 22 | + Err(err) => RunResult { |
| 23 | + io_errors: vec![err], |
| 24 | + ..Default::default() |
| 25 | + }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +fn do_crosscheck_owners(run_config: &RunConfig, cache: &Cache) -> Result<Vec<String>, String> { |
| 30 | + let config = load_config(run_config)?; |
| 31 | + let project = build_project(&config, run_config, cache)?; |
| 32 | + |
| 33 | + let mut mismatches: Vec<String> = Vec::new(); |
| 34 | + for file in &project.files { |
| 35 | + let (codeowners_team, fast_display) = owners_for_file(&file.path, run_config, &config)?; |
| 36 | + let codeowners_display = codeowners_team.clone().unwrap_or_else(|| "Unowned".to_string()); |
| 37 | + if !is_match(codeowners_team.as_deref(), &fast_display) { |
| 38 | + mismatches.push(format_mismatch(&project, &file.path, &codeowners_display, &fast_display)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Ok(mismatches) |
| 43 | +} |
| 44 | + |
| 45 | +fn load_config(run_config: &RunConfig) -> Result<Config, String> { |
| 46 | + config_from_path(&run_config.config_path).map_err(|e| e.to_string()) |
| 47 | +} |
| 48 | + |
| 49 | +fn build_project(config: &Config, run_config: &RunConfig, cache: &Cache) -> Result<Project, String> { |
| 50 | + let mut project_builder = ProjectBuilder::new( |
| 51 | + config, |
| 52 | + run_config.project_root.clone(), |
| 53 | + run_config.codeowners_file_path.clone(), |
| 54 | + cache, |
| 55 | + ); |
| 56 | + project_builder.build().map_err(|e| e.to_string()) |
| 57 | +} |
| 58 | + |
| 59 | +fn owners_for_file(path: &Path, run_config: &RunConfig, config: &Config) -> Result<(Option<String>, String), String> { |
| 60 | + let file_path_str = path.to_string_lossy().to_string(); |
| 61 | + |
| 62 | + let codeowners_team = team_for_file_from_codeowners(run_config, &file_path_str) |
| 63 | + .map_err(|e| e.to_string())? |
| 64 | + .map(|t| t.name); |
| 65 | + |
| 66 | + let fast_owners = find_file_owners(&run_config.project_root, config, Path::new(&file_path_str))?; |
| 67 | + let fast_display = match fast_owners.len() { |
| 68 | + 0 => "Unowned".to_string(), |
| 69 | + 1 => fast_owners[0].team.name.clone(), |
| 70 | + _ => { |
| 71 | + let names: Vec<String> = fast_owners.into_iter().map(|fo| fo.team.name).collect(); |
| 72 | + format!("Multiple: {}", names.join(", ")) |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + Ok((codeowners_team, fast_display)) |
| 77 | +} |
| 78 | + |
| 79 | +fn is_match(codeowners_team: Option<&str>, fast_display: &str) -> bool { |
| 80 | + match (codeowners_team, fast_display) { |
| 81 | + (None, "Unowned") => true, |
| 82 | + (Some(t), fd) if fd == t => true, |
| 83 | + _ => false, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn format_mismatch(project: &Project, file_path: &Path, codeowners_display: &str, fast_display: &str) -> String { |
| 88 | + let rel = project.relative_path(file_path).to_string_lossy().to_string(); |
| 89 | + format!("- {}: CODEOWNERS={} fast={}", rel, codeowners_display, fast_display) |
| 90 | +} |
0 commit comments