-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproject.rs
More file actions
231 lines (196 loc) · 5.82 KB
/
project.rs
File metadata and controls
231 lines (196 loc) · 5.82 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
use core::fmt;
use std::{
collections::HashMap,
fmt::Display,
fs::File,
path::{Path, PathBuf},
};
use error_stack::{Context, Result, ResultExt};
pub struct Project {
pub base_path: PathBuf,
pub files: Vec<ProjectFile>,
pub packages: Vec<Package>,
pub vendored_gems: Vec<VendoredGem>,
pub teams: Vec<Team>,
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)]
pub struct VendoredGem {
pub path: PathBuf,
pub name: String,
}
#[derive(Debug, Clone)]
pub struct ProjectFile {
pub owner: Option<String>,
pub path: PathBuf,
}
#[derive(Clone, Debug, Default)]
pub struct Team {
pub path: PathBuf,
pub name: String,
pub github_team: String,
pub owned_globs: Vec<String>,
pub subtracted_globs: Vec<String>,
pub owned_gems: Vec<String>,
pub avoid_ownership: bool,
}
impl Team {
pub fn from_team_file_path(absolute_path: PathBuf) -> Result<Self, Error> {
let file = File::open(&absolute_path).change_context(Error::Io)?;
let deserializer: deserializers::Team = serde_yaml::from_reader(file).change_context(Error::SerdeYaml)?;
Ok(Self {
path: absolute_path.to_owned(),
name: deserializer.name,
github_team: deserializer.github.team,
owned_globs: deserializer.owned_globs,
subtracted_globs: deserializer.subtracted_globs,
owned_gems: deserializer.ruby.map(|ruby| ruby.owned_gems).unwrap_or_default(),
avoid_ownership: deserializer.github.do_not_add_to_codeowners_file,
})
}
}
#[derive(Clone, Debug)]
pub struct Package {
pub path: PathBuf,
pub package_type: PackageType,
pub owner: String,
}
impl Package {
pub fn package_root(&self) -> Option<&Path> {
self.path.parent()
}
}
#[derive(Clone, Debug)]
pub struct DirectoryCodeownersFile {
pub path: PathBuf,
pub owner: String,
}
impl DirectoryCodeownersFile {
pub fn directory_root(&self) -> Option<&Path> {
self.path.parent()
}
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum PackageType {
Ruby,
Javascript,
}
impl Display for PackageType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub mod deserializers {
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Metadata {
pub owner: Option<String>,
}
#[derive(Deserialize)]
pub struct JavascriptPackage {
pub metadata: Option<Metadata>,
}
#[derive(Deserialize)]
pub struct RubyPackage {
pub owner: Option<String>,
}
#[derive(Deserialize)]
pub struct Github {
pub team: String,
#[serde(default = "bool_false")]
pub do_not_add_to_codeowners_file: bool,
}
#[derive(Deserialize)]
pub struct Ruby {
#[serde(default = "empty_string_vec")]
pub owned_gems: Vec<String>,
}
#[derive(Deserialize)]
pub struct Team {
pub name: String,
pub github: Github,
pub ruby: Option<Ruby>,
#[serde(default = "empty_string_vec")]
pub owned_globs: Vec<String>,
#[serde(alias = "unowned_globs", default = "empty_string_vec")]
pub subtracted_globs: Vec<String>,
}
fn empty_string_vec() -> Vec<String> {
Vec::new()
}
fn bool_false() -> bool {
false
}
}
#[derive(Debug)]
pub enum Error {
Io,
SerdeYaml,
SerdeJson,
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io => fmt.write_str("IO operation failed"),
Error::SerdeYaml => fmt.write_str("YAML serialization/deserialization failed"),
Error::SerdeJson => fmt.write_str("JSON serialization/deserialization failed"),
}
}
}
impl Context for Error {}
impl Project {
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).unwrap_or(absolute_path)
}
pub fn get_team(&self, name: &str) -> Option<Team> {
self.teams_by_name.get(name).cloned()
}
pub fn vendored_gem_by_name(&self) -> HashMap<String, VendoredGem> {
let mut result: HashMap<String, VendoredGem> = HashMap::new();
for vendored_gem in &self.vendored_gems {
result.insert(vendored_gem.name.clone(), vendored_gem.clone());
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vendored_gem_by_name_maps_all_gems() {
let vg1 = VendoredGem {
path: PathBuf::from("vendored/a"),
name: "a".to_string(),
};
let vg2 = VendoredGem {
path: PathBuf::from("vendored/b"),
name: "b".to_string(),
};
let project = Project {
base_path: PathBuf::from("."),
files: vec![],
packages: vec![],
vendored_gems: vec![vg1.clone(), vg2.clone()],
teams: vec![],
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();
assert_eq!(map.len(), 2);
assert_eq!(map.get("a").unwrap().name, vg1.name);
assert_eq!(map.get("b").unwrap().name, vg2.name);
}
}