Folder.getBuildHealthReports looks like it is double-counting each child Folder. It does a recursive traversal of the folder, applying FolderHealthMetric.Reporter to each node (leaf or not). But a typical FolderHealthMetric like WorstChildHealthMetric also calls getHealthReport on the child Folder, which winds up calling Folder.getBuildHealthReports recursively! This seems like a mistake.
Possible fixes:
- Make WorstChildHealthMetric.ReporterImpl.observe (and any other similar impls) ignore a Folder it is given.
- Make Folder.getBuildHealthReports not ask a reporter to observe a Folder.
- Make Folder.getBuildHealthReports not do a recursive traversal, only asking about direct children.
My preference is for the third fix, as it leaves the reporter in control of the logic; if it needs a recursive call it can do one.
By the way, the awkward reflection code in getHealthReport suggests that we need an interface in Jenkins core, such as
interface ItemWithHealthReport extends Item {
HealthReport getBuildHealth();
List<HealthReport> getBuildHealthReports();
}
AverageChildHealthMetric in cloudbees-folders-plus already implements the first fix. (Oddly WorstChildHealthMetric did not get the analogous change.) This complicates doing the third fix a bit as old versions of cloudbees-folders-plus with new versions of cloudbees-folder would behave incorrectly (at least, not recursively) in AverageChildHealthMetric; however the regression would be pretty minor, and corrected simply by upgrading both.