|
1 | 1 | use core::fmt; |
2 | 2 | use std::{ |
| 3 | + collections::HashMap, |
3 | 4 | fs::File, |
4 | 5 | path::{Path, PathBuf}, |
5 | 6 | process::Command, |
@@ -280,17 +281,36 @@ fn for_file_codeowners_only(run_config: &RunConfig, file_path: &str) -> RunResul |
280 | 281 | }, |
281 | 282 | } |
282 | 283 | } |
283 | | -pub fn team_for_file_from_codeowners(run_config: &RunConfig, file_path: &str) -> Result<Option<Team>, Error> { |
| 284 | + |
| 285 | +// For an array of file paths, return a map of file path to its owning team |
| 286 | +pub fn teams_for_files_from_codeowners(run_config: &RunConfig, file_paths: &[String]) -> Result<HashMap<String, Team>, Error> { |
| 287 | + let relative_file_paths: Vec<PathBuf> = file_paths |
| 288 | + .iter() |
| 289 | + .map(|path| Path::new(path).strip_prefix(&run_config.project_root).unwrap_or(Path::new(path))) |
| 290 | + .map(|path| path.to_path_buf()) |
| 291 | + .collect(); |
| 292 | + |
| 293 | + let parser = build_codeowners_parser(run_config)?; |
| 294 | + Ok(parser |
| 295 | + .teams_from_files_paths(&relative_file_paths) |
| 296 | + .map_err(|e| Error::Io(e.to_string()))?) |
| 297 | +} |
| 298 | + |
| 299 | +fn build_codeowners_parser(run_config: &RunConfig) -> Result<crate::ownership::codeowners_file_parser::Parser, Error> { |
284 | 300 | let config = config_from_path(&run_config.config_path)?; |
| 301 | + Ok(crate::ownership::codeowners_file_parser::Parser { |
| 302 | + codeowners_file_path: run_config.codeowners_file_path.clone(), |
| 303 | + project_root: run_config.project_root.clone(), |
| 304 | + team_file_globs: config.team_file_glob.clone(), |
| 305 | + }) |
| 306 | +} |
| 307 | + |
| 308 | +pub fn team_for_file_from_codeowners(run_config: &RunConfig, file_path: &str) -> Result<Option<Team>, Error> { |
285 | 309 | let relative_file_path = Path::new(file_path) |
286 | 310 | .strip_prefix(&run_config.project_root) |
287 | 311 | .unwrap_or(Path::new(file_path)); |
288 | 312 |
|
289 | | - let parser = crate::ownership::parser::Parser { |
290 | | - project_root: run_config.project_root.clone(), |
291 | | - codeowners_file_path: run_config.codeowners_file_path.clone(), |
292 | | - team_file_globs: config.team_file_glob.clone(), |
293 | | - }; |
| 313 | + let parser = build_codeowners_parser(run_config)?; |
294 | 314 | Ok(parser |
295 | 315 | .team_from_file_path(Path::new(relative_file_path)) |
296 | 316 | .map_err(|e| Error::Io(e.to_string()))?) |
|
0 commit comments