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
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ fn cli() -> Result<(), Error> {
let ownership = Ownership::build(Project::build(&project_root, &codeowners_file_path, &config).change_context(Error::Io)?);

match args.command {
Command::Validate => ownership.validate(false).change_context(Error::ValidationFailed)?,
Command::Validate => ownership.validate().change_context(Error::ValidationFailed)?,
Command::Generate => {
std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?;
}
Command::GenerateAndValidate => {
std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?;
ownership.validate(true).change_context(Error::ValidationFailed)?
ownership.validate().change_context(Error::ValidationFailed)?
}
Command::ForFile { name } => {
let file_owners = ownership.for_file(&name).change_context(Error::Io)?;
Expand Down
4 changes: 2 additions & 2 deletions src/ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ impl Ownership {
}

#[instrument(level = "debug", skip_all)]
pub fn validate(&self, skip_codeowners_file_validation: bool) -> Result<(), ValidatorErrors> {
pub fn validate(&self) -> Result<(), ValidatorErrors> {
info!("validating file ownership");
let validator = Validator {
project: self.project.clone(),
mappers: self.mappers(),
file_generator: FileGenerator { mappers: self.mappers() },
};

validator.validate(skip_codeowners_file_validation)
validator.validate()
}

#[instrument(level = "debug", skip_all)]
Expand Down
21 changes: 12 additions & 9 deletions src/ownership/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Errors(Vec<Error>);

impl Validator {
#[instrument(level = "debug", skip_all)]
pub fn validate(&self, skip_codeowners_file_validation: bool) -> Result<(), Errors> {
pub fn validate(&self) -> Result<(), Errors> {
let mut validation_errors = Vec::new();

debug!("validate_invalid_team");
Expand All @@ -45,10 +45,8 @@ impl Validator {
debug!("validate_file_ownership");
validation_errors.append(&mut self.validate_file_ownership());

if !skip_codeowners_file_validation {
debug!("validate_codeowners_file");
validation_errors.append(&mut self.validate_codeowners_file());
}
debug!("validate_codeowners_file");
validation_errors.append(&mut self.validate_codeowners_file());

if validation_errors.is_empty() {
Ok(())
Expand Down Expand Up @@ -129,10 +127,15 @@ impl Validator {
fn validate_codeowners_file(&self) -> Vec<Error> {
let generated_file = self.file_generator.generate_file();

if generated_file != self.project.codeowners_file {
vec![Error::CodeownershipFileIsStale]
} else {
vec![]
match self.project.get_codeowners_file() {
Ok(current_file) => {
if generated_file != current_file {
vec![Error::CodeownershipFileIsStale]
} else {
vec![]
}
}
Err(_) => vec![Error::CodeownershipFileIsStale], // Treat any read error as stale file
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Project {
pub packages: Vec<Package>,
pub vendored_gems: Vec<VendoredGem>,
pub teams: Vec<Team>,
pub codeowners_file: String,
pub codeowners_file_path: PathBuf,
pub directory_codeowner_files: Vec<DirectoryCodeownersFile>,
}

Expand Down Expand Up @@ -249,12 +249,6 @@ impl Project {
"finished scanning project",
);

let codeowners_file: String = if codeowners_file_path.exists() {
std::fs::read_to_string(codeowners_file_path).change_context(Error::Io)?
} else {
"".to_owned()
};

let owned_files = owned_files(owned_file_paths);

Ok(Project {
Expand All @@ -263,11 +257,20 @@ impl Project {
vendored_gems,
teams,
packages,
codeowners_file,
codeowners_file_path: codeowners_file_path.to_path_buf(),
directory_codeowner_files,
})
}

pub fn get_codeowners_file(&self) -> Result<String, Error> {
let codeowners_file: String = if self.codeowners_file_path.exists() {
std::fs::read_to_string(&self.codeowners_file_path).change_context(Error::Io)?
} else {
"".to_owned()
};
Ok(codeowners_file)
}

pub fn relative_path<'a>(&'a self, absolute_path: &'a Path) -> &'a Path {
absolute_path
.strip_prefix(&self.base_path)
Expand Down