|
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 | | - }; |
19 | | - |
20 | | - let codeowners_lines_in_priorty = build_codeowners_lines_in_priority(codeowners_file_path)?; |
21 | | - |
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())); |
| 15 | +pub struct Parser { |
| 16 | + pub project_root: PathBuf, |
| 17 | + pub codeowners_file_path: PathBuf, |
| 18 | + pub team_file_globs: Vec<String>, |
| 19 | +} |
| 20 | + |
| 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 | + } |
| 46 | + |
| 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 | + let mut teams = HashMap::new(); |
| 58 | + for glob in team_file_glob { |
| 59 | + let paths = glob::glob(&glob).expect("Failed to read glob pattern").filter_map(Result::ok); |
| 60 | + |
| 61 | + for path in paths { |
| 62 | + let team = Team::from_team_file_path(path).unwrap(); |
| 63 | + teams.insert(team.github_team.clone(), team); |
28 | 64 | } |
29 | 65 | } |
30 | 66 |
|
31 | | - Ok(None) |
| 67 | + teams |
32 | 68 | } |
33 | 69 |
|
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) |
| 70 | +#[memoize] |
| 71 | +fn build_codeowners_lines_in_priority(codeowners_file_path: String) -> Vec<String> { |
| 72 | + let codeowners_file = fs::read_to_string(codeowners_file_path).unwrap(); |
| 73 | + stripped_lines_by_priority(&codeowners_file) |
38 | 74 | } |
39 | 75 |
|
40 | 76 | fn stripped_lines_by_priority(codeowners_file: &str) -> Vec<String> { |
|
0 commit comments