Skip to content

Commit

Permalink
Adds check for license headers and unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Apr 12, 2024
1 parent 409987b commit d5a90fc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 53 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <name> 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<JavaCompile> {
options.encoding = "UTF-8"
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/amazon/ion/_Private_Trampoline.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion

/**
Expand Down

0 comments on commit d5a90fc

Please sign in to comment.