-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.java
More file actions
162 lines (135 loc) · 5.96 KB
/
Validate.java
File metadata and controls
162 lines (135 loc) · 5.96 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.collections4.CollectionUtils;
/*
General approach:
1. Traverse the entire directory once, and store owners and dependency info into a hashtable
2. For each file changed, check owner file for that directory, and check all other directories that have dependency
3. If there's now owner file for particular directory, refer to root owner file
4. Display the result
*/
public class Validate {
// For this exercise, assume all source files are in java
public static final String sourceExtension = "java";
public static Hashtable<String, Helper> fileMapping;
public static void main(String... args) {
fileMapping = new Hashtable<String, Helper>();
// Set directory to current
File dir = new File (".");
// Traverse through directory and fill in fileMapping table
traverseDirectory(dir.listFiles(), fileMapping);
// Parse arguments
String[] nameInput = args[0].split(",");
ArrayList <String> names = new ArrayList<String>(Arrays.asList(nameInput));
String[] filesInput = args[1].split(",");
ArrayList <String> paths = new ArrayList<String>(Arrays.asList(filesInput));
// Check if changed files are approved
validateApprovals(names, paths, fileMapping);
}
public static Hashtable<String, Helper> traverseDirectory(File[] files, Hashtable<String, Helper> input) {
Hashtable<String, Helper> result = new Hashtable<String, Helper>();
for (File file: files) {
// found directory, keep searching for files recursively
if (file.isDirectory()) {
traverseDirectory(file.listFiles(), input);
}
else {
// found source file
if (file.getAbsolutePath().endsWith(sourceExtension)) {
result = generateMapping(file.getParentFile(), input);
}
}
}
return result;
}
public static Hashtable<String, Helper> generateMapping(File file, Hashtable<String, Helper> input) {
ArrayList<String> owners = new ArrayList<String>();
ArrayList<String> dependencies = new ArrayList<String>();
Helper helper = new Helper(owners, dependencies);
File[] files = file.listFiles();
// Scan and read OWNERS and DEPENDENCIES files
for (File lookup: files) {
if (lookup.getAbsolutePath().endsWith("OWNERS")) {
Scanner scanner = null;
try {
scanner = new Scanner(lookup);
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
helper.addOwner(line);
}
} else if (lookup.getAbsolutePath().endsWith("DEPENDENCIES")) {
Scanner scanner = null;
try {
scanner = new Scanner(lookup);
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
helper.addDependency(line);
}
}
}
// Strip out the unnecessary prefix in path and store the values in hashtabl
input.put(file.getPath().replaceFirst("./", ""), helper);
return input;
}
public static String validateApprovals(ArrayList<String> names, ArrayList<String> files, Hashtable<String, Helper> input) {
for (String path: files) {
// Handles invalid file path
boolean fileExist = new File(".", path).exists();
if (!fileExist) {
System.out.println("Invalid file path provided");
return "Invalid file path provided";
}
File target = new File(path);
Boolean result = compareMappings(names, target, input);
if (result == false) {
// If any of the file doesn't have enough approvals, return insufficient
System.out.println("Insufficient approvals");
return "Insufficient approvals";
}
}
// If all files are checked and have sufficient approvals, then return approved
System.out.println("Approved");
return "Approved";
}
public static Boolean compareMappings(ArrayList<String> name, File path, Hashtable<String, Helper> input) {
String parentDir = path.getParent();
Helper currentDir = input.get(parentDir);
ArrayList<String> currentOwners = new ArrayList<String>();
ArrayList<String> rootOwners = input.get(".").owners;
AtomicBoolean result = new AtomicBoolean(false);
// get current dir owners
currentOwners.addAll(currentDir.owners);
// if the passed in name is within the OWNER file, set flag to true
// else set to false and return
if(CollectionUtils.containsAny(currentOwners, name)) {
result.set(true);
} else {
result.set(false);
return result.get();
}
// search all dependencies
input.forEach((key, value) -> {
// if no owner file, set to root dir owner
if (!CollectionUtils.containsAny(rootOwners, value.owners)) {
input.get(key).owners.addAll(rootOwners);
}
// found a dependency
if(value.dependencies.contains(parentDir))
{
// if the passed in name is not in the dependency OWNER, set flag to false
if(!CollectionUtils.containsAny(input.get(key).owners, name)) {
result.set(false);
}
}
});
return result.get();
}
}