1
|
|
|
<?php |
2
|
|
|
namespace TheCodingMachine\WashingMachine\Gitlab; |
3
|
|
|
|
4
|
|
|
use Gitlab\Client; |
5
|
|
|
use TheCodingMachine\WashingMachine\Clover\Analysis\Difference; |
6
|
|
|
use TheCodingMachine\WashingMachine\Clover\CloverFile; |
7
|
|
|
use TheCodingMachine\WashingMachine\Clover\DiffService; |
8
|
|
|
|
9
|
|
|
class SendCommentService |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Client |
13
|
|
|
*/ |
14
|
|
|
private $client; |
15
|
|
|
/** |
16
|
|
|
* @var DiffService |
17
|
|
|
*/ |
18
|
|
|
private $diffService; |
19
|
|
|
|
20
|
|
|
public function __construct(Client $client, DiffService $diffService) |
21
|
|
|
{ |
22
|
|
|
$this->client = $client; |
23
|
|
|
$this->diffService = $diffService; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function sendCodeCoverageCommentToMergeRequest(CloverFile $cloverFile, CloverFile $previousCloverFile, string $projectName, int $mergeRequestId, string $commitId, string $gitlabUrl) |
27
|
|
|
{ |
28
|
|
|
$coverage = $cloverFile->getCoveragePercentage(); |
29
|
|
|
$previousCoverage = $previousCloverFile->getCoveragePercentage(); |
30
|
|
|
|
31
|
|
|
$additionalText = ''; |
32
|
|
|
$style = ''; |
33
|
|
|
if ($coverage > $previousCoverage + 0.0001) { |
34
|
|
|
$additionalText = sprintf('(<em>+%.2f%%</em>)', ($coverage - $previousCoverage)*100); |
35
|
|
|
$style .= 'background-color: #00994c; color: white'; |
36
|
|
|
} elseif ($coverage < $previousCoverage - 0.0001) { |
37
|
|
|
$additionalText = sprintf('(<em>-%.2f%%</em>)', ($previousCoverage - $coverage)*100); |
38
|
|
|
$style .= 'background-color: #ff6666; color: white'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$differences = $this->diffService->getMeaningfulDifferences($cloverFile, $previousCloverFile); |
42
|
|
|
$differencesHtml = $this->getDifferencesHtml($differences, $commitId, $gitlabUrl); |
|
|
|
|
43
|
|
|
|
44
|
|
|
// Note: there is a failure in the way Gitlab escapes HTML for the tables. Let's use this!. |
45
|
|
|
$message = sprintf('<table> |
46
|
|
|
<tr> |
47
|
|
|
<td>PHP code coverage:</td> |
48
|
|
|
<td style="font-weight: bold">%.2f%%</td> |
49
|
|
|
<td style="%s">%s</td> |
50
|
|
|
<td width="99%%"></td> |
51
|
|
|
</tr> |
52
|
|
|
</table><br/>%s', $cloverFile->getCoveragePercentage()*100, $style, $additionalText, $differencesHtml); |
53
|
|
|
|
54
|
|
|
$this->client->merge_requests->addComment($projectName, $mergeRequestId, $message); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function sendDifferencesCommentsInCommit(CloverFile $cloverFile, CloverFile $previousCloverFile, string $projectName, string $commitId) |
58
|
|
|
{ |
59
|
|
|
$differences = $this->diffService->getMeaningfulDifferences($cloverFile, $previousCloverFile); |
60
|
|
|
|
61
|
|
|
foreach ($differences as $difference) { |
62
|
|
|
$note = $this->getDifferencesHtml([ $difference ], $commitId, $projectName); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->client->repositories->createCommitComment($projectName, $commitId, $note, [ |
65
|
|
|
'path' => $difference->getFile(), |
66
|
|
|
'line' => $difference->getLine(), |
67
|
|
|
'line_type' => 'new' |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Difference[] $differences |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function getDifferencesHtml(array $differences, string $commitId, string $gitlabUrl, string $projectName) : string |
78
|
|
|
{ |
79
|
|
|
$tableTemplate = '<table> |
80
|
|
|
<tr> |
81
|
|
|
<th></th> |
82
|
|
|
<th>C.R.A.P.</th> |
83
|
|
|
<th>Variation</th> |
84
|
|
|
<th width="99%%"></th> |
85
|
|
|
</tr> |
86
|
|
|
%s |
87
|
|
|
</table>'; |
88
|
|
|
$tableRows = ''; |
89
|
|
|
foreach ($differences as $difference) { |
90
|
|
|
$style = ''; |
91
|
|
|
if (!$difference->isNew()) { |
92
|
|
|
|
93
|
|
|
if ($difference->getCrapDifference() < 0) { |
94
|
|
|
$style = 'background-color: #00994c; color: white'; |
95
|
|
|
} else { |
96
|
|
|
$style = 'background-color: #ff6666; color: white'; |
97
|
|
|
} |
98
|
|
|
$differenceCol = sprintf('%+d', $difference->getCrapDifference()); |
99
|
|
|
} else { |
100
|
|
|
$differenceCol = '<em>New</em>'; |
101
|
|
|
// TODO: for new rows, it would be really cool to display a color code for the global CRAP score. |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$link = $this->getLinkToMethodInCommit($gitlabUrl, $projectName, $commitId, $difference->getFile(), $difference->getLine()); |
105
|
|
|
|
106
|
|
|
$tableRows .= sprintf('<tr> |
107
|
|
|
<td><code><a href="%s">%s</a></code></td> |
108
|
|
|
<td style="text-align:center">%d</td> |
109
|
|
|
<td style="text-align:center;%s">%s</td> |
110
|
|
|
</tr>', $link, $difference->getMethodShortName(), $difference->getCrapScore(), $style, $differenceCol); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return sprintf($tableTemplate, $tableRows); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function getLinkToMethodInCommit(string $gitlabUrl, string $projectName, string $commit, string $filePath, int $line) |
117
|
|
|
{ |
118
|
|
|
return rtrim($gitlabUrl, '/').'/'.$projectName.'/blob/'.$commit.'/'.ltrim('/', $filePath).'#L'.$line; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for function calls that miss required arguments.