Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Docs/Update custom evaluator sample #194

Merged
merged 5 commits into from
May 30, 2019
Merged
Changes from 3 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
23 changes: 21 additions & 2 deletions docs/incubating/custom-evaluator.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ can be provided as a closure as well:

```gradle
staticAnalysis {
evaluator { Set<Violations> allViolations ->
evaluator { Set allViolations ->
// add your evaluation logic here
}
//...
Expand All @@ -61,10 +61,29 @@ Anything that respect such contract is valid. For example, a custom evaluator mi
* Only break the build if there are errors or warnings in one specific report
* Or anything else that you can think of

For example, this custom evaluator fails the build if PMD errors are greater than five:

```gradle
evaluator { Set allViolations ->
allViolations.each { violation ->
if (violation.name == "PMD" && (violation.errors > 5)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below it says toolName but here it is name. Which one it is? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The backing property is called toolName but the method to access it is getName (and I'm guessing that's why the compiler allows .name as accessor?)

Do you think we shall stick to just name in the sample to create less confusion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like toolName is private. We shouldn't really mention that since it is not usable. Sticking to name makes sense 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amended here d3da57b

throw new GradleException("PMD Violations exceeded \n")
}
}
}
```
The properties you can read from a [`Violation`][violationscode] result are:

* `toolName`: Possible values are: `"PMD"`, `"Checkstyle"`, `"Findbugs"`, `"KTlint"`, `"Detekt"` and `"Lint"`.
* `errors`: Represents the number of errors found during the analysis.
* `warnings`: Represents the number of warnings found during the analysis.
* `reports`: Contains a list of the generated report files.

---
Please note that the presence of an `evaluator` property will make the plugin ignore the `penalty` closure and its thresholds. If you
want to provide behaviour on top of the default [`DefaultViolationsEvaluator`][defaultviolationsevaluatorcode], you can have your own
evaluator run its logic and then delegate the thresholds counting to an instance of `DefaultViolationsEvaluator` you create.

[violationsevaluatorcode]: https://github.com/novoda/gradle-static-analysis-plugin/blob/master/plugin/src/main/groovy/com/novoda/staticanalysis/ViolationsEvaluator.groovy
[defaultviolationsevaluatorcode]: https://github.com/novoda/gradle-static-analysis-plugin/blob/master/plugin/src/main/groovy/com/novoda/staticanalysis/DefaultViolationsEvaluator.groovy
[violationscode]: https://github.com/novoda/gradle-static-analysis-plugin/blob/master/plugin/src/main/groovy/com/novoda/staticanalysis/internal/Violations.groovy
[violationscode]: https://github.com/novoda/gradle-static-analysis-plugin/blob/master/plugin/src/main/groovy/com/novoda/staticanalysis/Violations.groovy