From c12e164ba3d718de66b2429066dd8bb9e28fcc0f Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Tue, 3 Sep 2024 16:32:29 +1200 Subject: [PATCH 1/5] Forbid wildcard imports Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: https://github.com/diffplug/spotless/issues/649 Signed-off-by: Thomas Farr --- java-client/build.gradle.kts | 10 ++++++++++ java-codegen/build.gradle.kts | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts index 5aeb1ffcde..103c027e6c 100644 --- a/java-client/build.gradle.kts +++ b/java-client/build.gradle.kts @@ -321,6 +321,16 @@ spotless { trimTrailingWhitespace() endWithNewline() + + val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE) + custom("Refuse wildcard imports") { contents -> + // Wildcard imports can't be resolved by spotless itself. + // This will require the developer themselves to adhere to best practices. + if (wildcardImportRegex.containsMatchIn(contents)) { + throw AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.") + } + contents + } } } diff --git a/java-codegen/build.gradle.kts b/java-codegen/build.gradle.kts index dbae78b0d9..7f088a09ab 100644 --- a/java-codegen/build.gradle.kts +++ b/java-codegen/build.gradle.kts @@ -273,5 +273,15 @@ spotless { trimTrailingWhitespace() endWithNewline() + + val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE) + custom("Refuse wildcard imports") { contents -> + // Wildcard imports can't be resolved by spotless itself. + // This will require the developer themselves to adhere to best practices. + if (wildcardImportRegex.containsMatchIn(contents)) { + throw AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.") + } + contents + } } } \ No newline at end of file From 9d8159a6262e5b6baf5cfda8abae295368e4226c Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Wed, 4 Sep 2024 12:12:11 +1200 Subject: [PATCH 2/5] Make conventions plugin Signed-off-by: Thomas Farr --- build.gradle.kts | 2 +- buildSrc/build.gradle.kts | 12 +++-- ...earch-java.spotless-conventions.gradle.kts | 46 +++++++++++++++++++ java-client/build.gradle.kts | 40 ++-------------- java-codegen/build.gradle.kts | 32 ++----------- samples/build.gradle.kts | 19 +------- 6 files changed, 64 insertions(+), 87 deletions(-) create mode 100644 buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts diff --git a/build.gradle.kts b/build.gradle.kts index 7779a42fd3..7e8dd668d4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,7 +40,7 @@ allprojects { mavenLocal() maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots") mavenCentral() - maven(url = "https://plugins.gradle.org/m2/") + gradlePluginPortal() } } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1d4c7ce452..19190b03bd 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -30,13 +30,19 @@ * GitHub history for details. */ -dependencies { - implementation("org.ajoberstar.grgit:grgit-gradle:5.2.2") +plugins { + `kotlin-dsl` } repositories { mavenLocal() maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots") mavenCentral() - maven(url = "https://plugins.gradle.org/m2/") + gradlePluginPortal() } + +dependencies { + implementation("org.ajoberstar.grgit:grgit-gradle:5.2.2") + implementation("com.diffplug.spotless", "spotless-plugin-gradle", "6.25.0") +} + diff --git a/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts b/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts new file mode 100644 index 0000000000..1d3a0fbf88 --- /dev/null +++ b/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts @@ -0,0 +1,46 @@ +plugins { + id("com.diffplug.spotless") +} + +spotless { + java { + target("**/*.java") + + licenseHeaderFile(rootProject.file("LICENSE_HEADER.txt")) + .named("PrimaryLicenseHeader") + .onlyIfContentMatches("^((?!Licensed to Elasticsearch)[\\s\\S])*$") + .delimiter("(package |//-----)") + + licenseHeaderFile(rootProject.file("LICENSE_HEADER_FORKED.txt")) + .named("ForkedLicenseHeader") + .onlyIfContentMatches("Licensed to Elasticsearch") + .delimiter("(package |//-----)") + + // Use the default importOrder configuration + importOrder() + removeUnusedImports() + + eclipse().configFile(rootProject.file("buildSrc/formatterConfig.xml")) + + trimTrailingWhitespace() + endWithNewline() + + val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE) + custom("Refuse wildcard imports") { contents -> + // Wildcard imports can't be resolved by spotless itself. + // This will require the developer themselves to adhere to best practices. + val wildcardImports = wildcardImportRegex.findAll(contents) + if (wildcardImports.any()) { + throw AssertionError( + """ + Please replace the following wildcard imports with explicit imports ('spotlessApply' cannot resolve this issue): + + ${wildcardImports.joinToString("\n") { "\t- ${it.value}" }} + + """.trimIndent() + ) + } + contents + } + } +} \ No newline at end of file diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts index 103c027e6c..7744fea2d7 100644 --- a/java-client/build.gradle.kts +++ b/java-client/build.gradle.kts @@ -40,7 +40,7 @@ buildscript { mavenLocal() maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots") mavenCentral() - maven(url = "https://plugins.gradle.org/m2/") + gradlePluginPortal() } dependencies { "classpath"(group = "org.opensearch.gradle", name = "build-tools", version = "3.0.0-SNAPSHOT") @@ -53,7 +53,8 @@ plugins { `maven-publish` id("com.github.jk1.dependency-license-report") version "2.9" id("org.owasp.dependencycheck") version "10.0.4" - id("com.diffplug.spotless") version "6.25.0" + + id("opensearch-java.spotless-conventions") } apply(plugin = "opensearch.repositories") apply(plugin = "org.owasp.dependencycheck") @@ -299,41 +300,6 @@ tasks.withType { } } -spotless { - java { - target("**/*.java") - - licenseHeaderFile("../LICENSE_HEADER.txt") - .named("PrimaryLicenseHeader") - .onlyIfContentMatches("^((?!Licensed to Elasticsearch)[\\s\\S])*$") - .delimiter("(package |//-----)") - - licenseHeaderFile("../LICENSE_HEADER_FORKED.txt") - .named("ForkedLicenseHeader") - .onlyIfContentMatches("Licensed to Elasticsearch") - .delimiter("(package |//-----)") - - // Use the default importOrder configuration - importOrder() - removeUnusedImports() - - eclipse().configFile("../buildSrc/formatterConfig.xml") - - trimTrailingWhitespace() - endWithNewline() - - val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE) - custom("Refuse wildcard imports") { contents -> - // Wildcard imports can't be resolved by spotless itself. - // This will require the developer themselves to adhere to best practices. - if (wildcardImportRegex.containsMatchIn(contents)) { - throw AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.") - } - contents - } - } -} - publishing { repositories{ if (version.toString().endsWith("SNAPSHOT")) { diff --git a/java-codegen/build.gradle.kts b/java-codegen/build.gradle.kts index 7f088a09ab..db6e3d2eca 100644 --- a/java-codegen/build.gradle.kts +++ b/java-codegen/build.gradle.kts @@ -21,7 +21,7 @@ buildscript { mavenLocal() maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots") mavenCentral() - maven(url = "https://plugins.gradle.org/m2/") + gradlePluginPortal() } dependencies { "classpath"(group = "org.opensearch.gradle", name = "build-tools", version = "3.0.0-SNAPSHOT") @@ -32,8 +32,9 @@ plugins { application id("com.github.jk1.dependency-license-report") version "2.9" id("org.owasp.dependencycheck") version "10.0.2" - id("com.diffplug.spotless") version "6.25.0" id("de.undercouch.download") version "5.6.0" + + id("opensearch-java.spotless-conventions") } apply(plugin = "opensearch.repositories") apply(plugin = "org.owasp.dependencycheck") @@ -257,31 +258,4 @@ tasks.withType { "checksum"("algorithm" to "sha-512", "file" to archiveFile.get(), "fileext" to ".sha512") } } -} - -spotless { - java { - target("**/*.java") - - licenseHeaderFile("../LICENSE_HEADER.txt") - - // Use the default importOrder configuration - importOrder() - removeUnusedImports() - - eclipse().configFile("../buildSrc/formatterConfig-generated.xml") - - trimTrailingWhitespace() - endWithNewline() - - val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE) - custom("Refuse wildcard imports") { contents -> - // Wildcard imports can't be resolved by spotless itself. - // This will require the developer themselves to adhere to best practices. - if (wildcardImportRegex.containsMatchIn(contents)) { - throw AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.") - } - contents - } - } } \ No newline at end of file diff --git a/samples/build.gradle.kts b/samples/build.gradle.kts index 0d9577107e..1ca5c814c9 100644 --- a/samples/build.gradle.kts +++ b/samples/build.gradle.kts @@ -9,7 +9,8 @@ plugins { java application - id("com.diffplug.spotless") version "6.25.0" + + id("opensearch-java.spotless-conventions") } java { @@ -26,22 +27,6 @@ dependencies { implementation("com.fasterxml.jackson.core", "jackson-databind", "2.15.2") } -spotless { - java { - - target("**/*.java") - - // Use the default importOrder configuration - importOrder() - removeUnusedImports() - - eclipse().configFile("../buildSrc/formatterConfig.xml") - - trimTrailingWhitespace() - endWithNewline() - } -} - application { mainClass.set("org.opensearch.client.samples.Main") } From 4d41ff39a5be2cd4de4ab36e41961c6f62447eca Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Wed, 4 Sep 2024 12:20:59 +1200 Subject: [PATCH 3/5] Fix wildcard imports Signed-off-by: Thomas Farr --- .../_types/mapping/IcuCollationKeywordProperty.java | 6 +++++- .../opensearch/integTest/aws/AwsSdk2GetRequestIT.java | 3 ++- .../org/opensearch/client/codegen/CodeGenerator.java | 6 +++++- .../org/opensearch/client/samples/FlatObjectBasics.java | 8 ++++++++ .../org/opensearch/client/samples/IndexTemplates.java | 9 ++++++++- .../opensearch/client/samples/util/IssueDocument.java | 8 ++++++++ 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/IcuCollationKeywordProperty.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/IcuCollationKeywordProperty.java index 17f3928655..0876e46265 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/IcuCollationKeywordProperty.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/IcuCollationKeywordProperty.java @@ -11,7 +11,11 @@ import jakarta.json.stream.JsonGenerator; import java.util.function.Function; import javax.annotation.Nullable; -import org.opensearch.client.json.*; +import org.opensearch.client.json.JsonpDeserializable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.ObjectBuilderDeserializer; +import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.opensearch._types.analysis.IcuCollationAlternate; import org.opensearch.client.opensearch._types.analysis.IcuCollationCaseFirst; import org.opensearch.client.opensearch._types.analysis.IcuCollationDecomposition; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2GetRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2GetRequestIT.java index 03dc5afb16..90f0a8841a 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2GetRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2GetRequestIT.java @@ -13,7 +13,8 @@ import org.junit.Test; import org.opensearch.client.opensearch.OpenSearchAsyncClient; import org.opensearch.client.opensearch.OpenSearchClient; -import org.opensearch.client.opensearch.core.*; +import org.opensearch.client.opensearch.core.GetRequest; +import org.opensearch.client.opensearch.core.GetResponse; public class AwsSdk2GetRequestIT extends AwsSdk2TransportTestCase { @Test diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java b/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java index 210079ccc2..9d59d803e1 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java @@ -8,7 +8,11 @@ package org.opensearch.client.codegen; -import static org.opensearch.client.codegen.model.OperationGroupMatcher.*; +import static org.opensearch.client.codegen.model.OperationGroupMatcher.and; +import static org.opensearch.client.codegen.model.OperationGroupMatcher.named; +import static org.opensearch.client.codegen.model.OperationGroupMatcher.namespace; +import static org.opensearch.client.codegen.model.OperationGroupMatcher.not; +import static org.opensearch.client.codegen.model.OperationGroupMatcher.or; import java.io.File; import java.io.IOException; diff --git a/samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java b/samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java index 240e53c67e..41c8a0615b 100644 --- a/samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java +++ b/samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java @@ -1,3 +1,11 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + package org.opensearch.client.samples; import java.util.List; diff --git a/samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java b/samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java index e086652f7a..3f43a07048 100644 --- a/samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java +++ b/samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java @@ -13,7 +13,14 @@ import org.apache.logging.log4j.Logger; import org.opensearch.client.opensearch._types.Time; import org.opensearch.client.opensearch.cluster.PutComponentTemplateRequest; -import org.opensearch.client.opensearch.indices.*; +import org.opensearch.client.opensearch.indices.CreateIndexRequest; +import org.opensearch.client.opensearch.indices.DeleteIndexRequest; +import org.opensearch.client.opensearch.indices.DeleteIndexTemplateRequest; +import org.opensearch.client.opensearch.indices.GetIndicesSettingsRequest; +import org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse; +import org.opensearch.client.opensearch.indices.GetMappingRequest; +import org.opensearch.client.opensearch.indices.GetMappingResponse; +import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest; /** * Run with: ./gradlew :samples:run -Dsamples.mainClass=IndexTemplates diff --git a/samples/src/main/java/org/opensearch/client/samples/util/IssueDocument.java b/samples/src/main/java/org/opensearch/client/samples/util/IssueDocument.java index 45e212ed1f..db3077d551 100644 --- a/samples/src/main/java/org/opensearch/client/samples/util/IssueDocument.java +++ b/samples/src/main/java/org/opensearch/client/samples/util/IssueDocument.java @@ -1,3 +1,11 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + package org.opensearch.client.samples.util; import java.util.List; From d0117493b8b0848e9b763acda1352b96163d8ab9 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Wed, 4 Sep 2024 15:12:11 +1200 Subject: [PATCH 4/5] Allow overriding eclipse formatter config file Signed-off-by: Thomas Farr --- ...opensearch-java.spotless-conventions.gradle.kts | 14 +++++++++++++- java-codegen/build.gradle.kts | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts b/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts index 1d3a0fbf88..489a81aec9 100644 --- a/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts @@ -2,6 +2,14 @@ plugins { id("com.diffplug.spotless") } +interface SpotlessConventionsPluginExtension { + val eclipseFormatterConfigFile: RegularFileProperty +} + +val extension = project.extensions.create("spotlessConventions") + +extension.eclipseFormatterConfigFile.convention(rootProject.layout.projectDirectory.file("buildSrc/formatterConfig.xml")) + spotless { java { target("**/*.java") @@ -20,11 +28,15 @@ spotless { importOrder() removeUnusedImports() - eclipse().configFile(rootProject.file("buildSrc/formatterConfig.xml")) + eclipse().configFile(extension.eclipseFormatterConfigFile) trimTrailingWhitespace() endWithNewline() + // NOTE: Any time a custom step below is modified, bump this number. + // Allows up-to-date checks to work correctly with custom steps. + bumpThisNumberIfACustomStepChanges(1) + val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE) custom("Refuse wildcard imports") { contents -> // Wildcard imports can't be resolved by spotless itself. diff --git a/java-codegen/build.gradle.kts b/java-codegen/build.gradle.kts index db6e3d2eca..c95fe1be78 100644 --- a/java-codegen/build.gradle.kts +++ b/java-codegen/build.gradle.kts @@ -258,4 +258,8 @@ tasks.withType { "checksum"("algorithm" to "sha-512", "file" to archiveFile.get(), "fileext" to ".sha512") } } +} + +spotlessConventions { + eclipseFormatterConfigFile = rootProject.file("buildSrc/formatterConfig-generated.xml") } \ No newline at end of file From 0d9eb7eeacdf4a7a9760ada3ae10db956731553b Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Wed, 4 Sep 2024 15:41:36 +1200 Subject: [PATCH 5/5] Fix message formatting Signed-off-by: Thomas Farr --- ...opensearch-java.spotless-conventions.gradle.kts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts b/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts index 489a81aec9..8f03e2d995 100644 --- a/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/opensearch-java.spotless-conventions.gradle.kts @@ -43,14 +43,14 @@ spotless { // This will require the developer themselves to adhere to best practices. val wildcardImports = wildcardImportRegex.findAll(contents) if (wildcardImports.any()) { - throw AssertionError( - """ + var msg = """ Please replace the following wildcard imports with explicit imports ('spotlessApply' cannot resolve this issue): - - ${wildcardImports.joinToString("\n") { "\t- ${it.value}" }} - - """.trimIndent() - ) + """.trimIndent() + wildcardImports.forEach { + msg += "\n\t- ${it.value}" + } + msg += "\n" + throw AssertionError(msg) } contents }