-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.rs
More file actions
247 lines (212 loc) · 8.11 KB
/
resolver.rs
File metadata and controls
247 lines (212 loc) · 8.11 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
use super::{
inline_parser::detect_inline_codeowners,
types::{CodeownersEntryMatcher, Tag},
};
use crate::utils::error::{Error, Result};
use std::path::Path;
use super::types::Owner;
/// Find both owners and tags for a specific file based on all parsed CODEOWNERS entries
pub fn find_owners_and_tags_for_file(
file_path: &Path, entries: &[CodeownersEntryMatcher],
) -> Result<(Vec<Owner>, Vec<Tag>)> {
// First, check for inline CODEOWNERS declaration (highest priority)
if let Some(inline_entry) = detect_inline_codeowners(file_path)? {
return Ok((inline_entry.owners, inline_entry.tags));
}
// Early return if no entries
if entries.is_empty() {
return Ok((Vec::new(), Vec::new()));
}
let target_dir = file_path
.parent()
.ok_or_else(|| Error::new("file path has no parent directory"))?;
let mut candidates: Vec<_> = entries
.iter()
.filter_map(|entry| {
let codeowners_dir = match entry.source_file.parent() {
Some(dir) => dir,
None => {
eprintln!(
"CODEOWNERS entry has no parent directory: {}",
entry.source_file.display()
);
return None;
}
};
// Check if the CODEOWNERS directory is an ancestor of the target directory
if !target_dir.starts_with(codeowners_dir) {
return None;
}
// Calculate the depth as the number of components in the relative path from codeowners_dir to target_dir
let rel_path = match target_dir.strip_prefix(codeowners_dir) {
Ok(p) => p,
Err(_) => return None, // Should not happen due to starts_with check
};
let depth = rel_path.components().count();
// Check if the pattern matches the target file
let matches = {
entry
.override_matcher
.matched(file_path, false)
.is_whitelist()
};
if matches {
Some((entry, depth))
} else {
None
}
})
.collect();
// Sort the candidates by depth, source file, and line number
candidates.sort_unstable_by(|a, b| {
let a_entry = a.0;
let a_depth = a.1;
let b_entry = b.0;
let b_depth = b.1;
// Primary sort by depth (ascending)
a_depth
.cmp(&b_depth)
// Then by source file (to group entries from the same CODEOWNERS file)
.then_with(|| a_entry.source_file.cmp(&b_entry.source_file))
// Then by line number (descending) to prioritize later entries in the same file
.then_with(|| b_entry.line_number.cmp(&a_entry.line_number))
});
// Extract both owners and tags from the highest priority entry, if any
Ok(candidates
.first()
.map(|(entry, _)| (entry.owners.clone(), entry.tags.clone()))
.unwrap_or_default())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::types::{Owner, OwnerType, Tag};
use ignore::overrides::OverrideBuilder;
use std::path::PathBuf;
fn create_test_owner(identifier: &str, owner_type: OwnerType) -> Owner {
Owner {
identifier: identifier.to_string(),
owner_type,
}
}
fn create_test_tag(name: &str) -> Tag {
Tag(name.to_string())
}
fn create_test_codeowners_entry_matcher(
source_file: &str, line_number: usize, pattern: &str, owners: Vec<Owner>, tags: Vec<Tag>,
) -> CodeownersEntryMatcher {
let source_path = PathBuf::from(source_file);
let codeowners_dir = source_path.parent().unwrap();
let mut builder = OverrideBuilder::new(codeowners_dir);
builder.add(pattern).unwrap();
let override_matcher = builder.build().unwrap();
CodeownersEntryMatcher {
source_file: source_path,
line_number,
pattern: pattern.to_string(),
owners,
tags,
override_matcher,
}
}
#[test]
fn test_find_owners_and_tags_for_file_empty_entries() {
let entries = vec![];
let file_path = Path::new("/project/src/main.rs");
let result = find_owners_and_tags_for_file(file_path, &entries).unwrap();
assert!(result.0.is_empty());
assert!(result.1.is_empty());
}
#[test]
fn test_find_owners_and_tags_for_file_simple_match() {
let expected_owner = create_test_owner("@rust-team", OwnerType::Team);
let expected_tag = create_test_tag("rust");
let entries = vec![create_test_codeowners_entry_matcher(
"/project/CODEOWNERS",
1,
"*.rs",
vec![expected_owner.clone()],
vec![expected_tag.clone()],
)];
let file_path = Path::new("/project/src/main.rs");
let result = find_owners_and_tags_for_file(file_path, &entries).unwrap();
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0], expected_owner);
assert_eq!(result.1.len(), 1);
assert_eq!(result.1[0], expected_tag);
}
#[test]
fn test_find_owners_and_tags_for_file_directory_hierarchy() {
let root_owner = create_test_owner("@root-team", OwnerType::Team);
let root_tag = create_test_tag("root");
let src_owner = create_test_owner("@src-team", OwnerType::Team);
let src_tag = create_test_tag("source");
let entries = vec![
create_test_codeowners_entry_matcher(
"/project/CODEOWNERS",
1,
"*",
vec![root_owner.clone()],
vec![root_tag.clone()],
),
create_test_codeowners_entry_matcher(
"/project/src/CODEOWNERS",
1,
"*.rs",
vec![src_owner.clone()],
vec![src_tag.clone()],
),
];
let file_path = Path::new("/project/src/main.rs");
let result = find_owners_and_tags_for_file(file_path, &entries).unwrap();
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0], src_owner);
assert_eq!(result.1.len(), 1);
assert_eq!(result.1[0], src_tag);
}
#[test]
fn test_find_owners_and_tags_for_file_line_number_priority() {
let general_owner = create_test_owner("@general-team", OwnerType::Team);
let general_tag = create_test_tag("general");
let specific_owner = create_test_owner("@specific-team", OwnerType::Team);
let specific_tag = create_test_tag("specific");
let entries = vec![
create_test_codeowners_entry_matcher(
"/project/CODEOWNERS",
1,
"*",
vec![general_owner.clone()],
vec![general_tag.clone()],
),
create_test_codeowners_entry_matcher(
"/project/CODEOWNERS",
10,
"src/*.rs",
vec![specific_owner.clone()],
vec![specific_tag.clone()],
),
];
let file_path = Path::new("/project/src/main.rs");
let result = find_owners_and_tags_for_file(file_path, &entries).unwrap();
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0], specific_owner);
assert_eq!(result.1.len(), 1);
assert_eq!(result.1[0], specific_tag);
}
#[test]
fn test_find_owners_and_tags_for_file_valid_pattern() {
let entries = vec![create_test_codeowners_entry_matcher(
"/project/CODEOWNERS",
2,
"*.rs",
vec![create_test_owner("@team2", OwnerType::Team)],
vec![create_test_tag("tag2")],
)];
let file_path = Path::new("/project/src/main.rs");
let result = find_owners_and_tags_for_file(file_path, &entries).unwrap();
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0].identifier, "@team2");
assert_eq!(result.1.len(), 1);
assert_eq!(result.1[0].0, "tag2");
}
}