From c8adab5136ea575ba367ea1642d7e8986335c945 Mon Sep 17 00:00:00 2001 From: Mattis Bratland Date: Wed, 22 Jul 2026 13:50:16 +0200 Subject: [PATCH] chore: use Spotless with ktlint Replace the ktlint Gradle wrapper with Spotless backed by ktlint 1.8.0, and update CI and the pre-commit hook to use Spotless tasks. --- .github/workflows/app-test.yaml | 4 +-- build.gradle.kts | 11 +++++++- get_started.sh => get-started.sh | 0 gradle/libs.versions.toml | 4 ++- scripts/hooks/pre-commit | 8 +++--- .../CustomGenericKubernetesResourceMatcher.kt | 25 ++++++++++++++----- .../extensions/KubernetesResourceSource.kt | 8 ++++-- 7 files changed, 44 insertions(+), 16 deletions(-) rename get_started.sh => get-started.sh (100%) diff --git a/.github/workflows/app-test.yaml b/.github/workflows/app-test.yaml index 00bf067..af9047e 100644 --- a/.github/workflows/app-test.yaml +++ b/.github/workflows/app-test.yaml @@ -38,8 +38,8 @@ jobs: - name: Setup Gradle uses: gradle/actions/setup-gradle@v6 - - name: Run ktlint Check - run: ./gradlew :ktlintCheck --stacktrace --configuration-cache + - name: Run Spotless Check + run: ./gradlew :spotlessCheck --stacktrace --configuration-cache unit-tests: name: Run Unit Tests diff --git a/build.gradle.kts b/build.gradle.kts index 3066520..fca2bef 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { kotlin("jvm") application alias(libs.plugins.fabric8.generator) - alias(libs.plugins.ktlint) + alias(libs.plugins.spotless) } group = "no.fintlabs" @@ -187,6 +187,15 @@ tasks { } } +spotless { + kotlin { + ktlint(libs.versions.ktlint.get()) + } + kotlinGradle { + ktlint(libs.versions.ktlint.get()) + } +} + fun fetchNumCores(): Int = Runtime .getRuntime() diff --git a/get_started.sh b/get-started.sh similarity index 100% rename from get_started.sh rename to get-started.sh diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4c25153..bbbe9dd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,6 +11,8 @@ jackson = "2.21.5" logunit = "2.0.0" http4k = "6.53.0.0" micrometer = "1.17.0" +ktlint = "1.8.0" +spotless = "8.8.0" [libraries] fabric8-kubernetes-client = { module = "io.fabric8:kubernetes-client", version.ref = "fabric8" } @@ -51,7 +53,7 @@ koin-test-junit5 = { module = "io.insert-koin:koin-test-junit5" } [plugins] kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version = "2.4.0"} fabric8-generator = { id = "io.fabric8.java-generator", version.ref = "fabric8" } -ktlint = { id = "org.jlleitschuh.gradle.ktlint", version="14.2.0" } +spotless = { id = "com.diffplug.spotless", version.ref = "spotless" } [bundles] operator = ["operator-framework-core", "operator-micrometer-support"] diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit index 913387b..756aa53 100755 --- a/scripts/hooks/pre-commit +++ b/scripts/hooks/pre-commit @@ -3,7 +3,7 @@ repository_root=$(git rev-parse --show-toplevel) cd "$repository_root" || exit 1 -echo "[git pre-commit hook] Running ktlintFormat before commit." +echo "[git pre-commit hook] Running spotlessApply before commit." staged_files=$(git diff --name-only --cached --diff-filter=d) if [ -z "$staged_files" ]; then @@ -11,13 +11,13 @@ if [ -z "$staged_files" ]; then exit 0 fi -./gradlew :ktlintFormat --configuration-cache +./gradlew :spotlessApply --configuration-cache --daemon status=$? -echo "Finished ktlintFormat with status: $status" +echo "Finished spotlessApply with status: $status" if [ "$status" -ne 0 ]; then - echo "Error running ktlintFormat, aborting commit." + echo "Error running spotlessApply, aborting commit." exit "$status" fi diff --git a/src/main/kotlin/no/fintlabs/application/matcher/CustomGenericKubernetesResourceMatcher.kt b/src/main/kotlin/no/fintlabs/application/matcher/CustomGenericKubernetesResourceMatcher.kt index 193a090..de3adf4 100644 --- a/src/main/kotlin/no/fintlabs/application/matcher/CustomGenericKubernetesResourceMatcher.kt +++ b/src/main/kotlin/no/fintlabs/application/matcher/CustomGenericKubernetesResourceMatcher.kt @@ -138,16 +138,25 @@ class CustomGenericKubernetesResourceMatcher { resourceMap: MutableMap, ) { when (resource) { - is Pod -> resource.spec.containers.normalizeContainers(resourceMap) - is Deployment -> + is Pod -> { + resource.spec.containers.normalizeContainers(resourceMap) + } + + is Deployment -> { resource.spec.template.spec.containers .normalizeContainers(resourceMap) - is Job -> + } + + is Job -> { resource.spec.template.spec.containers .normalizeContainers(resourceMap) - is ReplicaSet -> + } + + is ReplicaSet -> { resource.spec.template.spec.containers .normalizeContainers(resourceMap) + } + is StatefulSet -> { resource.spec.template.spec.containers .normalizeContainers(resourceMap) @@ -162,10 +171,14 @@ class CustomGenericKubernetesResourceMatcher { ) } } - is PersistentVolumeClaim -> + + is PersistentVolumeClaim -> { resource.spec.resources?.normalizeResources(resourceMap, "spec", "resources") - is PersistentVolume -> + } + + is PersistentVolume -> { resource.spec.capacity?.normalizeQuantity(resourceMap, "spec", "capacity") + } } } diff --git a/src/test/integration/kotlin/no/fintlabs/extensions/KubernetesResourceSource.kt b/src/test/integration/kotlin/no/fintlabs/extensions/KubernetesResourceSource.kt index 0899b73..ca45386 100644 --- a/src/test/integration/kotlin/no/fintlabs/extensions/KubernetesResourceSource.kt +++ b/src/test/integration/kotlin/no/fintlabs/extensions/KubernetesResourceSource.kt @@ -28,9 +28,13 @@ class KubernetesResourceSource( private fun fromPath(path: Path): List = when { - Files.isDirectory(path) -> + Files.isDirectory(path) -> { path.toFile().listFiles()?.flatMap { fromPath(it.toPath()) } ?: emptyList() - else -> listOf(KubernetesResourceSource(path.toFile())) + } + + else -> { + listOf(KubernetesResourceSource(path.toFile())) + } } fun fromResources(resources: List): List =