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.3.0"
version = "0.3.1"
edition = "2024"

[profile.release]
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ codeowners gv --no-cache
- `vendored_gems_path` (default: `'vendored/'`)
- `cache_directory` (default: `'tmp/cache/codeowners'`)
- `ignore_dirs` (default includes: `.git`, `node_modules`, `tmp`, etc.)
- `executable_name` (default: `'codeowners'`): Customize the command name shown in validation error messages. Useful when using `codeowners-rs` via wrappers like the [code_ownership](https://github.com/rubyatscale/code_ownership) Ruby gem.

Example configuration with custom executable name:

```yaml
owned_globs:
- '{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx}'
executable_name: 'bin/codeownership' # For Ruby gem wrapper
```

See examples in `tests/fixtures/**/config/` for reference setups.

Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn cli() -> Result<RunResult, RunnerError> {
codeowners_file_path,
project_root,
no_cache: args.no_cache,
executable_name: None,
};

let runner_result = match args.command {
Expand Down
41 changes: 41 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct Config {

#[serde(default = "default_ignore_dirs")]
pub ignore_dirs: Vec<String>,

#[serde(default = "default_executable_name")]
pub executable_name: String,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -61,6 +64,10 @@ fn vendored_gems_path() -> String {
"vendored/".to_string()
}

fn default_executable_name() -> String {
"codeowners".to_string()
}

fn default_ignore_dirs() -> Vec<String> {
vec![
".cursor".to_owned(),
Expand Down Expand Up @@ -121,6 +128,40 @@ mod tests {
vec!["frontend/**/node_modules/**/*", "frontend/**/__generated__/**/*"]
);
assert_eq!(config.vendored_gems_path, "vendored/");
assert_eq!(config.executable_name, "codeowners");
Ok(())
}

#[test]
fn test_parse_config_with_custom_executable_name() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"**/*.rb\"
executable_name: my-custom-codeowners
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(config.executable_name, "my-custom-codeowners");
Ok(())
}

#[test]
fn test_executable_name_defaults_when_not_specified() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"**/*.rb\"
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(config.executable_name, "codeowners");
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/crosscheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
ownership::file_owner_resolver::find_file_owners,
project::Project,
project_builder::ProjectBuilder,
runner::{RunConfig, RunResult, config_from_path, team_for_file_from_codeowners},
runner::{RunConfig, RunResult, config_from_run_config, team_for_file_from_codeowners},
};

pub fn crosscheck_owners(run_config: &RunConfig, cache: &Cache) -> RunResult {
Expand Down Expand Up @@ -43,7 +43,7 @@ fn do_crosscheck_owners(run_config: &RunConfig, cache: &Cache) -> Result<Vec<Str
}

fn load_config(run_config: &RunConfig) -> Result<Config, String> {
config_from_path(&run_config.config_path).map_err(|e| e.to_string())
config_from_run_config(run_config).map_err(|e| e.to_string())
}

fn build_project(config: &Config, run_config: &RunConfig, cache: &Cache) -> Result<Project, String> {
Expand Down
1 change: 1 addition & 0 deletions src/ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl Ownership {
project: self.project.clone(),
mappers: self.mappers(),
file_generator: FileGenerator { mappers: self.mappers() },
executable_name: self.project.executable_name.clone(),
};

validator.validate()
Expand Down
1 change: 1 addition & 0 deletions src/ownership/file_owner_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ mod tests {
vendored_gems_path: vendored_path.to_string(),
cache_directory: "tmp/cache/codeowners".to_string(),
ignore_dirs: vec![],
executable_name: "codeowners".to_string(),
}
}

Expand Down
25 changes: 15 additions & 10 deletions src/ownership/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ pub struct Validator {
pub project: Arc<Project>,
pub mappers: Vec<Box<dyn Mapper>>,
pub file_generator: FileGenerator,
pub executable_name: String,
}

#[derive(Debug)]
enum Error {
InvalidTeam { name: String, path: PathBuf },
FileWithoutOwner { path: PathBuf },
FileWithMultipleOwners { path: PathBuf, owners: Vec<Owner> },
CodeownershipFileIsStale,
CodeownershipFileIsStale { executable_name: String },
}

#[derive(Debug)]
Expand Down Expand Up @@ -130,12 +131,16 @@ impl Validator {
match self.project.get_codeowners_file() {
Ok(current_file) => {
if generated_file != current_file {
vec![Error::CodeownershipFileIsStale]
vec![Error::CodeownershipFileIsStale {
executable_name: self.executable_name.to_string(),
}]
} else {
vec![]
}
}
Err(_) => vec![Error::CodeownershipFileIsStale], // Treat any read error as stale file
Err(_) => vec![Error::CodeownershipFileIsStale {
executable_name: self.executable_name.to_string(),
}],
}
}

Expand All @@ -161,13 +166,13 @@ impl Validator {
impl Error {
pub fn category(&self) -> String {
match self {
Error::FileWithoutOwner { path: _ } => "Some files are missing ownership".to_owned(),
Error::FileWithMultipleOwners { path: _, owners: _ } => "Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways".to_owned(),
Error::CodeownershipFileIsStale => {
"CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file".to_owned()
Error::FileWithoutOwner { path: _ } => "Some files are missing ownership".to_owned(),
Error::FileWithMultipleOwners { path: _, owners: _ } => "Code ownership should only be defined for each file in one way. The following files have declared ownership in multiple ways".to_owned(),
Error::CodeownershipFileIsStale { executable_name } => {
format!("CODEOWNERS out of date. Run `{} generate` to update the CODEOWNERS file", executable_name)
}
Error::InvalidTeam { name: _, path: _ } => "Found invalid team annotations".to_owned(),
}
Error::InvalidTeam { name: _, path: _ } => "Found invalid team annotations".to_owned(),
}
}

pub fn messages(&self) -> Vec<String> {
Expand All @@ -187,7 +192,7 @@ impl Error {

vec![messages.join("\n")]
}
Error::CodeownershipFileIsStale => vec![],
Error::CodeownershipFileIsStale { executable_name: _ } => vec![],
Error::InvalidTeam { name, path } => vec![format!("- {} is referencing an invalid team - '{}'", path.to_string_lossy(), name)],
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Project {
pub codeowners_file_path: PathBuf,
pub directory_codeowner_files: Vec<DirectoryCodeownersFile>,
pub teams_by_name: HashMap<String, Team>,
pub executable_name: String,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -219,6 +220,7 @@ mod tests {
codeowners_file_path: PathBuf::from(".github/CODEOWNERS"),
directory_codeowner_files: vec![],
teams_by_name: HashMap::new(),
executable_name: "codeowners".to_string(),
};

let map = project.vendored_gem_by_name();
Expand Down
1 change: 1 addition & 0 deletions src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl<'a> ProjectBuilder<'a> {
codeowners_file_path: self.codeowners_file_path.to_path_buf(),
directory_codeowner_files: directory_codeowners,
teams_by_name,
executable_name: self.config.executable_name.clone(),
})
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::Path, process::Command};
use std::process::Command;

use error_stack::{Result, ResultExt};
use serde::Serialize;
Expand Down Expand Up @@ -44,15 +44,20 @@ where
runnable(runner)
}

pub(crate) fn config_from_path(path: &Path) -> Result<Config, Error> {
match crate::config::Config::load_from_path(path) {
Ok(c) => Ok(c),
pub(crate) fn config_from_run_config(run_config: &RunConfig) -> Result<Config, Error> {
match crate::config::Config::load_from_path(&run_config.config_path) {
Ok(mut c) => {
if let Some(executable_name) = &run_config.executable_name {
c.executable_name = executable_name.clone();
}
Ok(c)
}
Err(msg) => Err(error_stack::Report::new(Error::Io(msg))),
}
}
impl Runner {
pub fn new(run_config: &RunConfig) -> Result<Self, Error> {
let config = config_from_path(&run_config.config_path)?;
let config = config_from_run_config(run_config)?;

let cache: Cache = if run_config.no_cache {
NoopCache::default().into()
Expand Down
10 changes: 5 additions & 5 deletions src/runner/api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;

use crate::ownership::FileOwner;
use crate::project::Team;
use crate::{ownership::FileOwner, runner::config_from_run_config};

use super::{Error, ForFileResult, RunConfig, RunResult, config_from_path, run};
use super::{Error, ForFileResult, RunConfig, RunResult, run};

pub fn for_file(run_config: &RunConfig, file_path: &str, from_codeowners: bool, json: bool) -> RunResult {
if from_codeowners {
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn crosscheck_owners(run_config: &RunConfig) -> RunResult {

// Returns all owners for a file without creating a Runner (performance optimized)
pub fn owners_for_file(run_config: &RunConfig, file_path: &str) -> error_stack::Result<Vec<FileOwner>, Error> {
let config = config_from_path(&run_config.config_path)?;
let config = config_from_run_config(run_config)?;
use crate::ownership::file_owner_resolver::find_file_owners;
let owners = find_file_owners(&run_config.project_root, &config, std::path::Path::new(file_path)).map_err(Error::Io)?;
Ok(owners)
Expand All @@ -60,7 +60,7 @@ pub fn teams_for_files_from_codeowners(
run_config: &RunConfig,
file_paths: &[String],
) -> error_stack::Result<HashMap<String, Option<Team>>, Error> {
let config = config_from_path(&run_config.config_path)?;
let config = config_from_run_config(run_config)?;
let res = crate::ownership::codeowners_query::teams_for_files_from_codeowners(
&run_config.project_root,
&run_config.codeowners_file_path,
Expand All @@ -80,7 +80,7 @@ pub fn team_for_file_from_codeowners(run_config: &RunConfig, file_path: &str) ->

// Fast path that avoids creating a full Runner for single file queries
fn for_file_optimized(run_config: &RunConfig, file_path: &str, json: bool) -> RunResult {
let config = match config_from_path(&run_config.config_path) {
let config = match config_from_run_config(run_config) {
Ok(c) => c,
Err(err) => {
return RunResult::from_io_error(Error::Io(err.to_string()), json);
Expand Down
1 change: 1 addition & 0 deletions src/runner/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct RunConfig {
pub codeowners_file_path: PathBuf,
pub config_path: PathBuf,
pub no_cache: bool,
pub executable_name: Option<String>,
}

#[derive(Debug, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub fn build_run_config(project_root: &Path, codeowners_rel_path: &str) -> RunCo
codeowners_file_path,
config_path,
no_cache: true,
executable_name: None,
}
}

Expand Down
67 changes: 67 additions & 0 deletions tests/executable_name_config_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use indoc::indoc;
use predicates::prelude::*;
use std::error::Error;

mod common;
use common::OutputStream;
use common::run_codeowners;

#[test]
fn test_validate_with_custom_executable_name() -> Result<(), Box<dyn Error>> {
// When executable_name is configured, error should show that command
run_codeowners(
"custom_executable_name",
&["validate"],
false,
OutputStream::Stdout,
predicate::str::contains("Run `bin/codeownership generate`"),
)?;
Ok(())
}

#[test]
fn test_validate_with_default_executable_name() -> Result<(), Box<dyn Error>> {
// When executable_name is not configured, error should show default "codeowners"
run_codeowners(
"default_executable_name",
&["validate"],
false,
OutputStream::Stdout,
predicate::str::contains("Run `codeowners generate`"),
)?;
Ok(())
}

#[test]
fn test_custom_executable_name_full_error_message() -> Result<(), Box<dyn Error>> {
// Verify the complete error message format with custom executable
run_codeowners(
"custom_executable_name",
&["validate"],
false,
OutputStream::Stdout,
predicate::eq(indoc! {"

CODEOWNERS out of date. Run `bin/codeownership generate` to update the CODEOWNERS file

"}),
)?;
Ok(())
}

#[test]
fn test_default_executable_name_full_error_message() -> Result<(), Box<dyn Error>> {
// Verify the complete error message format with default executable
run_codeowners(
"default_executable_name",
&["validate"],
false,
OutputStream::Stdout,
predicate::eq(indoc! {"

CODEOWNERS out of date. Run `codeowners generate` to update the CODEOWNERS file

"}),
)?;
Ok(())
}
10 changes: 10 additions & 0 deletions tests/fixtures/custom_executable_name/.github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# STOP! - DO NOT EDIT THIS FILE MANUALLY
# This file was automatically generated by "codeowners".
#
# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
# teams. This is useful when developers create Pull Requests since the
# code/file owner is notified. Reference GitHub docs for more details:
# https://help.github.com/en/articles/about-code-owners

# Outdated content to trigger validation error
/app/old.rb @FooTeam
3 changes: 3 additions & 0 deletions tests/fixtures/custom_executable_name/app/foo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @team Foo
puts 'foo'

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
owned_globs:
- "{app,config}/**/*.rb"
executable_name: "bin/codeownership"
Loading