Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds check for license headers and unused imports #789

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 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 @@ -69,6 +69,7 @@ group = "com.amazon.ion"
version = File(project.rootDir.path + "/project.version").readLines().single()
description = "A Java implementation of the Amazon Ion data notation."

val githubRepositoryUrl = "https://github.com/amazon-ion/ion-java/"
val isReleaseVersion: Boolean = !version.toString().endsWith("SNAPSHOT")
val generatedResourcesDir = "$buildDir/generated/main/resources"
lateinit var sourcesJar: AbstractArchiveTask
Expand Down Expand Up @@ -99,6 +100,68 @@ 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
}

var name = "$git remote".runCommand().lines().firstOrNull { it.isSourceRepo() }

if (System.getenv("CI") == "true") {
// When running on a CI environment e.g. GitHub Actions, we might need to automatically add the remote
if (name == null) {
name = "ci_source_repository"
"$git remote add $name $githubRepositoryUrl".runCommand()
}
// ...and make sure that we have indeed fetched that remote
"$git fetch $name".runCommand()
}

name ?: throw Exception(
"""
|No git remote found for amazon-ion/ion-java. Try again after running:
|
| git remote add -f <name> $githubRepositoryUrl
""".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 Expand Up @@ -418,7 +481,7 @@ publishing {
pom {
name.set("Ion Java")
description.set(project.description)
url.set("https://github.com/amazon-ion/ion-java/")
url.set(githubRepositoryUrl)

licenses {
license {
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
Loading