-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathSensiolabsInsight.php
More file actions
177 lines (147 loc) · 4.25 KB
/
Copy pathSensiolabsInsight.php
File metadata and controls
177 lines (147 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace PHPCensor\Plugin;
use Exception;
use PHPCensor;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
use PHPCensor\Common\Exception\RuntimeException;
/**
* Sensiolabs Insight Plugin - Allows Sensiolabs Insight testing.
*
* @package PHP Censor
* @subpackage Application
*
* @author Eugen Ganshorn <eugen@ganshorn.eu>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class SensiolabsInsight extends Plugin
{
/**
* @var string
*/
protected $userUuid;
/**
* @var string
*/
protected $authToken;
/**
* @var string
*/
protected $projectUuid;
/**
* @var int
*/
protected $allowedWarnings = 0;
/**
* @return string
*/
public static function pluginName()
{
return 'sensiolabs_insight';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (\array_key_exists('allowed_warnings', $options)) {
$this->allowedWarnings = (int)$options['allowed_warnings'];
}
if (\array_key_exists('user_uuid', $options)) {
$this->userUuid = $options['user_uuid'];
}
if (\array_key_exists('auth_token', $options)) {
$this->authToken = $this->builder->interpolate($options['auth_token'], true);
}
if (\array_key_exists('project_uuid', $options)) {
$this->projectUuid = $options['project_uuid'];
}
$this->executable = $this->findBinary('insight');
}
/**
* Runs Sensiolabs Insights in a specified directory.
*
* @throws Exception
*/
public function execute()
{
$insightBinaryPath = $this->executable;
$this->executeSensiolabsInsight($insightBinaryPath);
$errorCount = $this->processReport(\trim($this->builder->getLastOutput()));
$this->build->storeMeta((self::pluginName() . '-warnings'), $errorCount);
return $this->wasLastExecSuccessful($errorCount);
}
/**
* Process PHPMD's XML output report.
*
*
* @return int
*
* @throws Exception
*/
protected function processReport($xmlString)
{
$xml = \simplexml_load_string($xmlString);
if ($xml === false) {
$this->builder->log($xmlString);
throw new RuntimeException('Could not process PHPMD report XML.');
}
$warnings = 0;
foreach ($xml->file as $file) {
$fileName = (string)$file['name'];
$fileName = \str_replace($this->builder->buildPath, '', $fileName);
foreach ($file->violation as $violation) {
$warnings++;
$this->build->reportError(
$this->builder,
self::pluginName(),
(string)$violation,
PHPCensor\Model\BuildError::SEVERITY_HIGH,
$fileName,
(int)$violation['beginline'],
(int)$violation['endline']
);
}
}
return $warnings;
}
/**
* Execute Sensiolabs Insight.
*/
protected function executeSensiolabsInsight($binaryPath)
{
$cmd = $binaryPath . ' -n analyze --reference %s %s --api-token %s --user-uuid %s';
// Run Sensiolabs Insight:
$this->builder->executeCommand(
$cmd,
$this->build->getBranch(),
$this->projectUuid,
$this->authToken,
$this->userUuid
);
$cmd = $binaryPath . ' -n analysis --format pmd %s --api-token %s --user-uuid %s';
// Run Sensiolabs Insight:
$this->builder->executeCommand(
$cmd,
$this->projectUuid,
$this->authToken,
$this->userUuid
);
}
/**
* Returns a bool indicating if the error count can be considered a success.
*
* @param int $errorCount
*
* @return bool
*/
protected function wasLastExecSuccessful($errorCount)
{
if ($this->allowedWarnings !== -1 && $errorCount > $this->allowedWarnings) {
return false;
}
return true;
}
}