Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "codeowners"
version = "0.7.1"
version = "0.7.2"
edition = "2024"
license = "MIT"

Expand Down
30 changes: 30 additions & 0 deletions src/common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ pub mod tests {
)
}

pub fn build_ownership_with_nested_package_codeowners() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
relative_path: "packs/foo/package.yml".to_owned(),
content: "owner: Foo\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/bam/package.yml".to_owned(),
content: "owner: Bam\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/bar/package.yml".to_owned(),
content: "owner: Bar\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/bar/baz/package.yml".to_owned(),
content: "owner: Baz\n".to_owned(),
},
TestProjectFile {
relative_path: "packs/foo/unknown/package.yml".to_owned(),
content: "owner: NoSuchTeam\n".to_owned(),
},
// A sibling whose name starts with `foo`, which `packs/foo` doesn't contain.
TestProjectFile {
relative_path: "packs/foobar/package.yml".to_owned(),
content: "owner: Bar\n".to_owned(),
}
)
}

pub fn build_ownership_with_package_codeowners_with_glob_metacharacters() -> Result<Ownership, Box<dyn Error>> {
ownership!(
TestProjectFile {
Expand Down
65 changes: 58 additions & 7 deletions src/ownership/file_owner_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
sources_by_team.entry(owner_team_name).or_default().push(dir_source);
}

if let Some((owner_team_name, package_source)) = nearest_package_owner(project_root, &relative_file_path, config, teams_by_name) {
for (owner_team_name, package_source) in nearest_package_owners(project_root, &relative_file_path, config, teams_by_name) {
sources_by_team.entry(owner_team_name).or_default().push(package_source);
}

Expand Down Expand Up @@ -267,19 +267,22 @@ fn most_specific_directory_owner(
best
}

fn nearest_package_owner(
// The owners of the packages in the nearest directory with one that names a known team. `validate` matches a
// `package.yml` and a `package.json` in one directory alike, so both are returned.
fn nearest_package_owners(
project_root: &Path,
relative_file_path: &Path,
config: &Config,
teams_by_name: &HashMap<String, Team>,
) -> Option<(String, Source)> {
) -> Vec<(String, Source)> {
let mut current = project_root.join(relative_file_path);
loop {
if !current.pop() {
break;
}
let parent_rel = crate::path_utils::relative_to(project_root, current.as_path());
if let Some(rel_str) = parent_rel.to_str() {
let mut owners = Vec::new();
if glob_list_matches(rel_str, &config.ruby_package_paths) {
let pkg_yml = current.join("package.yml");
if pkg_yml.exists() {
Expand All @@ -288,7 +291,7 @@ fn nearest_package_owner(
if let Some(team) = owner.and_then(|owner| teams_by_name.get(&owner)) {
let package_path = parent_rel.join("package.yml");
let package_glob = directory_glob(rel_str);
return Some((
owners.push((
team.name.clone(),
Source::Package(package_path.to_string_lossy().to_string(), package_glob),
));
Expand All @@ -297,7 +300,7 @@ fn nearest_package_owner(
// validate rejects this package, so don't fall through to an enclosing package's owner.
Err(e) => {
eprintln!("Error reading ruby package: {e:?}, path: {}", pkg_yml.display());
return None;
return Vec::new();
}
}
}
Expand All @@ -310,18 +313,21 @@ fn nearest_package_owner(
{
let package_path = parent_rel.join("package.json");
let package_glob = directory_glob(rel_str);
return Some((
owners.push((
team.name.clone(),
Source::Package(package_path.to_string_lossy().to_string(), package_glob),
));
}
}
if !owners.is_empty() {
return owners;
}
}
if current == project_root {
break;
}
}
None
Vec::new()
}

// removed: use `Source::len()` instead
Expand Down Expand Up @@ -412,6 +418,19 @@ mod tests {
}
}

// The owner `nearest_package_owners` finds, for a project with no directory holding both a `package.yml` and a
// `package.json`.
fn nearest_package_owner(
project_root: &Path,
relative_file_path: &Path,
config: &Config,
teams_by_name: &HashMap<String, Team>,
) -> Option<(String, Source)> {
let mut owners = nearest_package_owners(project_root, relative_file_path, config, teams_by_name);
assert!(owners.len() <= 1, "expected at most one package owner, got {owners:?}");
owners.pop()
}

#[test]
fn test_read_top_of_file_team_parses_at_and_colon_forms() {
let td = tempdir().unwrap();
Expand Down Expand Up @@ -593,6 +612,38 @@ mod tests {
}
}

#[test]
fn test_nearest_package_owners_returns_a_package_yml_and_a_package_json_in_one_directory() {
let td = tempdir().unwrap();
let project_root = td.path();
let config = build_config_for_temp("packs/**/*", "packs/**/*", "vendored");

let pkg = project_root.join("packs/payroll");
std::fs::create_dir_all(&pkg).unwrap();
std::fs::write(pkg.join("package.yml"), "---\nowner: Payroll\n").unwrap();
std::fs::write(pkg.join("package.json"), r#"{"metadata": {"owner": "UX"}}"#).unwrap();

let mut tbn: HashMap<String, Team> = HashMap::new();
for name in ["Payroll", "UX"] {
let t = team_named(name);
tbn.insert(t.name.clone(), t);
}

assert_eq!(
nearest_package_owners(project_root, Path::new("packs/payroll/app/a.rb"), &config, &tbn),
vec![
(
"Payroll".to_string(),
Source::Package("packs/payroll/package.yml".to_string(), "packs/payroll/**/**".to_string())
),
(
"UX".to_string(),
Source::Package("packs/payroll/package.json".to_string(), "packs/payroll/**/**".to_string())
),
]
);
}

#[test]
fn test_nearest_package_owner_describes_a_package_by_the_glob_validate_matches() {
let td = tempdir().unwrap();
Expand Down
Loading
Loading