From d5a90fcbb6e4fe9bf40381b4556fe1ab86d91886 Mon Sep 17 00:00:00 2001 From: Matthew Pope Date: Fri, 12 Apr 2024 09:25:52 -0700 Subject: [PATCH] Adds check for license headers and unused imports --- .github/workflows/main.yml | 2 + build.gradle.kts | 55 ++++++++++++++++++- .../com/amazon/ion/_Private_Trampoline.kt | 2 + 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1782ce30a5..6063c87a06 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,6 +23,8 @@ jobs: - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 with: submodules: recursive + # Need to also fetch origin/master for the "rachetFrom" functionality of the "spotless" gradle plugin + - run: git fetch origin/master - uses: gradle/wrapper-validation-action@b231772637bb498f11fdbc86052b6e8a8dc9fc92 # v2.1.2 - name: Use java ${{ matrix.java }} uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1 diff --git a/build.gradle.kts b/build.gradle.kts index 1d7ae0975f..ebcfad7fd0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,8 +30,8 @@ plugins { id("org.cyclonedx.bom") version "1.7.2" id("com.github.spotbugs") version "5.0.13" id("org.jlleitschuh.gradle.ktlint") version "11.3.2" - // TODO: more static analysis. E.g.: - // id("com.diffplug.spotless") version "6.11.0" + + id("com.diffplug.spotless") version "6.11.0" // Used for generating the third party attribution document id("com.github.jk1.dependency-license-report") version "2.5" @@ -99,6 +99,57 @@ licenseReport { ) } +/** + * This is the `git remote` name that corresponds to amazon-ion/ion-java. + * It is used for applying the "spotless" checks only to things that are changed + * compared to the master branch of the source repo. + */ +val sourceRepoRemoteName: String by lazy { + val git = System.getenv("GIT_CLI") ?: "git" + + fun String.isSourceRepo(): Boolean { + val url = "$git remote get-url ${this@isSourceRepo}".runCommand() + return "amazon-ion/ion-java" in url || "amzn/ion-java" in url + } + + "$git remote".runCommand().lines().firstOrNull { it.isSourceRepo() } + ?: throw Exception( + """ + |No git remote found for amazon-ion/ion-java. Try again after running: + | + | git remote add -f https://github.com/amazon-ion/ion-java/ + """.trimMargin() + ) +} + +fun String.runCommand(workingDir: File = rootProject.projectDir): String { + val parts = this.split("\\s".toRegex()) + val proc = ProcessBuilder(*parts.toTypedArray()) + .directory(workingDir) + .redirectOutput(ProcessBuilder.Redirect.PIPE) + .redirectError(ProcessBuilder.Redirect.PIPE) + .start() + proc.waitFor(30, TimeUnit.SECONDS) + return proc.inputStream.bufferedReader().readText() +} + +spotless { + ratchetFrom("$sourceRepoRemoteName/master") + + val shortFormLicenseHeader = """ + // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + // SPDX-License-Identifier: Apache-2.0 + """.trimIndent() + + java { + licenseHeader(shortFormLicenseHeader) + removeUnusedImports() + } + kotlin { + licenseHeader(shortFormLicenseHeader) + } +} + tasks { withType { options.encoding = "UTF-8" diff --git a/src/main/java/com/amazon/ion/_Private_Trampoline.kt b/src/main/java/com/amazon/ion/_Private_Trampoline.kt index c4b9680c24..a44b34cc3a 100644 --- a/src/main/java/com/amazon/ion/_Private_Trampoline.kt +++ b/src/main/java/com/amazon/ion/_Private_Trampoline.kt @@ -1,3 +1,5 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion /**