-
Notifications
You must be signed in to change notification settings - Fork 26
/
CompatibilityReport.java
302 lines (248 loc) · 9.45 KB
/
CompatibilityReport.java
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
///usr/bin/env jbang "$0" "$@" ; exit $? // (1)
//DEPS io.vertx:vertx-core:3.9.2
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.json.*;
class CompatibilityReport {
public static void main(String[] args) throws Exception {
System.out.println("Looking for compatibility reports");
File current = new File(System.getProperty("user.dir"));
File[] children = current.listFiles();
Map<String, JsonArray> reports = new LinkedHashMap<>();
for (File child : children) {
if (child.isDirectory()) {
File report = new File(child, "target/compatibility-report.json");
if (report.isFile()) {
System.out.println("Found compatibility report " + report.getAbsolutePath());
JsonArray content = new JsonArray(Files.readString(report.toPath()));
reports.put(child.getName(), content);
} else if (child.isDirectory()) {
for (File child2 : child.listFiles()) {
if (child2.isDirectory()) {
report = new File(child2, "target/compatibility-report.json");
if (report.isFile()) {
System.out.println("Found compatibility report " + report.getAbsolutePath());
JsonArray content = new JsonArray(Files.readString(report.toPath()));
reports.put(child2.getName(), content);
}
}
}
}
}
}
StringBuilder buffer = new StringBuilder();
System.out.println(reports.size() + " reports found");
buffer.append("# API Change analysis\n");
ObjectMapper mapper = new ObjectMapper();
for (Map.Entry<String, JsonArray> entry : reports.entrySet()) {
JsonArray diff = entry.getValue();
List<Difference> differences = mapper.readValue(diff.encode(), new TypeReference<List<Difference>>() {
})
.stream()
.filter(f -> !f.isCompatible())
.collect(Collectors.toList());
if (differences.isEmpty()) {
System.out.println("Skipping " + entry.getKey() + " - no changes or compatible changes");
continue;
}
buffer.append("\n## Incompatible changes in ").append(entry.getKey()).append("\n");
buffer.append("[cols=\"1,1,1,1\", options=\"header\"]\n");
buffer.append("|===\n");
buffer.append("| Element | Classification | Criticality | Description\n");
buffer.append("\n");
for (Difference difference : differences) {
buffer.append("| `");
buffer.append(difference.getOldCode());
buffer.append("` a| ");
buffer.append(difference.getCompatibility());
buffer.append(" | " );
buffer.append(difference.getCriticality());
buffer.append(" | ");
buffer.append(difference.getDescription());
buffer.append("\n");
buffer.append("\n");
}
buffer.append("|===\n");
buffer.append("'''\n");
}
File target = new File("target");
target.mkdirs();
Files.write(new File(target, "compatibility-report.adoc").toPath(), buffer.toString().getBytes());
}
private static boolean isCompatible(List<Difference> diffs) {
boolean isCompatible = true;
for (Difference diff : diffs) {
isCompatible = isCompatible && diff.isCompatible();
}
return isCompatible;
}
public static class Compatibility {
private String compatibility;
private String severity;
public String getCompatibility() {
return compatibility;
}
public Compatibility setCompatibility(String compatibility) {
this.compatibility = compatibility;
return this;
}
public String getSeverity() {
return severity;
}
public Compatibility setSeverity(String severity) {
this.severity = severity;
return this;
}
}
public static class Attachment {
private String name;
private String value;
public String getName() {
return name;
}
public Attachment setName(String name) {
this.name = name;
return this;
}
public String getValue() {
return value;
}
public Attachment setValue(String value) {
this.value = value;
return this;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Difference {
private String code;
@JsonProperty("old")
private String oldCode;
@JsonProperty("new")
private String newCode;
private String name;
private String criticality;
private String description;
private Compatibility[] classification;
private Attachment[] attachments;
private String justification;
public String getPackage() {
return findAttachmentByName("package");
}
public String getClassName() {
return findAttachmentByName("classQualifiedName");
}
public String getSimpleClassName() {
return findAttachmentByName("classSimpleName");
}
public String getMethodName() {
return findAttachmentByName("methodName");
}
public String getFieldName() {
return findAttachmentByName("fieldName");
}
public String getNewArchive() {
return findAttachmentByName("newArchive");
}
public String getCriticality() {
return criticality;
}
private String findAttachmentByName(String name) {
if (attachments == null) {
return null;
} else {
return Arrays.stream(attachments).filter(a -> a.name.equalsIgnoreCase(name))
.map(a -> a.value)
.findFirst()
.orElse(null);
}
}
@Override
public String toString() {
return description;
}
private String findCompatibilityByName(String name) {
if (classification == null) {
return null;
} else {
return Arrays.stream(classification).filter(a -> a.compatibility.equalsIgnoreCase(name))
.map(a -> a.severity)
.findFirst()
.orElse(null);
}
}
private static final List<String> COMPATIBLE = Arrays.asList("NON_BREAKING", "EQUIVALENT");
public boolean isCompatible() {
String source = findCompatibilityByName("SOURCE");
String binary = findCompatibilityByName("BINARY");
boolean isSourceCompatible = COMPATIBLE.contains(source.toUpperCase());
boolean isBinaryCompatible = COMPATIBLE.contains(binary.toUpperCase());
return isSourceCompatible && isBinaryCompatible;
}
public String getElement() {
String name = getMethodName();
if (name == null) {
name = getFieldName();
}
return getClassName() + "#" + name;
}
public String getCompatibility() {
return "\n* Source: " + findCompatibilityByName("SOURCE") + " \n " + "* Binary: "
+ findCompatibilityByName("BINARY");
}
public String getCode() {
return code;
}
public Difference setCode(String code) {
this.code = code;
return this;
}
public String getOldCode() {
return oldCode;
}
public Difference setOldCode(String oldCode) {
this.oldCode = oldCode;
return this;
}
public String getNewCode() {
return newCode;
}
public Difference setNewCode(String newCode) {
this.newCode = newCode;
return this;
}
public String getName() {
return name;
}
public Difference setName(String name) {
this.name = name;
return this;
}
public String getDescription() {
return description;
}
public Difference setDescription(String description) {
this.description = description;
return this;
}
public Compatibility[] getClassification() {
return classification;
}
public Difference setClassification(Compatibility[] classification) {
this.classification = classification;
return this;
}
public Attachment[] getAttachments() {
return attachments;
}
public Difference setAttachments(Attachment[] attachments) {
this.attachments = attachments;
return this;
}
}
}