Skip to content

Commit b0899d3

Browse files
authored
read codeowners on demand (#35)
1 parent 50b7d05 commit b0899d3

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ fn cli() -> Result<(), Error> {
114114
let ownership = Ownership::build(Project::build(&project_root, &codeowners_file_path, &config).change_context(Error::Io)?);
115115

116116
match args.command {
117-
Command::Validate => ownership.validate(false).change_context(Error::ValidationFailed)?,
117+
Command::Validate => ownership.validate().change_context(Error::ValidationFailed)?,
118118
Command::Generate => {
119119
std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?;
120120
}
121121
Command::GenerateAndValidate => {
122122
std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?;
123-
ownership.validate(true).change_context(Error::ValidationFailed)?
123+
ownership.validate().change_context(Error::ValidationFailed)?
124124
}
125125
Command::ForFile { name } => {
126126
let file_owners = ownership.for_file(&name).change_context(Error::Io)?;

src/ownership.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ impl Ownership {
8686
}
8787

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

97-
validator.validate(skip_codeowners_file_validation)
97+
validator.validate()
9898
}
9999

100100
#[instrument(level = "debug", skip_all)]

src/ownership/validator.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct Errors(Vec<Error>);
3636

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

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

48-
if !skip_codeowners_file_validation {
49-
debug!("validate_codeowners_file");
50-
validation_errors.append(&mut self.validate_codeowners_file());
51-
}
48+
debug!("validate_codeowners_file");
49+
validation_errors.append(&mut self.validate_codeowners_file());
5250

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

132-
if generated_file != self.project.codeowners_file {
133-
vec![Error::CodeownershipFileIsStale]
134-
} else {
135-
vec![]
130+
match self.project.get_codeowners_file() {
131+
Ok(current_file) => {
132+
if generated_file != current_file {
133+
vec![Error::CodeownershipFileIsStale]
134+
} else {
135+
vec![]
136+
}
137+
}
138+
Err(_) => vec![Error::CodeownershipFileIsStale], // Treat any read error as stale file
136139
}
137140
}
138141

src/project.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Project {
2323
pub packages: Vec<Package>,
2424
pub vendored_gems: Vec<VendoredGem>,
2525
pub teams: Vec<Team>,
26-
pub codeowners_file: String,
26+
pub codeowners_file_path: PathBuf,
2727
pub directory_codeowner_files: Vec<DirectoryCodeownersFile>,
2828
}
2929

@@ -249,12 +249,6 @@ impl Project {
249249
"finished scanning project",
250250
);
251251

252-
let codeowners_file: String = if codeowners_file_path.exists() {
253-
std::fs::read_to_string(codeowners_file_path).change_context(Error::Io)?
254-
} else {
255-
"".to_owned()
256-
};
257-
258252
let owned_files = owned_files(owned_file_paths);
259253

260254
Ok(Project {
@@ -263,11 +257,20 @@ impl Project {
263257
vendored_gems,
264258
teams,
265259
packages,
266-
codeowners_file,
260+
codeowners_file_path: codeowners_file_path.to_path_buf(),
267261
directory_codeowner_files,
268262
})
269263
}
270264

265+
pub fn get_codeowners_file(&self) -> Result<String, Error> {
266+
let codeowners_file: String = if self.codeowners_file_path.exists() {
267+
std::fs::read_to_string(&self.codeowners_file_path).change_context(Error::Io)?
268+
} else {
269+
"".to_owned()
270+
};
271+
Ok(codeowners_file)
272+
}
273+
271274
pub fn relative_path<'a>(&'a self, absolute_path: &'a Path) -> &'a Path {
272275
absolute_path
273276
.strip_prefix(&self.base_path)

0 commit comments

Comments
 (0)