Skip to content

Commit

Permalink
Allow errorprone failures to be automatically suppressed (#6)
Browse files Browse the repository at this point in the history
Allow errorprone failures to be automatically suppressed
  • Loading branch information
CRogers authored Nov 4, 2024
1 parent 160a6f1 commit cbd1152
Show file tree
Hide file tree
Showing 28 changed files with 2,374 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ allprojects {

apply plugin: 'com.palantir.java-format'

group = 'com.palantir.baseline'
group = 'com.palantir.suppressible-error-prone'
version System.env.CIRCLE_TAG ?: gitVersion()
}

Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-6.v2.yml
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
3 changes: 3 additions & 0 deletions gradle-suppressible-error-prone/.gitignore
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/
41 changes: 41 additions & 0 deletions gradle-suppressible-error-prone/build.gradle
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')
}
}
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));
}
}
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();
}
}
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));
}
}
Loading

0 comments on commit cbd1152

Please sign in to comment.