Skip to content

Commit

Permalink
Move and polish ProblemReporter interface (gradle#31844)
Browse files Browse the repository at this point in the history
  • Loading branch information
bot-gradle authored Dec 24, 2024
2 parents 12f2436 + 11eac63 commit 9384425
Show file tree
Hide file tree
Showing 38 changed files with 212 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class ConfigurationCacheProblems(
private
fun InternalProblems.onProblem(problem: PropertyProblem, severity: ProblemSeverity) {
val message = problem.message.render()
internalReporter.create {
internalReporter.internalCreate {
id(
DeprecationMessageBuilder.createDefaultDeprecationId(message),
message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class FlowParametersInstantiator(
object : AbstractTaskDependencyResolveContext() {
override fun add(dependency: Any) {
problems.add(
internalProblemReporter.create {
internalProblemReporter.internalCreate {
id("invalid-dependency", "Property cannot carry dependency", GradleCoreProblemGroup.validation().property())
contextualLabel("Property '$propertyName' cannot carry a dependency on $dependency as these are not yet supported.")
severity(Severity.ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class WorkerExecutorProblemsApiIntegrationTest extends AbstractIntegrationSpec {
Exception wrappedException = new Exception("Wrapped cause");
// Create and report a problem
// This needs to be Java 6 compatible, as we are in a worker
getProblems().getReporter().reporting(problem -> problem
getProblems().getReporter().report(problem -> problem
.id(org.gradle.api.problems.ProblemId.create("type", "label", org.gradle.api.problems.ProblemGroup.create("generic", "Generic")))
.stackLocation()
.withException(new RuntimeException("Exception message", wrappedException))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void featureUsed(final DeprecatedFeatureUsage usage) {

private void reportDeprecation(final DeprecatedFeatureUsage usage, final ProblemDiagnostics diagnostics) {
InternalProblemReporter reporter = ((InternalProblems) problemsService).getInternalReporter();
Problem problem = reporter.create(new Action<InternalProblemSpec>() {
Problem problem = reporter.internalCreate(new Action<InternalProblemSpec>() {
@Override
public void execute(InternalProblemSpec builder) {
InternalProblemSpec problemSpec = builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Injected {
def problems = project.objects.newInstance(Injected).getProblems()
def problemGroup = ProblemGroup.create("root", "Root Group")

problems.getReporter().reporting {
problems.getReporter().report {
it.id(ProblemId.create('adhoc-script-deprecation', 'Deprecated script plugin', problemGroup))
.contextualLabel("Deprecated script plugin 'demo-script-plugin'")
.severity(Severity.WARNING)
Expand All @@ -19,7 +19,7 @@ problems.getReporter().reporting {

tasks.register('warningTask') {
doLast {
problems.getReporter().reporting {
problems.getReporter().report {
it.id(ProblemId.create('adhoc-task-deprecation', 'Deprecated task', problemGroup))
.contextualLabel("Task 'warningTask' is deprecated")
.severity(Severity.WARNING)
Expand All @@ -30,11 +30,10 @@ tasks.register('warningTask') {

tasks.register('failingTask') {
doLast {
problems.getReporter().throwing {
problems.getReporter().throwing(new RuntimeException("The 'failingTask' should not be called")) {
it.id(ProblemId.create('broken-task', 'Task should not be called', problemGroup))
.contextualLabel("Task 'failingTask' should not be called")
.severity(Severity.ERROR)
.withException(new RuntimeException("The 'failingTask' should not be called"))
.solution("Please use 'successfulTask' instead of this task")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Injected {
val problems = project.objects.newInstance<Injected>().problems
val problemGroup = ProblemGroup.create("root", "Root Group")

problems.getReporter().reporting {
problems.getReporter().report {
id(ProblemId.create("adhoc-script-deprecation", "Deprecated script plugin", problemGroup))
.contextualLabel("Deprecated script plugin 'demo-script-plugin'")
.severity(Severity.WARNING)
Expand All @@ -20,7 +20,7 @@ problems.getReporter().reporting {
tasks {
val warningTask by registering {
doLast {
problems.getReporter().reporting {
problems.getReporter().report {
id(ProblemId.create("adhoc-task-deprecation", "Deprecated task", problemGroup))
.contextualLabel("Task 'warningTask' is deprecated")
.severity(Severity.WARNING)
Expand All @@ -31,11 +31,10 @@ tasks {

val failingTask by registering {
doLast {
problems.getReporter().throwing {
problems.getReporter().throwing(RuntimeException("The 'failingTask' should not be called")) {
id(ProblemId.create("broken-task", "Task should not be called", problemGroup))
.contextualLabel("Task 'failingTask' should not be called")
.severity(Severity.ERROR)
.withException(RuntimeException("The 'failingTask' should not be called"))
.solution("Please use 'successfulTask' instead of this task")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public boolean canBuild(String modelName) {

@Override
public Object buildAll(String modelName, Project project) {
problems.getReporter().reporting(problem -> problem
problems.getReporter().report(problem -> problem
.id(ProblemId.create("unused", "Demo model", ModelBuilderPlugin.PROBLEM_GROUP))
.severity(Severity.WARNING)
.details("This is a demo model and doesn't do anything useful")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ public abstract class FailingTask extends DefaultTask {
@TaskAction
public void run() {
// tag::problems-api-fail-the-build[]
throw getProblems().getReporter().throwing(problemSpec -> {
throw getProblems().getReporter().throwing((new RuntimeException("Message from runtime exception")), problemSpec -> {
problemSpec.id(ProblemId.create("sample-error", "Sample Error", StandardPlugin.PROBLEM_GROUP));
problemSpec.contextualLabel("This happened because ProblemReporter.throwing() was called");
problemSpec.details("This is a demonstration of how to add\ndetailed information to a build failure");
problemSpec.documentedAt("https://example.com/docs");
problemSpec.withException(new RuntimeException("Message from runtime exception"));
problemSpec.solution("Remove the Problems.throwing() method call from the task action");
});
// end::problems-api-fail-the-build[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public StandardPlugin(Problems problems) {
public void apply(Project project) {
project.getTasks().register("myFailingTask", FailingTask.class);
// tag::problems-api-report[]
problems.getReporter().reporting(problem -> problem
problems.getReporter().report(problem -> problem
.id(ProblemId.create("adhoc-plugin-deprecation", "Plugin is deprecated", PROBLEM_GROUP))
.contextualLabel("The 'standard-plugin' is deprecated")
.documentedAt("https://github.com/gradle/gradle/README.md")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ProblemReportingPlugin(Problems problems) { // <1>
}

public void apply(Project project) {
this.problemReporter.reporting(builder -> builder // <3>
this.problemReporter.report(builder -> builder // <3>
.id(ProblemId.create("adhoc-deprecation", "Plugin 'x' is deprecated", PROBLEM_GROUP))
.details("The plugin 'x' is deprecated since version 2.5")
.solution("Please use plugin 'y'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class DevelocityPluginEndOfBuildCallbackIntegrationTest extends AbstractIntegrat
${ProblemGroup.name} problemGroup = ${ProblemGroup.name}.create("generic", "group label");
${ProblemId.name} problemId = ${ProblemId.name}.create("type", "label", problemGroup)
${getProblemReportingScript """
problems.getReporter().throwing {
problems.getReporter().throwing(new RuntimeException('failed')) {
it.id(problemId)
.withException(new RuntimeException('failed'))
}"""}
task $succeedingTaskName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
def "problem replaced with a validation warning if mandatory id is missing"() {
given:
withReportProblemTask """
problems.getReporter().reporting {
problems.getReporter().report {
it.details('Wrong API usage, will not show up anywhere')
}
"""
Expand Down Expand Up @@ -65,7 +65,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
}
"""
Expand Down Expand Up @@ -93,7 +93,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.stackLocation()
}
Expand Down Expand Up @@ -121,7 +121,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.documentedAt("https://example.org/doc")
}
Expand All @@ -138,7 +138,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.offsetInFileLocation("test-location", 1, 2)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.lineInFileLocation("test-location", 1, 2)
}
Expand Down Expand Up @@ -199,7 +199,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.severity(Severity.${severity.name()})
}
Expand All @@ -219,7 +219,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.solution("solution")
}
Expand All @@ -236,7 +236,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.withException(new RuntimeException("test"))
}
Expand All @@ -256,7 +256,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.additionalData(org.gradle.api.problems.internal.GeneralDataSpec) {
it.put('key','value')
Expand All @@ -271,11 +271,11 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
receivedProblem.additionalData.asMap == ['key': 'value']
}

def "cannot set addtional data with different type"() {
def "cannot set additional data with different type"() {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.additionalData(org.gradle.api.problems.internal.GeneralDataSpec) {
it.put('key','value')
Expand All @@ -298,7 +298,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
buildFile 'class InvalidData implements AdditionalData {}'
withReportProblemTask """
${problemIdScript()}
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.additionalData(InvalidData) {}
}
Expand All @@ -324,9 +324,8 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
given:
withReportProblemTask """
${problemIdScript()}
problems.getReporter().throwing {
problems.getReporter().throwing(new RuntimeException('test')) {
it.id(problemId)
.withException(new RuntimeException('test'))
}
"""

Expand All @@ -342,13 +341,12 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
withReportProblemTask """
${problemIdScript()}
try {
problems.getReporter().throwing {
problems.getReporter().throwing(new RuntimeException("test")) {
it.id(${ProblemId.name}.create("type11", "inner", problemGroup))
.withException(new RuntimeException("test"))
}
} catch (RuntimeException ex) {
problems.getReporter().throwing {
it.id(${ProblemId.name}.create("type12", "outer", problemGroup)).withException(ex)
problems.getReporter().throwing(ex) {
it.id(${ProblemId.name}.create("type12", "outer", problemGroup))
}
}
"""
Expand All @@ -366,7 +364,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
withReportProblemTask """
${problemIdScript()}
for (int i = 0; i < 10; i++) {
problems.getReporter().reporting {
problems.getReporter().report {
it.id(problemId)
.severity(Severity.WARNING)
.solution("solution")
Expand Down Expand Up @@ -397,7 +395,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
withReportProblemTask """
${problemIdScript()}
for (int i = 0; i < 10; i++) {
problems.getReporter().reporting {
problems.getReporter().report {
it.id(${ProblemId.name}.create("type\$i", "This is the heading problem text\$i", problemGroup))
.severity(Severity.WARNING)
.details("This is a huge amount of extremely and very relevant details for this problem\$i")
Expand Down Expand Up @@ -432,7 +430,7 @@ class ProblemsServiceIntegrationTest extends AbstractIntegrationSpec {
withReportProblemTask """
${problemIdScript()}
for (int i = 0; i < 10; i++) {
problems.getReporter().reporting {
problems.getReporter().report {
it.id(${ProblemId.name}.create("type\$i", "This is the heading problem text\$i", problemGroup))
.severity(Severity.WARNING)
.details("This is a huge amount of extremely and very relevant details for this problem\$i")
Expand Down
Loading

0 comments on commit 9384425

Please sign in to comment.