Skip to content

Commit d04194d

Browse files
committed
stubbing out teams_for_files_from_codeowners
1 parent 37027e3 commit d04194d

File tree

3 files changed

+30
-112
lines changed

3 files changed

+30
-112
lines changed

src/ownership.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::{
99
};
1010
use tracing::{info, instrument};
1111

12-
pub(crate) mod codeowners_file_parser;
1312
mod file_generator;
1413
mod file_owner_finder;
1514
pub mod for_file_fast;
1615
pub(crate) mod mapper;
16+
pub(crate) mod codeowners_file_parser;
1717
mod validator;
1818

1919
use crate::{
@@ -24,9 +24,9 @@ use crate::{
2424
pub use validator::Errors as ValidatorErrors;
2525

2626
use self::{
27-
codeowners_file_parser::parse_for_team,
2827
file_generator::FileGenerator,
2928
mapper::{JavascriptPackageMapper, Mapper, RubyPackageMapper, TeamFileMapper, TeamGemMapper, TeamGlobMapper, TeamYmlMapper},
29+
codeowners_file_parser::parse_for_team,
3030
validator::Validator,
3131
};
3232

src/ownership/codeowners_file_parser.rs

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::{
44
};
55
use fast_glob::glob_match;
66
use memoize::memoize;
7-
use rayon::prelude::*;
87
use regex::Regex;
98
use std::{
109
collections::HashMap,
@@ -23,57 +22,33 @@ pub struct Parser {
2322
}
2423

2524
impl Parser {
26-
pub fn teams_from_files_paths(&self, file_paths: &[PathBuf]) -> Result<HashMap<String, Option<Team>>, Box<dyn Error>> {
27-
let file_inputs: Vec<(String, String)> = file_paths
28-
.iter()
29-
.map(|path| {
30-
let file_path_str = path
31-
.to_str()
32-
.ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid file path"))?;
33-
let original = file_path_str.to_string();
34-
let prefixed = if file_path_str.starts_with('/') {
35-
original.clone()
36-
} else {
37-
format!("/{}", file_path_str)
38-
};
39-
Ok((original, prefixed))
40-
})
41-
.collect::<Result<Vec<_>, IoError>>()?;
42-
43-
if file_inputs.is_empty() {
44-
return Ok(HashMap::new());
45-
}
46-
47-
let codeowners_entries: Vec<(String, String)> =
48-
build_codeowners_lines_in_priority(self.codeowners_file_path.to_string_lossy().into_owned())
49-
.iter()
50-
.map(|line| {
51-
line.split_once(' ')
52-
.map(|(glob, team_name)| (glob.to_string(), team_name.to_string()))
53-
.ok_or_else(|| IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))
54-
})
55-
.collect::<Result<_, IoError>>()
56-
.map_err(|e| Box::new(e) as Box<dyn Error>)?;
57-
58-
let teams_by_name = teams_by_github_team_name(self.absolute_team_files_globs());
59-
60-
let result: HashMap<String, Option<Team>> = file_inputs
61-
.par_iter()
62-
.map(|(key, prefixed)| {
63-
let team = codeowners_entries
64-
.iter()
65-
.find(|(glob, _)| glob_match(glob, prefixed))
66-
.and_then(|(_, team_name)| teams_by_name.get(team_name).cloned());
67-
(key.clone(), team)
68-
})
69-
.collect();
70-
71-
Ok(result)
25+
pub fn teams_from_files_paths(&self, file_paths: &[PathBuf]) -> Result<HashMap<String, Team>, Box<dyn Error>> {
26+
todo!()
7227
}
73-
28+
7429
pub fn team_from_file_path(&self, file_path: &Path) -> Result<Option<Team>, Box<dyn Error>> {
75-
let teams = self.teams_from_files_paths(&[file_path.to_path_buf()])?;
76-
Ok(teams.get(file_path.to_string_lossy().into_owned().as_str()).cloned().flatten())
30+
let file_path_str = file_path
31+
.to_str()
32+
.ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid file path"))?;
33+
let slash_prefixed = if file_path_str.starts_with("/") {
34+
file_path_str.to_string()
35+
} else {
36+
format!("/{}", file_path_str)
37+
};
38+
39+
let codeowners_lines_in_priorty = build_codeowners_lines_in_priority(self.codeowners_file_path.to_string_lossy().into_owned());
40+
for line in codeowners_lines_in_priorty {
41+
let (glob, team_name) = line
42+
.split_once(' ')
43+
.ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))?;
44+
if glob_match(glob, &slash_prefixed) {
45+
let tbn = teams_by_github_team_name(self.absolute_team_files_globs());
46+
let team: Option<Team> = tbn.get(team_name.to_string().as_str()).cloned();
47+
return Ok(team);
48+
}
49+
}
50+
51+
Ok(None)
7752
}
7853

7954
fn absolute_team_files_globs(&self) -> Vec<String> {

src/runner.rs

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn for_file_codeowners_only(run_config: &RunConfig, file_path: &str) -> RunResul
283283
}
284284

285285
// 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, Option<Team>>, Error> {
286+
pub fn teams_for_files_from_codeowners(run_config: &RunConfig, file_paths: &[String]) -> Result<HashMap<String, Team>, Error> {
287287
let relative_file_paths: Vec<PathBuf> = file_paths
288288
.iter()
289289
.map(|path| Path::new(path).strip_prefix(&run_config.project_root).unwrap_or(Path::new(path)))
@@ -362,9 +362,10 @@ fn for_file_optimized(run_config: &RunConfig, file_path: &str) -> RunResult {
362362
mod tests {
363363
use tempfile::tempdir;
364364

365-
use super::*;
366365
use crate::{common_test, ownership::mapper::Source};
367366

367+
use super::*;
368+
368369
#[test]
369370
fn test_version() {
370371
assert_eq!(version(), env!("CARGO_PKG_VERSION").to_string());
@@ -416,62 +417,4 @@ mod tests {
416417
assert_eq!(team.github_team, "@b");
417418
assert!(team.path.to_string_lossy().ends_with("config/teams/b.yml"));
418419
}
419-
420-
#[test]
421-
fn test_teams_for_files_from_codeowners() {
422-
let project_root = Path::new("tests/fixtures/valid_project");
423-
let file_paths = [
424-
"javascript/packages/items/item.ts",
425-
"config/teams/payroll.yml",
426-
"ruby/app/models/bank_account.rb",
427-
"made/up/file.rb",
428-
"ruby/ignored_files/git_ignored.rb",
429-
];
430-
let run_config = RunConfig {
431-
project_root: project_root.to_path_buf(),
432-
codeowners_file_path: project_root.join(".github/CODEOWNERS").to_path_buf(),
433-
config_path: project_root.join("config/code_ownership.yml").to_path_buf(),
434-
no_cache: false,
435-
};
436-
let teams =
437-
teams_for_files_from_codeowners(&run_config, &file_paths.iter().map(|s| s.to_string()).collect::<Vec<String>>()).unwrap();
438-
assert_eq!(teams.len(), 5);
439-
assert_eq!(
440-
teams
441-
.get("javascript/packages/items/item.ts")
442-
.unwrap()
443-
.as_ref()
444-
.map(|t| t.name.as_str()),
445-
Some("Payroll")
446-
);
447-
assert_eq!(
448-
teams.get("config/teams/payroll.yml").unwrap().as_ref().map(|t| t.name.as_str()),
449-
Some("Payroll")
450-
);
451-
assert_eq!(
452-
teams
453-
.get("ruby/app/models/bank_account.rb")
454-
.unwrap()
455-
.as_ref()
456-
.map(|t| t.name.as_str()),
457-
Some("Payments")
458-
);
459-
assert_eq!(teams.get("made/up/file.rb").unwrap().as_ref().map(|t| t.name.as_str()), None);
460-
assert_eq!(
461-
teams
462-
.get("ruby/ignored_files/git_ignored.rb")
463-
.unwrap()
464-
.as_ref()
465-
.map(|t| t.name.as_str()),
466-
None
467-
);
468-
assert_eq!(
469-
teams
470-
.get("ruby/ignored_files/git_ignored.rb")
471-
.unwrap()
472-
.as_ref()
473-
.map(|t| t.name.as_str()),
474-
None
475-
);
476-
}
477420
}

0 commit comments

Comments
 (0)