|
1 |
| -use crate::ownership::{FileGenerator, TeamOwnership}; |
| 1 | +use crate::{ |
| 2 | + ownership::{FileGenerator, TeamOwnership}, |
| 3 | + project::Team, |
| 4 | +}; |
2 | 5 | use fast_glob::glob_match;
|
| 6 | +use memoize::memoize; |
3 | 7 | use std::{
|
| 8 | + collections::HashMap, |
4 | 9 | error::Error,
|
5 | 10 | fs,
|
6 | 11 | io::Error as IoError,
|
7 | 12 | path::{Path, PathBuf},
|
8 | 13 | };
|
9 | 14 |
|
10 |
| -pub fn team_name_from_file_path(file_path: &Path, codeowners_file_path: &PathBuf) -> Result<Option<String>, Box<dyn Error>> { |
11 |
| - let file_path_str = file_path |
12 |
| - .to_str() |
13 |
| - .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid file path"))?; |
14 |
| - let slash_prefixed = if file_path_str.starts_with("/") { |
15 |
| - file_path_str.to_string() |
16 |
| - } else { |
17 |
| - format!("/{}", file_path_str) |
18 |
| - }; |
| 15 | +pub struct Parser { |
| 16 | + pub project_root: PathBuf, |
| 17 | + pub codeowners_file_path: PathBuf, |
| 18 | + pub team_file_globs: Vec<String>, |
| 19 | +} |
19 | 20 |
|
20 |
| - let codeowners_lines_in_priorty = build_codeowners_lines_in_priority(codeowners_file_path)?; |
| 21 | +impl Parser { |
| 22 | + pub fn team_from_file_path(&self, file_path: &Path) -> Result<Option<Team>, Box<dyn Error>> { |
| 23 | + let file_path_str = file_path |
| 24 | + .to_str() |
| 25 | + .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid file path"))?; |
| 26 | + let slash_prefixed = if file_path_str.starts_with("/") { |
| 27 | + file_path_str.to_string() |
| 28 | + } else { |
| 29 | + format!("/{}", file_path_str) |
| 30 | + }; |
| 31 | + |
| 32 | + let codeowners_lines_in_priorty = build_codeowners_lines_in_priority(self.codeowners_file_path.to_string_lossy().into_owned()); |
| 33 | + for line in codeowners_lines_in_priorty { |
| 34 | + let (glob, team_name) = line |
| 35 | + .split_once(' ') |
| 36 | + .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))?; |
| 37 | + if glob_match(glob, &slash_prefixed) { |
| 38 | + let tbn = teams_by_github_team_name(self.absolute_team_files_globs()); |
| 39 | + let team: Option<Team> = tbn.get(team_name.to_string().as_str()).cloned(); |
| 40 | + return Ok(team); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Ok(None) |
| 45 | + } |
21 | 46 |
|
22 |
| - for line in codeowners_lines_in_priorty { |
23 |
| - let (glob, team_name) = line |
24 |
| - .split_once(' ') |
25 |
| - .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))?; |
26 |
| - if glob_match(glob, &slash_prefixed) { |
27 |
| - return Ok(Some(team_name.to_string())); |
| 47 | + fn absolute_team_files_globs(&self) -> Vec<String> { |
| 48 | + self.team_file_globs |
| 49 | + .iter() |
| 50 | + .map(|glob| format!("{}/{}", self.project_root.display(), glob)) |
| 51 | + .collect() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[memoize] |
| 56 | +fn teams_by_github_team_name(team_file_glob: Vec<String>) -> HashMap<String, Team> { |
| 57 | + dbg!("in teams_by_github_team_name"); |
| 58 | + let mut teams = HashMap::new(); |
| 59 | + for glob in team_file_glob { |
| 60 | + let paths = glob::glob(&glob).expect("Failed to read glob pattern").filter_map(Result::ok); |
| 61 | + |
| 62 | + for path in paths { |
| 63 | + let team = match Team::from_team_file_path(path) { |
| 64 | + Ok(team) => team, |
| 65 | + Err(e) => { |
| 66 | + eprintln!("Error parsing team file: {}", e); |
| 67 | + continue; |
| 68 | + } |
| 69 | + }; |
| 70 | + teams.insert(team.github_team.clone(), team); |
28 | 71 | }
|
29 | 72 | }
|
30 | 73 |
|
31 |
| - Ok(None) |
| 74 | + teams |
32 | 75 | }
|
33 | 76 |
|
34 |
| -fn build_codeowners_lines_in_priority(codeowners_file_path: &PathBuf) -> Result<Vec<String>, Box<dyn Error>> { |
35 |
| - let codeowners_file = fs::read_to_string(codeowners_file_path)?; |
36 |
| - let stripped_lines = stripped_lines_by_priority(&codeowners_file); |
37 |
| - Ok(stripped_lines) |
| 77 | +#[memoize] |
| 78 | +fn build_codeowners_lines_in_priority(codeowners_file_path: String) -> Vec<String> { |
| 79 | + dbg!("in build_codeowners_lines_in_priority"); |
| 80 | + dbg!(&codeowners_file_path); |
| 81 | + let codeowners_file = match fs::read_to_string(codeowners_file_path) { |
| 82 | + Ok(codeowners_file) => codeowners_file, |
| 83 | + Err(e) => { |
| 84 | + // we can't return the error because it's not clonable |
| 85 | + eprintln!("Error reading codeowners file: {}", e); |
| 86 | + return vec![]; |
| 87 | + } |
| 88 | + }; |
| 89 | + stripped_lines_by_priority(&codeowners_file) |
38 | 90 | }
|
39 | 91 |
|
40 | 92 | fn stripped_lines_by_priority(codeowners_file: &str) -> Vec<String> {
|
|
0 commit comments