Skip to content

Commit

Permalink
[8.0] Use Java 15 text blocks for JSON and multiline strings (#80751) (
Browse files Browse the repository at this point in the history
…#81771)

* [8.0] Use Java 15 text blocks for JSON and multiline strings (#80751)

The ES code base is quite JSON heavy. It uses a lot of multi-line JSON requests in tests which need to be escaped and concatenated which in turn makes them hard to read. Let's try to leverage Java 15 text blocks for representing them.

Backports #80751 to 8.0
  • Loading branch information
arteam authored Dec 15, 2021
1 parent f613df6 commit daa4be4
Show file tree
Hide file tree
Showing 722 changed files with 28,192 additions and 24,597 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,55 +85,46 @@ void generateDependenciesGraph() {
for (final Dependency dependency : runtimeDependencies) {
final String id = dependency.getGroup() + ":" + dependency.getName();
final String versionedId = id + "@" + dependency.getVersion();
final StringBuilder packageString = new StringBuilder();
final StringBuilder nodeString = new StringBuilder();
if (dependency instanceof ProjectDependency) {
continue;
}
packageString.append("{\"id\": \"")
.append(versionedId)
.append("\",\"info\": {\"name\": \"")
.append(id)
.append("\",\"version\": \"")
.append(dependency.getVersion())
.append("\"}}");
packages.add(packageString.toString());
nodeString.append("{\"nodeId\": \"")
.append(versionedId)
.append("\",\"pkgId\": \"")
.append(versionedId)
.append("\",\"deps\": []}");
packages.add("""
{"id": "%s","info": {"name": "%s","version": "%s"}}\
""".formatted(versionedId, id, dependency.getVersion()));
nodeString.append("""
{"nodeId": "%s","pkgId": "%s","deps": []}\
""".formatted(versionedId, versionedId));
nodes.add(nodeString.toString());
nodeIds.add("{\"nodeId\": \"" + versionedId + "\"}");
nodeIds.add("""
{"nodeId": "%s"}\
""".formatted(versionedId));
}
// We add one package and one node for each dependency, it suffices to check packages.
if (packages.size() > 0) {
final String projectName = "elastic/elasticsearch" + getProject().getPath();
final StringBuilder output = new StringBuilder();
output.append("{\"depGraph\": {\"schemaVersion\": \"1.2.0\",\"pkgManager\": {\"name\": \"gradle\"},\"pkgs\": [")
.append("{\"id\": \"")
.append(projectName)
.append("@0.0.0")
.append("\", \"info\": {\"name\": \"")
.append(projectName)
.append("\", \"version\": \"0.0.0\"}},")
.append(String.join(",", packages))
.append("],\"graph\": {\"rootNodeId\": \"")
.append(projectName)
.append("@0.0.0")
.append("\",\"nodes\": [")
.append("{\"nodeId\": \"")
.append(projectName)
.append("@0.0.0")
.append("\",\"pkgId\": \"")
.append(projectName)
.append("@0.0.0")
.append("\",\"deps\": [")
.append(String.join(",", nodeIds))
.append("]},")
.append(String.join(",", nodes))
.append("]}}}");
getLogger().debug("Dependency Graph: " + output.toString());
final String output = """
{
"depGraph": {
"schemaVersion": "1.2.0",
"pkgManager": {"name": "gradle"},
"pkgs": [
{
"id": "%s@0.0.0",
"info": {"name": "%1$s", "version": "0.0.0"}
},
%s
],
"graph": {
"rootNodeId": "%1$s@0.0.0",
"nodes": [
{ "nodeId": "%1$s@0.0.0","pkgId": "%1$s@0.0.0","deps": [%s] },
%s
]
}
}
}""".formatted(projectName, String.join(",", packages), String.join(",", nodeIds), String.join(",", nodes));
getLogger().debug("Dependency Graph: " + output);
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost postRequest = new HttpPost(url);
postRequest.addHeader("Authorization", "token " + token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ void failIfDockerUnavailable(List<String> tasks) {
// Some other problem, print the error
final String message = String.format(
Locale.ROOT,
"a problem occurred while using Docker from [%s]%s yet it is required to run the following task%s: \n%s\n"
+ "the problem is that Docker exited with exit code [%d] with standard error output:\n%s",
"""
a problem occurred while using Docker from [%s]%s yet it is required to run the following task%s:
%s
the problem is that Docker exited with exit code [%d] with standard error output:
%s""",
availability.path,
availability.version == null ? "" : " v" + availability.version,
tasks.size() > 1 ? "s" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,11 @@ private void checkSha(File jar, String jarName, Set<File> shaFiles) throws NoSuc
String sha = getSha1(jar);

if (expectedSha.equals(sha) == false) {
final String exceptionMessage = String.format(
Locale.ROOT,
"SHA has changed! Expected %s for %s but got %s."
+ "\nThis usually indicates a corrupt dependency cache or artifacts changed upstream."
+ "\nEither wipe your cache, fix the upstream artifact, or delete %s and run updateShas",
expectedSha,
jarName,
sha,
shaFile
);
final String exceptionMessage = String.format(Locale.ROOT, """
SHA has changed! Expected %s for %s but got %s.
This usually indicates a corrupt dependency cache or artifacts changed upstream.
Either wipe your cache, fix the upstream artifact, or delete %s and run updateShas
""", expectedSha, jarName, sha, shaFile);

throw new GradleException(exceptionMessage);
}
Expand Down
Loading

0 comments on commit daa4be4

Please sign in to comment.