forked from rubyatscale/code_ownership
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
134 lines (121 loc) · 4.53 KB
/
lib.rs
File metadata and controls
134 lines (121 loc) · 4.53 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
use std::{collections::HashMap, env, path::PathBuf};
use codeowners::runner::{self, RunConfig};
use magnus::{Error, Ruby, Value, function, prelude::*};
use serde::{Deserialize, Serialize};
use serde_magnus::serialize;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Team {
pub team_name: String,
pub team_config_yml: String,
pub reasons: Vec<String>,
}
fn for_team(ruby: &Ruby, team_name: String) -> Result<Value, Error> {
let run_config = build_run_config();
let team = runner::for_team(&run_config, &team_name);
validate_result(ruby, &team)
}
fn teams_for_files(ruby: &Ruby, file_paths: Vec<String>) -> Result<Value, Error> {
let run_config = build_run_config();
let path_teams = runner::teams_for_files_from_codeowners(&run_config, &file_paths);
match path_teams {
Ok(path_teams) => {
let mut teams_map: HashMap<String, Option<Team>> = HashMap::new();
for (path, team) in path_teams {
if let Some(found_team) = team {
teams_map.insert(path, Some(Team {
team_name: found_team.name.to_string(),
team_config_yml: found_team.name.to_string(),
reasons: vec![],
}));
} else {
teams_map.insert(path, None);
}
}
let serialized: Value = serialize(ruby, &teams_map)?;
Ok(serialized)
}
Err(e) => Err(Error::new(ruby.exception_runtime_error(), e.to_string())),
}
}
fn for_file(ruby: &Ruby, file_path: String) -> Result<Option<Value>, Error> {
let run_config = build_run_config();
match runner::file_owner_for_file(&run_config, &file_path) {
Ok(owner) => {
if let Some(owner) = owner {
let team = Team {
team_name: owner.team.name,
team_config_yml: owner.team_config_file_path.to_string(),
reasons: owner
.sources
.iter()
.map(|source| source.to_string())
.collect(),
};
let serialized: Value = serialize(ruby, &team)?;
Ok(Some(serialized))
} else {
Ok(None)
}
}
Err(e) => Err(Error::new(
ruby.exception_runtime_error(),
e.to_string(),
)),
}
}
fn version() -> String {
runner::version()
}
fn validate(ruby: &Ruby, files: Option<Vec<String>>) -> Result<Value, Error> {
let run_config = build_run_config();
let files_vec = files.unwrap_or_default();
let run_result = runner::validate(&run_config, files_vec);
validate_result(ruby, &run_result)
}
fn generate_and_validate(ruby: &Ruby, files: Option<Vec<String>>, skip_stage: bool) -> Result<Value, Error> {
let run_config = build_run_config();
let files_vec = files.unwrap_or_default();
let run_result = runner::generate_and_validate(&run_config, files_vec, skip_stage);
validate_result(ruby, &run_result)
}
fn validate_result(ruby: &Ruby, run_result: &runner::RunResult) -> Result<Value, Error> {
if !run_result.validation_errors.is_empty() {
Err(Error::new(
ruby.exception_runtime_error(),
run_result.validation_errors.join("\n"),
))
} else if !run_result.io_errors.is_empty() {
Err(Error::new(
ruby.exception_runtime_error(),
run_result.io_errors.join("\n"),
))
} else {
let serialized: Value = serialize(ruby, &run_result.info_messages)?;
Ok(serialized)
}
}
fn build_run_config() -> RunConfig {
let project_root = match env::current_dir() {
Ok(path) => path,
_ => PathBuf::from("."),
};
let config_path = project_root.join("config/code_ownership.yml");
RunConfig {
project_root,
codeowners_file_path: None,
config_path,
no_cache: false,
executable_name: None,
}
}
#[magnus::init]
fn init(ruby: &Ruby) -> Result<(), Error> {
let module = ruby.define_module("RustCodeOwners")?;
module.define_singleton_method("for_file", function!(for_file, 1))?;
module.define_singleton_method("generate_and_validate", function!(generate_and_validate, 2))?;
module.define_singleton_method("validate", function!(validate, 1))?;
module.define_singleton_method("for_team", function!(for_team, 1))?;
module.define_singleton_method("version", function!(version, 0))?;
module.define_singleton_method("teams_for_files", function!(teams_for_files, 1))?;
Ok(())
}