|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Jobs; |
| 4 | + |
| 5 | +use App\Models\Build; |
| 6 | +use App\Models\CoverageDiff; |
| 7 | +use App\Models\CoverageLine; |
| 8 | +use App\Models\CoverageView; |
| 9 | +use Exception; |
| 10 | +use Illuminate\Bus\Queueable; |
| 11 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 12 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 13 | +use Illuminate\Queue\InteractsWithQueue; |
| 14 | +use Illuminate\Support\Collection; |
| 15 | +use InvalidArgumentException; |
| 16 | +use SebastianBergmann\Diff\Differ; |
| 17 | +use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; |
| 18 | + |
| 19 | +class ComputeCoverageDifference implements ShouldQueue |
| 20 | +{ |
| 21 | + use Dispatchable; |
| 22 | + use InteractsWithQueue; |
| 23 | + use Queueable; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + public Build $baseBuild, |
| 27 | + public Build $compareBuild, |
| 28 | + ) { |
| 29 | + if ($this->baseBuild->projectid !== $this->compareBuild->projectid) { |
| 30 | + throw new InvalidArgumentException('Builds must belong to the same project.'); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Execute the job. |
| 36 | + */ |
| 37 | + public function handle(): void |
| 38 | + { |
| 39 | + $basePaths = $this->baseBuild->coverage()->pluck('fullpath'); |
| 40 | + $comparePaths = $this->compareBuild->coverage()->pluck('fullpath'); |
| 41 | + /** @var Collection<int, string> $allPaths */ |
| 42 | + $allPaths = $basePaths->merge($comparePaths)->unique()->sort()->values(); |
| 43 | + |
| 44 | + $coverageDiff = CoverageDiff::updateOrCreate( |
| 45 | + [ |
| 46 | + 'basebuildid' => $this->baseBuild->id, |
| 47 | + 'comparebuildid' => $this->compareBuild->id, |
| 48 | + ], |
| 49 | + [ |
| 50 | + 'coveredlinesadded' => 0, |
| 51 | + 'coveredlinesremoved' => 0, |
| 52 | + 'coveredlinesuncovered' => 0, |
| 53 | + 'uncoveredlinesadded' => 0, |
| 54 | + 'uncoveredlinesremoved' => 0, |
| 55 | + 'uncoveredlinescovered' => 0, |
| 56 | + ] |
| 57 | + ); |
| 58 | + |
| 59 | + foreach ($allPaths->chunk(100) as $batch) { |
| 60 | + $baseViews = $this->baseBuild->coverage() |
| 61 | + ->whereIn('fullpath', $batch) |
| 62 | + ->get() |
| 63 | + ->keyBy('fullpath'); |
| 64 | + $compareViews = $this->compareBuild->coverage() |
| 65 | + ->whereIn('fullpath', $batch) |
| 66 | + ->get() |
| 67 | + ->keyBy('fullpath'); |
| 68 | + |
| 69 | + foreach ($batch as $path) { |
| 70 | + /** @var CoverageView|null $baseView */ |
| 71 | + $baseView = $baseViews->get((string) $path); |
| 72 | + /** @var CoverageView|null $compareView */ |
| 73 | + $compareView = $compareViews->get((string) $path); |
| 74 | + $this->computeFileDiff($baseView, $compareView, $coverageDiff); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + $coverageDiff->save(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Compute the coverage diff between two coverage files. |
| 83 | + */ |
| 84 | + private function computeFileDiff(?CoverageView $cv1, ?CoverageView $cv2, CoverageDiff $coverageDiff): void |
| 85 | + { |
| 86 | + if ($cv1 === null && $cv2 === null) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + if ($cv1 === null) { |
| 91 | + // File added in compare build |
| 92 | + if ($cv2 !== null) { |
| 93 | + foreach ($cv2->coveredLines as $line) { |
| 94 | + if ($line->isCovered) { |
| 95 | + $coverageDiff->coveredlinesadded++; |
| 96 | + } else { |
| 97 | + $coverageDiff->uncoveredlinesadded++; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if ($cv2 === null) { |
| 105 | + // File removed in compare build |
| 106 | + foreach ($cv1->coveredLines as $line) { |
| 107 | + if ($line->isCovered) { |
| 108 | + $coverageDiff->coveredlinesremoved++; |
| 109 | + } else { |
| 110 | + $coverageDiff->uncoveredlinesremoved++; |
| 111 | + } |
| 112 | + } |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + /** @var Collection<int, CoverageLine> $cl1 */ |
| 117 | + $cl1 = collect($cv1->coveredLines)->keyBy('lineNumber'); |
| 118 | + /** @var Collection<int, CoverageLine> $cl2 */ |
| 119 | + $cl2 = collect($cv2->coveredLines)->keyBy('lineNumber'); |
| 120 | + |
| 121 | + $differ = new Differ(new UnifiedDiffOutputBuilder()); |
| 122 | + |
| 123 | + // Files are 1-indexed, with the first line being line 1. |
| 124 | + $file1Line = 1; |
| 125 | + $file2Line = 1; |
| 126 | + foreach ($differ->diffToArray($cv1->file ?? '', $cv2->file ?? '') as [$lineText, $status]) { |
| 127 | + $line1 = $cl1->get($file1Line); |
| 128 | + $line2 = $cl2->get($file2Line); |
| 129 | + |
| 130 | + switch ($status) { |
| 131 | + case Differ::OLD: // Line is shared between both files |
| 132 | + if ($line1 !== null && $line1->isCovered && $line2 !== null && !$line2->isCovered) { |
| 133 | + $coverageDiff->coveredlinesuncovered++; |
| 134 | + } elseif ($line1 !== null && !$line1->isCovered && $line2 !== null && $line2->isCovered) { |
| 135 | + $coverageDiff->uncoveredlinescovered++; |
| 136 | + } elseif ($line1 !== null && $line2 === null) { |
| 137 | + // We consider a line to be added if it was previously not executable, and now is executable. |
| 138 | + // The reverse is also true: a line is removed if it was previously executable, and now is not executable. |
| 139 | + if ($line1->isCovered) { |
| 140 | + $coverageDiff->coveredlinesremoved++; |
| 141 | + } else { |
| 142 | + $coverageDiff->uncoveredlinesremoved++; |
| 143 | + } |
| 144 | + } elseif ($line1 === null && $line2 !== null) { |
| 145 | + if ($line2->isCovered) { |
| 146 | + $coverageDiff->coveredlinesadded++; |
| 147 | + } else { |
| 148 | + $coverageDiff->uncoveredlinesadded++; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + $file1Line++; |
| 153 | + $file2Line++; |
| 154 | + break; |
| 155 | + case Differ::ADDED: // Line exists in file 2 but not file 1 |
| 156 | + if ($line2 !== null) { |
| 157 | + if ($line2->isCovered) { |
| 158 | + $coverageDiff->coveredlinesadded++; |
| 159 | + } else { |
| 160 | + $coverageDiff->uncoveredlinesadded++; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + $file2Line++; |
| 165 | + break; |
| 166 | + case Differ::REMOVED: // Line exists in file 1 but not file 2 |
| 167 | + if ($line1 !== null) { |
| 168 | + if ($line1->isCovered) { |
| 169 | + $coverageDiff->coveredlinesremoved++; |
| 170 | + } else { |
| 171 | + $coverageDiff->uncoveredlinesremoved++; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + $file1Line++; |
| 176 | + break; |
| 177 | + default: |
| 178 | + throw new Exception('Invalid Differ status: ' . $status); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments