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