Skip to content

Commit bee9414

Browse files
committed
memoize codeowners
1 parent f708aa9 commit bee9414

9 files changed

+331
-63
lines changed

Cargo.lock

+87-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ clap_derive = "4.5.18"
1515
error-stack = "0.5.0"
1616
enum_dispatch = "0.3.13"
1717
fast-glob = "0.4.0"
18+
glob = "0.3.2"
1819
ignore = "0.4.23"
1920
itertools = "0.14.0"
2021
lazy_static = "1.5.0"
22+
memoize = "0.5.1"
2123
path-clean = "1.0.1"
2224
rayon = "1.10.0"
2325
regex = "1.11.1"
@@ -30,7 +32,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
3032

3133
[dev-dependencies]
3234
assert_cmd = "2.0.16"
33-
glob = "0.3.1"
3435
rusty-hook = "^0.11.2"
3536
predicates = "3.1.2"
3637
pretty_assertions = "1.4.1" # Shows a more readable diff when comparing objects

src/cli.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ use std::path::{Path, PathBuf};
99
enum Command {
1010
#[clap(about = "Finds the owner of a given file.", visible_alias = "f")]
1111
ForFile {
12-
#[arg(short, long, default_value = "false")]
13-
verbose: bool,
12+
#[arg(
13+
short,
14+
long,
15+
default_value = "false",
16+
help = "Find the owner from the CODEOWNERS file and just return the team name and yml path"
17+
)]
18+
fast: bool,
1419
name: String,
1520
},
1621

@@ -98,7 +103,7 @@ pub fn cli() -> Result<RunResult, RunnerError> {
98103
Command::Validate => runner::validate(&run_config, vec![]),
99104
Command::Generate => runner::generate(&run_config),
100105
Command::GenerateAndValidate => runner::generate_and_validate(&run_config, vec![]),
101-
Command::ForFile { name, verbose } => runner::for_file(&run_config, &name, verbose),
106+
Command::ForFile { name, fast } => runner::for_file(&run_config, &name, fast),
102107
Command::ForTeam { name } => runner::for_team(&run_config, &name),
103108
Command::DeleteCache => runner::delete_cache(&run_config),
104109
};

src/config.rs

+59-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ pub struct Config {
77
#[serde(default = "ruby_package_paths")]
88
pub ruby_package_paths: Vec<String>,
99

10-
#[serde(alias = "js_package_paths")]
10+
#[serde(alias = "js_package_paths", default = "javascript_package_paths")]
1111
pub javascript_package_paths: Vec<String>,
1212

1313
#[serde(default = "team_file_glob")]
1414
pub team_file_glob: Vec<String>,
15+
16+
#[serde(default = "unowned_globs")]
1517
pub unowned_globs: Vec<String>,
1618

17-
#[serde(alias = "unbuilt_gems_path")]
19+
#[serde(alias = "unbuilt_gems_path", default = "vendored_gems_path")]
1820
pub vendored_gems_path: String,
1921

2022
#[serde(default = "default_cache_directory")]
@@ -39,3 +41,58 @@ fn team_file_glob() -> Vec<String> {
3941
fn default_cache_directory() -> String {
4042
String::from("tmp/cache/codeowners")
4143
}
44+
45+
fn javascript_package_paths() -> Vec<String> {
46+
vec!["frontend/**/*".to_owned()]
47+
}
48+
49+
fn unowned_globs() -> Vec<String> {
50+
vec![
51+
"frontend/**/node_modules/**/*".to_owned(),
52+
"frontend/**/__generated__/**/*".to_owned(),
53+
]
54+
}
55+
56+
fn vendored_gems_path() -> String {
57+
"vendored/".to_string()
58+
}
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use std::{
63+
error::Error,
64+
fs::{self, File},
65+
};
66+
67+
use indoc::indoc;
68+
use tempfile::tempdir;
69+
70+
use super::*;
71+
72+
#[test]
73+
fn test_parse_config() -> Result<(), Box<dyn Error>> {
74+
let temp_dir = tempdir()?;
75+
let config_path = temp_dir.path().join("config.yml");
76+
let config_str = indoc! {"
77+
---
78+
owned_globs:
79+
- \"{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}\"
80+
"};
81+
fs::write(&config_path, config_str)?;
82+
let config_file = File::open(&config_path)?;
83+
let config: Config = serde_yaml::from_reader(config_file)?;
84+
assert_eq!(
85+
config.owned_globs,
86+
vec!["{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}"]
87+
);
88+
assert_eq!(config.ruby_package_paths, vec!["packs/**/*", "components/**"]);
89+
assert_eq!(config.javascript_package_paths, vec!["frontend/**/*"]);
90+
assert_eq!(config.team_file_glob, vec!["config/teams/**/*.yml"]);
91+
assert_eq!(
92+
config.unowned_globs,
93+
vec!["frontend/**/node_modules/**/*", "frontend/**/__generated__/**/*"]
94+
);
95+
assert_eq!(config.vendored_gems_path, "vendored/");
96+
Ok(())
97+
}
98+
}

src/ownership.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use mapper::{OwnerMatcher, Source, TeamName};
44
use std::{
55
error::Error,
66
fmt::{self, Display},
7-
path::{Path, PathBuf},
7+
path::Path,
88
sync::Arc,
99
};
1010
use tracing::{info, instrument};
1111

1212
mod file_generator;
1313
mod file_owner_finder;
1414
pub(crate) mod mapper;
15-
mod parser;
15+
pub(crate) mod parser;
1616
mod validator;
1717

1818
use crate::{
@@ -178,10 +178,6 @@ impl Ownership {
178178
}
179179
}
180180

181-
pub fn fast_team_name_from_file_path(file_path: &str, code_owners_file_path: &PathBuf) -> Result<Option<String>, Box<dyn Error>> {
182-
parser::team_name_from_file_path(Path::new(file_path), code_owners_file_path)
183-
}
184-
185181
#[cfg(test)]
186182
mod tests {
187183
use super::*;

0 commit comments

Comments
 (0)