-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow errorprone failures to be automatically suppressed (#6)
Allow errorprone failures to be automatically suppressed
- Loading branch information
Showing
28 changed files
with
2,374 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: feature | ||
feature: | ||
description: Allow errorprone failures to be automatically suppressed | ||
links: | ||
- https://github.com/palantir/suppressible-error-prone/pull/6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Avoids issues with nebulatests hitting the excludedPathRegex as they have build in the name | ||
# see SuppressibleErrorPronePluginIntegrationTest for more info | ||
nebulatestSourceSets/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
apply plugin: 'groovy' | ||
apply plugin: 'java-gradle-plugin' | ||
apply plugin: 'com.palantir.external-publish-jar' | ||
apply plugin: 'com.palantir.external-publish-gradle-plugin' | ||
|
||
dependencies { | ||
implementation 'net.ltgt.gradle:gradle-errorprone-plugin' | ||
implementation 'org.ow2.asm:asm' | ||
implementation 'com.palantir.gradle.utils:environment-variables' | ||
|
||
testImplementation 'com.netflix.nebula:nebula-test' | ||
testImplementation 'org.junit.jupiter:junit-jupiter' | ||
testImplementation 'org.assertj:assertj-core' | ||
testImplementation 'commons-io:commons-io' | ||
} | ||
|
||
gradlePlugin { | ||
plugins { | ||
suppressibleErrorProne { | ||
id = 'com.palantir.suppressible-error-prone' | ||
displayName = 'Suppressible Error Prone' | ||
implementationClass = 'com.palantir.gradle.suppressibleerrorprone.SuppressibleErrorPronePlugin' | ||
} | ||
} | ||
} | ||
|
||
test { | ||
systemProperty 'ignoreDeprecations', 'true' | ||
systemProperty 'projectVersion', project.version | ||
} | ||
|
||
// Added as a jar the errorprone configuration inside the plugin, so in nebula-tests needs to exist in maven local | ||
tasks.test.dependsOn tasks.findByPath(':suppressible-error-prone:publishToMavenLocal') | ||
// Similarly, added as a jar for compileOnly inside the plugin, so in nebula-tests needs to exist in maven local | ||
tasks.test.dependsOn tasks.findByPath(':suppressible-error-prone-annotations:publishToMavenLocal') | ||
|
||
tasks.withType(JavaCompile) { | ||
options.errorprone { | ||
disable('StrictUnusedVariable') | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...prone/src/main/java/com/palantir/gradle/suppressibleerrorprone/ConditionalPatchCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.gradle.suppressibleerrorprone; | ||
|
||
import java.util.Set; | ||
import org.gradle.api.specs.Spec; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
|
||
public record ConditionalPatchCheck(Spec<JavaCompile> when, Set<String> checks) { | ||
public ConditionalPatchCheck(Spec<JavaCompile> when, String... checks) { | ||
this(when, Set.of(checks)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...-error-prone/src/main/java/com/palantir/gradle/suppressibleerrorprone/IfModuleIsUsed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.gradle.suppressibleerrorprone; | ||
|
||
import java.util.Objects; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.artifacts.component.ModuleComponentIdentifier; | ||
import org.gradle.api.specs.Spec; | ||
import org.gradle.api.tasks.SourceSetContainer; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
|
||
public record IfModuleIsUsed(String group, String module) implements Spec<JavaCompile> { | ||
@Override | ||
public boolean isSatisfiedBy(JavaCompile javaCompile) { | ||
Project project = javaCompile.getProject(); | ||
return project | ||
.getExtensions() | ||
.getByType(SourceSetContainer.class) | ||
.matching(sourceSet -> sourceSet.getCompileJavaTaskName().equals(javaCompile.getName())) | ||
.stream() | ||
.findFirst() | ||
.filter(sourceSet -> { | ||
Configuration compileClasspath = | ||
project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()); | ||
return hasDependenciesMatching( | ||
compileClasspath, | ||
mci -> Objects.equals(mci.getGroup(), group) && Objects.equals(mci.getModule(), module)); | ||
}) | ||
.isPresent(); | ||
} | ||
|
||
private static boolean hasDependenciesMatching(Configuration configuration, Spec<ModuleComponentIdentifier> spec) { | ||
return configuration | ||
.getIncoming() | ||
.artifactView(viewConfiguration -> viewConfiguration.componentFilter(ci -> | ||
ci instanceof ModuleComponentIdentifier && spec.isSatisfiedBy((ModuleComponentIdentifier) ci))) | ||
.getArtifacts() | ||
.iterator() | ||
.hasNext(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...main/java/com/palantir/gradle/suppressibleerrorprone/SuppressibleErrorProneExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.gradle.suppressibleerrorprone; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import net.ltgt.gradle.errorprone.ErrorProneOptions; | ||
import org.gradle.api.Action; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.plugins.ExtensionAware; | ||
import org.gradle.api.provider.ListProperty; | ||
import org.gradle.api.provider.SetProperty; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
|
||
public abstract class SuppressibleErrorProneExtension { | ||
private final Project project; | ||
|
||
public SuppressibleErrorProneExtension(Project project) { | ||
this.project = project; | ||
} | ||
|
||
public abstract SetProperty<String> getPatchChecks(); | ||
|
||
public abstract ListProperty<ConditionalPatchCheck> getConditionalPatchChecks(); | ||
|
||
public final Set<String> patchChecksForCompilation(JavaCompile javaCompile) { | ||
return Stream.concat( | ||
getPatchChecks().get().stream(), | ||
getConditionalPatchChecks().get().stream() | ||
.filter(conditionalPatchCheck -> | ||
conditionalPatchCheck.when().isSatisfiedBy(javaCompile)) | ||
.flatMap(conditionalPatchCheck -> conditionalPatchCheck.checks().stream())) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public final void configureEachErrorProneOptions(Action<ErrorProneOptions> action) { | ||
project.getTasks().withType(JavaCompile.class).configureEach(javaCompile -> ((ExtensionAware) | ||
javaCompile.getOptions()) | ||
.getExtensions() | ||
.configure(ErrorProneOptions.class, action)); | ||
} | ||
} |
Oops, something went wrong.