Skip to content

Commit 9d82508

Browse files
committed
imp(common): fetch owners/tags functions
1 parent 8e1ddc6 commit 9d82508

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

core/src/cache.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::io::{Read, Write};
22
use std::path::{Path, PathBuf};
33

4-
use crate::common::{find_owners_for_file, find_tags_for_file};
4+
use crate::common::{collect_owners, collect_tags, find_owners_for_file, find_tags_for_file};
55
use crate::types::{CacheEncoding, CodeownersCache, CodeownersEntry, FileEntry};
66
use utils::error::{Error, Result};
77

88
/// Create a cache from parsed CODEOWNERS entries and files
99
pub fn build_cache(entries: Vec<CodeownersEntry>, files: Vec<PathBuf>) -> Result<CodeownersCache> {
1010
let mut file_entries = Vec::new();
11-
let owners_map = std::collections::HashMap::new();
12-
let tags_map = std::collections::HashMap::new();
11+
let mut owners_map = std::collections::HashMap::new();
12+
let mut tags_map = std::collections::HashMap::new();
1313

1414
// Process each file to find owners and tags
1515
for file_path in files {
@@ -25,6 +25,28 @@ pub fn build_cache(entries: Vec<CodeownersEntry>, files: Vec<PathBuf>) -> Result
2525
file_entries.push(file_entry);
2626
}
2727

28+
// Process each owner
29+
let owners = collect_owners(&entries);
30+
owners.iter().for_each(|owner| {
31+
let paths = owners_map.entry(owner.clone()).or_insert_with(Vec::new);
32+
for file_entry in &file_entries {
33+
if file_entry.owners.contains(owner) {
34+
paths.push(file_entry.path.clone());
35+
}
36+
}
37+
});
38+
39+
// Process each tag
40+
let tags = collect_tags(&entries);
41+
tags.iter().for_each(|tag| {
42+
let paths = tags_map.entry(tag.clone()).or_insert_with(Vec::new);
43+
for file_entry in &file_entries {
44+
if file_entry.tags.contains(tag) {
45+
paths.push(file_entry.path.clone());
46+
}
47+
}
48+
});
49+
2850
Ok(CodeownersCache {
2951
entries,
3052
files: file_entries,

core/src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn codeowners_parse(
3939
//dbg!(&files);
4040
let cache = build_cache(parsed_codeowners, files)?;
4141

42-
//store_cache(&cache, cache_file.unwrap(), encoding)?;
42+
store_cache(&cache, cache_file.unwrap(), encoding)?;
4343

4444
let cache = load_cache(cache_file.unwrap())?;
4545

core/src/common.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ignore::{
55
use std::path::{Path, PathBuf};
66
use utils::error::{Error, Result};
77

8-
use crate::types::{CodeownersEntry, Owner, OwnerType, Tag};
8+
use crate::types::{CodeownersEntry, FileEntry, Owner, OwnerType, Tag};
99

1010
/// Find CODEOWNERS files recursively in the given directory and its subdirectories
1111
pub fn find_codeowners_files<P: AsRef<Path>>(base_path: P) -> Result<Vec<PathBuf>> {
@@ -313,6 +313,50 @@ pub fn find_tags_for_file(file_path: &Path, entries: &[CodeownersEntry]) -> Resu
313313
.unwrap_or_default())
314314
}
315315

316+
/// Find all files owned by a specific owner
317+
pub fn find_files_for_owner(files: &[FileEntry], owner: &Owner) -> Vec<PathBuf> {
318+
files
319+
.iter()
320+
.filter(|file_entry| file_entry.owners.contains(owner))
321+
.map(|file_entry| file_entry.path.clone())
322+
.collect()
323+
}
324+
325+
/// Find all files tagged with a specific tag
326+
pub fn find_files_for_tag(files: &[FileEntry], tag: &Tag) -> Vec<PathBuf> {
327+
files
328+
.iter()
329+
.filter(|file_entry| file_entry.tags.contains(tag))
330+
.map(|file_entry| file_entry.path.clone())
331+
.collect()
332+
}
333+
334+
/// Collect all unique owners from CODEOWNERS entries
335+
pub fn collect_owners(entries: &[CodeownersEntry]) -> Vec<Owner> {
336+
let mut owners = std::collections::HashSet::new();
337+
338+
for entry in entries {
339+
for owner in &entry.owners {
340+
owners.insert(owner.clone());
341+
}
342+
}
343+
344+
owners.into_iter().collect()
345+
}
346+
347+
/// Collect all unique tags from CODEOWNERS entries
348+
pub fn collect_tags(entries: &[CodeownersEntry]) -> Vec<Tag> {
349+
let mut tags = std::collections::HashSet::new();
350+
351+
for entry in entries {
352+
for tag in &entry.tags {
353+
tags.insert(tag.clone());
354+
}
355+
}
356+
357+
tags.into_iter().collect()
358+
}
359+
316360
#[cfg(test)]
317361
mod tests {
318362
use super::*;

0 commit comments

Comments
 (0)