-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
246 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::{collections::HashMap, path::Path}; | ||
|
||
use super::mapper::{directory_mapper::is_directory_mapper_source, OwnerMatcher, Source, TeamName}; | ||
|
||
#[derive(Debug)] | ||
pub struct Owner { | ||
pub sources: Vec<Source>, | ||
pub team_name: TeamName, | ||
} | ||
|
||
pub struct FileOwnerFinder<'a> { | ||
pub owner_matchers: &'a [OwnerMatcher], | ||
} | ||
|
||
impl<'a> FileOwnerFinder<'a> { | ||
pub fn find(&self, relative_path: &Path) -> Vec<Owner> { | ||
let mut team_sources_map: HashMap<&TeamName, Vec<Source>> = HashMap::new(); | ||
let mut directory_overrider = DirectoryOverrider::default(); | ||
|
||
for owner_matcher in self.owner_matchers { | ||
let (owner, source) = owner_matcher.owner_for(relative_path); | ||
|
||
if let Some(team_name) = owner { | ||
if is_directory_mapper_source(source) { | ||
directory_overrider.process(team_name, source); | ||
} else { | ||
team_sources_map.entry(team_name).or_default().push(source.clone()); | ||
} | ||
} | ||
} | ||
|
||
// Add most specific directory owner if it exists | ||
if let Some((team_name, source)) = directory_overrider.specific_directory_owner() { | ||
team_sources_map.entry(team_name).or_default().push(source.clone()); | ||
} | ||
|
||
team_sources_map | ||
.into_iter() | ||
.map(|(team_name, sources)| Owner { | ||
sources, | ||
team_name: team_name.clone(), | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
/// DirectoryOverrider is used to override the owner of a directory if a more specific directory owner is found. | ||
#[derive(Debug, Default)] | ||
pub struct DirectoryOverrider<'a> { | ||
specific_directory_owner: Option<(&'a TeamName, &'a Source)>, | ||
} | ||
|
||
impl<'a> DirectoryOverrider<'a> { | ||
fn process(&mut self, team_name: &'a TeamName, source: &'a Source) { | ||
if self | ||
.specific_directory_owner | ||
.map_or(true, |(_, current_source)| current_source.len() < source.len()) | ||
{ | ||
self.specific_directory_owner = Some((team_name, source)); | ||
} | ||
} | ||
|
||
fn specific_directory_owner(&self) -> Option<(&TeamName, &Source)> { | ||
self.specific_directory_owner | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_directory_overrider() { | ||
let mut directory_overrider = DirectoryOverrider::default(); | ||
assert_eq!(directory_overrider.specific_directory_owner(), None); | ||
let team_name_1 = "team1".to_string(); | ||
let source_1 = "src/**".to_string(); | ||
directory_overrider.process(&team_name_1, &source_1); | ||
assert_eq!(directory_overrider.specific_directory_owner(), Some((&team_name_1, &source_1))); | ||
|
||
let team_name_longest = "team2".to_string(); | ||
let source_longest = "source/subdir/**".to_string(); | ||
directory_overrider.process(&team_name_longest, &source_longest); | ||
assert_eq!( | ||
directory_overrider.specific_directory_owner(), | ||
Some((&team_name_longest, &source_longest)) | ||
); | ||
|
||
let team_name_3 = "team3".to_string(); | ||
let source_3 = "source/**".to_string(); | ||
directory_overrider.process(&team_name_3, &source_3); | ||
assert_eq!( | ||
directory_overrider.specific_directory_owner(), | ||
Some((&team_name_longest, &source_longest)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Payroll |
1 change: 1 addition & 0 deletions
1
tests/fixtures/invalid_project/ruby/app/services/multi_owned.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @team Payments |
Oops, something went wrong.