-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrunner.rs
More file actions
437 lines (394 loc) · 14.2 KB
/
runner.rs
File metadata and controls
437 lines (394 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::process::Command;
use error_stack::{Result, ResultExt};
use serde::Serialize;
use crate::{
cache::{Cache, Caching, file::GlobalCache, noop::NoopCache},
config::Config,
ownership::{FileOwner, Ownership},
project_builder::ProjectBuilder,
};
mod types;
pub use self::types::{Error, RunConfig, RunResult};
mod api;
pub use self::api::*;
pub struct Runner {
run_config: RunConfig,
ownership: Ownership,
cache: Cache,
config: Config,
}
pub fn version() -> String {
env!("CARGO_PKG_VERSION").to_string()
}
pub type Runnable = fn(Runner) -> RunResult;
pub fn run<F>(run_config: &RunConfig, runnable: F) -> RunResult
where
F: FnOnce(Runner) -> RunResult,
{
let runner = match Runner::new(run_config) {
Ok(runner) => runner,
Err(err) => {
return RunResult {
io_errors: vec![err.to_string()],
..Default::default()
};
}
};
runnable(runner)
}
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_run_config(run_config)?;
let cache: Cache = if run_config.no_cache {
NoopCache::default().into()
} else {
GlobalCache::new(run_config.project_root.clone(), config.cache_directory.clone())
.change_context(Error::Io(format!(
"Can't create cache: {}",
&run_config.config_path.to_string_lossy()
)))
.attach_printable(format!("Can't create cache: {}", &run_config.config_path.to_string_lossy()))?
.into()
};
let mut project_builder = ProjectBuilder::new(
&config,
run_config.project_root.clone(),
run_config.codeowners_file_path.clone(),
&cache,
);
let project = project_builder.build().change_context(Error::Io(format!(
"Can't build project: {}",
&run_config.config_path.to_string_lossy()
)))?;
let ownership = Ownership::build(project);
cache.persist_cache().change_context(Error::Io(format!(
"Can't persist cache: {}",
&run_config.config_path.to_string_lossy()
)))?;
Ok(Self {
run_config: run_config.clone(),
ownership,
cache,
config,
})
}
pub fn validate(&self, file_paths: Vec<String>) -> RunResult {
if file_paths.is_empty() {
self.validate_all()
} else {
self.validate_files(file_paths)
}
}
fn validate_all(&self) -> RunResult {
match self.ownership.validate() {
Ok(_) => RunResult::default(),
Err(err) => RunResult {
validation_errors: vec![format!("{}", err)],
..Default::default()
},
}
}
fn validate_files(&self, file_paths: Vec<String>) -> RunResult {
let mut unowned_files = Vec::new();
let mut io_errors = Vec::new();
for file_path in file_paths {
match team_for_file_from_codeowners(&self.run_config, &file_path) {
Ok(Some(_)) => {}
Ok(None) => unowned_files.push(file_path),
Err(err) => io_errors.push(format!("{}: {}", file_path, err)),
}
}
if !unowned_files.is_empty() {
let validation_errors = std::iter::once("Unowned files detected:".to_string())
.chain(unowned_files.into_iter().map(|file| format!(" {}", file)))
.collect();
return RunResult {
validation_errors,
io_errors,
..Default::default()
};
}
if !io_errors.is_empty() {
return RunResult {
io_errors,
..Default::default()
};
}
RunResult::default()
}
pub fn generate(&self, git_stage: bool) -> RunResult {
let content = self.ownership.generate_file();
if let Some(parent) = &self.run_config.codeowners_file_path.parent() {
let _ = std::fs::create_dir_all(parent);
}
match std::fs::write(&self.run_config.codeowners_file_path, content) {
Ok(_) => {
if git_stage {
self.git_stage();
}
RunResult::default()
}
Err(err) => RunResult {
io_errors: vec![err.to_string()],
..Default::default()
},
}
}
pub fn generate_and_validate(&self, file_paths: Vec<String>, git_stage: bool) -> RunResult {
let run_result = self.generate(git_stage);
if run_result.has_errors() {
return run_result;
}
self.validate(file_paths)
}
fn git_stage(&self) {
let _ = Command::new("git")
.arg("add")
.arg(&self.run_config.codeowners_file_path)
.current_dir(&self.run_config.project_root)
.output();
}
pub fn for_team(&self, team_name: &str) -> RunResult {
let mut info_messages = vec![];
let mut io_errors = vec![];
match self.ownership.for_team(team_name) {
Ok(team_ownerships) => {
info_messages.push(format!("# Code Ownership Report for `{}` Team", team_name));
for team_ownership in team_ownerships {
info_messages.push(format!("\n#{}", team_ownership.heading));
match team_ownership.globs.len() {
0 => info_messages.push("This team owns nothing in this category.".to_string()),
_ => info_messages.push(team_ownership.globs.join("\n")),
}
}
}
Err(err) => io_errors.push(format!("{}", err)),
}
RunResult {
info_messages,
io_errors,
..Default::default()
}
}
pub fn delete_cache(&self) -> RunResult {
match self.cache.delete_cache().change_context(Error::Io(format!(
"Can't delete cache: {}",
&self.run_config.config_path.to_string_lossy()
))) {
Ok(_) => RunResult::default(),
Err(err) => RunResult {
io_errors: vec![err.to_string()],
..Default::default()
},
}
}
pub fn crosscheck_owners(&self) -> RunResult {
crate::crosscheck::crosscheck_owners(&self.run_config, &self.cache)
}
pub fn owners_for_file(&self, file_path: &str) -> Result<Vec<FileOwner>, Error> {
use crate::ownership::file_owner_resolver::find_file_owners;
let owners = find_file_owners(&self.run_config.project_root, &self.config, std::path::Path::new(file_path)).map_err(Error::Io)?;
Ok(owners)
}
pub fn for_file_derived(&self, file_path: &str, json: bool) -> RunResult {
let file_owners = match self.owners_for_file(file_path) {
Ok(v) => v,
Err(err) => {
return RunResult::from_io_error(Error::Io(err.to_string()), json);
}
};
match file_owners.as_slice() {
[] => RunResult::from_file_owner(&FileOwner::default(), json),
[owner] => RunResult::from_file_owner(owner, json),
many => {
let mut error_messages = vec!["Error: file is owned by multiple teams!".to_string()];
for owner in many {
error_messages.push(format!("\n{}", owner));
}
RunResult::from_validation_errors(error_messages, json)
}
}
}
pub fn for_file_codeowners_only(&self, file_path: &str, json: bool) -> RunResult {
match team_for_file_from_codeowners(&self.run_config, file_path) {
Ok(Some(team)) => {
let team_yml = crate::path_utils::relative_to(&self.run_config.project_root, team.path.as_path())
.to_string_lossy()
.to_string();
let result = ForFileResult {
team_name: team.name.clone(),
github_team: team.github_team.clone(),
team_yml,
description: vec!["Owner inferred from codeowners file".to_string()],
};
if json {
RunResult::json_info(result)
} else {
RunResult {
info_messages: vec![format!(
"Team: {}\nGithub Team: {}\nTeam YML: {}\nDescription:\n- {}",
result.team_name,
result.github_team,
result.team_yml,
result.description.join("\n- ")
)],
..Default::default()
}
}
}
Ok(None) => RunResult::from_file_owner(&FileOwner::default(), json),
Err(err) => {
if json {
RunResult::json_io_error(Error::Io(err.to_string()))
} else {
RunResult {
io_errors: vec![err.to_string()],
..Default::default()
}
}
}
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct ForFileResult {
pub team_name: String,
pub github_team: String,
pub team_yml: String,
pub description: Vec<String>,
}
impl RunResult {
pub fn has_errors(&self) -> bool {
!self.validation_errors.is_empty() || !self.io_errors.is_empty()
}
fn from_io_error(error: Error, json: bool) -> Self {
if json {
Self::json_io_error(error)
} else {
Self {
io_errors: vec![error.to_string()],
..Default::default()
}
}
}
fn from_file_owner(file_owner: &FileOwner, json: bool) -> Self {
if json {
let description: Vec<String> = if file_owner.sources.is_empty() {
vec![]
} else {
file_owner.sources.iter().map(|source| source.to_string()).collect()
};
Self::json_info(ForFileResult {
team_name: file_owner.team.name.clone(),
github_team: file_owner.team.github_team.clone(),
team_yml: file_owner.team_config_file_path.clone(),
description,
})
} else {
Self {
info_messages: vec![format!("{}", file_owner)],
..Default::default()
}
}
}
fn from_validation_errors(validation_errors: Vec<String>, json: bool) -> Self {
if json {
Self::json_validation_error(validation_errors)
} else {
Self {
validation_errors,
..Default::default()
}
}
}
pub fn json_info(result: ForFileResult) -> Self {
let json = match serde_json::to_string_pretty(&result) {
Ok(json) => json,
Err(e) => return Self::fallback_io_error(&e.to_string()),
};
Self {
info_messages: vec![json],
..Default::default()
}
}
pub fn json_io_error(error: Error) -> Self {
let message = match error {
Error::Io(msg) => msg,
Error::ValidationFailed => "Error::ValidationFailed".to_string(),
};
let json = match serde_json::to_string(&serde_json::json!({"error": message})) {
Ok(json) => json,
Err(e) => return Self::fallback_io_error(&format!("JSON serialization failed: {}", e)),
};
Self {
io_errors: vec![json],
..Default::default()
}
}
pub fn json_validation_error(validation_errors: Vec<String>) -> Self {
let json_obj = serde_json::json!({"validation_errors": validation_errors});
let json = match serde_json::to_string_pretty(&json_obj) {
Ok(json) => json,
Err(e) => return Self::fallback_io_error(&format!("JSON serialization failed: {}", e)),
};
Self {
validation_errors: vec![json],
..Default::default()
}
}
fn fallback_io_error(message: &str) -> Self {
Self {
io_errors: vec![format!("{{\"error\": \"{}\"}}", message.replace('"', "\\\""))],
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version() {
assert_eq!(version(), env!("CARGO_PKG_VERSION").to_string());
}
#[test]
fn test_json_info() {
let result = ForFileResult {
team_name: "team1".to_string(),
github_team: "team1".to_string(),
team_yml: "config/teams/team1.yml".to_string(),
description: vec!["file annotation".to_string()],
};
let result = RunResult::json_info(result);
assert_eq!(result.info_messages.len(), 1);
assert_eq!(
result.info_messages[0],
"{\n \"team_name\": \"team1\",\n \"github_team\": \"team1\",\n \"team_yml\": \"config/teams/team1.yml\",\n \"description\": [\n \"file annotation\"\n ]\n}"
);
}
#[test]
fn test_json_io_error() {
let result = RunResult::json_io_error(Error::Io("unable to find file".to_string()));
assert_eq!(result.io_errors.len(), 1);
assert_eq!(result.io_errors[0], "{\"error\":\"unable to find file\"}");
}
#[test]
fn test_json_validation_error() {
let result = RunResult::json_validation_error(vec!["file has multiple owners".to_string()]);
assert_eq!(result.validation_errors.len(), 1);
assert_eq!(
result.validation_errors[0],
"{\n \"validation_errors\": [\n \"file has multiple owners\"\n ]\n}"
);
}
}