Skip to content

Commit 539125e

Browse files
committed
Issue #109 Address code review feedback
- Exit non-zero on IOException when writing output files - Use hex format for fallback fingerprint (%07x) - Use report timestamp instead of Instant.now() in summary - Extract getDifferentApiCount() helper to reduce duplication - Use helper in generateFingerprint(), generateSummary(), hasDifferences()
1 parent 206ad18 commit 539125e

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

json-java21-api-tracker/src/main/java/io/github/simbo1905/tracker/ApiTracker.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,7 @@ static String fetchUpstreamSource(String className) {
956956
/// @param report the full comparison report
957957
/// @return 7-character fingerprint or "0000000" if no differences
958958
static String generateFingerprint(JsonObject report) {
959-
final var summary = (JsonObject) report.members().get("summary");
960-
final var differentApi = ((JsonNumber) summary.members().get("differentApi")).toNumber().longValue();
961-
962-
if (differentApi == 0) {
959+
if (getDifferentApiCount(report) == 0) {
963960
return "0000000";
964961
}
965962

@@ -1002,10 +999,25 @@ static String generateFingerprint(JsonObject report) {
1002999
return hexString.substring(0, 7);
10031000
} catch (NoSuchAlgorithmException e) {
10041001
LOGGER.warning("SHA-256 not available, using fallback fingerprint");
1005-
return String.format("%07d", stableString.hashCode() & 0xFFFFFFF);
1002+
return String.format("%07x", stableString.hashCode() & 0xFFFFFFF);
10061003
}
10071004
}
10081005

1006+
/// Extracts the differentApi count from a report summary
1007+
/// @param report the comparison report
1008+
/// @return the count of classes with different APIs
1009+
private static long getDifferentApiCount(JsonObject report) {
1010+
final var summary = (JsonObject) report.members().get("summary");
1011+
if (summary == null) {
1012+
return 0;
1013+
}
1014+
final var differentApiValue = summary.members().get("differentApi");
1015+
if (differentApiValue instanceof JsonNumber num) {
1016+
return num.toNumber().longValue();
1017+
}
1018+
return 0;
1019+
}
1020+
10091021
/// Generates a terse human-readable summary of the API differences
10101022
/// Suitable for GitHub issue body
10111023
/// @param report the full comparison report
@@ -1017,7 +1029,7 @@ static String generateSummary(JsonObject report) {
10171029

10181030
final var totalClasses = ((JsonNumber) summary.members().get("totalClasses")).toNumber().longValue();
10191031
final var matchingClasses = ((JsonNumber) summary.members().get("matchingClasses")).toNumber().longValue();
1020-
final var differentApi = ((JsonNumber) summary.members().get("differentApi")).toNumber().longValue();
1032+
final var differentApi = getDifferentApiCount(report);
10211033
final var missingUpstream = ((JsonNumber) summary.members().get("missingUpstream")).toNumber().longValue();
10221034

10231035
sb.append("## API Comparison Summary\n\n");
@@ -1066,7 +1078,8 @@ static String generateSummary(JsonObject report) {
10661078
}
10671079

10681080
sb.append("---\n");
1069-
sb.append("*Generated by API Tracker on ").append(Instant.now().toString().split("T")[0]).append("*\n");
1081+
final var timestamp = ((JsonString) report.members().get("timestamp")).value();
1082+
sb.append("*Generated by API Tracker on ").append(timestamp.split("T")[0]).append("*\n");
10701083

10711084
return sb.toString();
10721085
}
@@ -1075,8 +1088,6 @@ static String generateSummary(JsonObject report) {
10751088
/// @param report the comparison report
10761089
/// @return true if differentApi > 0
10771090
static boolean hasDifferences(JsonObject report) {
1078-
final var summary = (JsonObject) report.members().get("summary");
1079-
final var differentApi = ((JsonNumber) summary.members().get("differentApi")).toNumber().longValue();
1080-
return differentApi > 0;
1091+
return getDifferentApiCount(report) > 0;
10811092
}
10821093
}

json-java21-api-tracker/src/main/java/io/github/simbo1905/tracker/ApiTrackerRunner.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ private static void writeOutputFiles(String jsonOutput, String fingerprint, Stri
9696
System.out.println();
9797
System.out.println("Output files written to: " + outputDir.toAbsolutePath());
9898
} catch (IOException e) {
99-
System.err.println("Warning: Could not write output files: " + e.getMessage());
99+
System.err.println("Error: Could not write output files: " + e.getMessage());
100+
//noinspection CallToPrintStackTrace
101+
e.printStackTrace();
102+
System.exit(1);
100103
}
101104
}
102105

0 commit comments

Comments
 (0)