|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace whm\Smoke\Extensions\SmokeReporter\Reporter; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
6
|
|
|
use whm\Smoke\Config\Configuration; |
|
7
|
|
|
use whm\Smoke\Scanner\Result; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class XUnitReporter. |
|
11
|
|
|
*/ |
|
12
|
|
|
class KoalamonReporter implements Reporter |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Result[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $results; |
|
18
|
|
|
|
|
19
|
|
|
private $koaloMon = 'http://www.koalamon.com/app_dev.php/webhook/'; |
|
20
|
|
|
private $apiKey; |
|
21
|
|
|
private $config; |
|
22
|
|
|
private $system; |
|
23
|
|
|
private $collect; |
|
24
|
|
|
private $identifier; |
|
25
|
|
|
|
|
26
|
|
|
private $output; |
|
27
|
|
|
|
|
28
|
|
|
const STATUS_SUCCESS = 'success'; |
|
29
|
|
|
const STATUS_FAILURE = 'failure'; |
|
30
|
|
|
|
|
31
|
|
|
public function init($apiKey, $system, $identifier = "", $collect = true, Configuration $_configuration, OutputInterface $_output) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->config = $_configuration; |
|
34
|
|
|
$this->apiKey = $apiKey; |
|
35
|
|
|
$this->system = $system; |
|
36
|
|
|
$this->collect = $collect; |
|
37
|
|
|
$this->identifier = $identifier; |
|
38
|
|
|
|
|
39
|
|
|
$this->output = $_output; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Rule []; |
|
44
|
|
|
*/ |
|
45
|
|
|
private function getRuleKeys() |
|
46
|
|
|
{ |
|
47
|
|
|
$keys = array(); |
|
48
|
|
|
foreach ($this->config->getRules() as $key => $rule) { |
|
49
|
|
|
$keys[] = $key; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $keys; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function processResult(Result $result) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->results[] = $result; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function finish() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->output->writeln("Sending results to www.koalamon.com ... \n"); |
|
63
|
|
|
|
|
64
|
|
|
if ($this->collect) { |
|
65
|
|
|
$this->sendCollected(); |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->sendSingle(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function sendSingle() |
|
72
|
|
|
{ |
|
73
|
|
|
$rules = $this->getRuleKeys(); |
|
74
|
|
|
foreach ($this->results as $result) { |
|
75
|
|
|
$failedTests = array(); |
|
76
|
|
|
if ($result->isFailure()) { |
|
77
|
|
|
foreach ($result->getMessages() as $ruleLKey => $message) { |
|
78
|
|
|
$identifier = 'smoke_' . $ruleLKey . '_' . $result->getUrl(); |
|
79
|
|
|
$this->send($identifier, $this->system, 'smoke', $message, self::STATUS_FAILURE, (string)$result->getUrl()); |
|
80
|
|
|
$failedTests[] = $ruleLKey; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
foreach ($rules as $rule) { |
|
84
|
|
|
if (!in_array($rule, $failedTests, true)) { |
|
85
|
|
|
$identifier = 'smoke_' . $rule . '_' . $result->getUrl(); |
|
86
|
|
|
$this->send($identifier, $this->system, 'smoke_' . $rule, '', self::STATUS_SUCCESS, (string)$result->getUrl()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function sendCollected() |
|
93
|
|
|
{ |
|
94
|
|
|
$failureMessages = array(); |
|
95
|
|
|
|
|
96
|
|
|
foreach ($this->getRuleKeys() as $rule) { |
|
97
|
|
|
$failureMessages[$rule] = ""; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
foreach ($this->results as $result) { |
|
101
|
|
|
if ($result->isFailure()) { |
|
102
|
|
|
foreach ($result->getMessages() as $ruleLKey => $message) { |
|
103
|
|
|
if ($failureMessages[$ruleLKey] == "") { |
|
104
|
|
|
$failureMessages[$ruleLKey] = " The smoke test for " . $this->system . " failed (Rule: " . $ruleLKey . ").<ul>"; |
|
105
|
|
|
} |
|
106
|
|
|
$failureMessages[$ruleLKey] .= "<li>" . $message . "(url: " . $result->getUrl() . ")</li>"; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach ($failureMessages as $key => $failureMessage) { |
|
112
|
|
|
if ($failureMessage != "") { |
|
113
|
|
|
$this->send($this->identifier . '_' . $key, $this->system, 'smoke', $failureMessage . '</ul>', self::STATUS_FAILURE, ""); |
|
114
|
|
|
} else { |
|
115
|
|
|
$this->send($this->identifier . '_' . $key, $this->system, 'smoke', "", self::STATUS_SUCCESS, ""); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function send($identifier, $system, $tool, $message, $status, $url = '') |
|
121
|
|
|
{ |
|
122
|
|
|
$curl = curl_init(); |
|
123
|
|
|
$responseBody = array( |
|
124
|
|
|
'system' => $system, |
|
125
|
|
|
'status' => $status, |
|
126
|
|
|
'message' => $message, |
|
127
|
|
|
'identifier' => $identifier, |
|
128
|
|
|
'type' => $tool, |
|
129
|
|
|
'url' => $url, |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
$koalamonUrl = $this->koaloMon . '?api_key=' . $this->apiKey; |
|
133
|
|
|
curl_setopt_array($curl, array( |
|
134
|
|
|
CURLOPT_URL => $koalamonUrl, |
|
135
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
136
|
|
|
CURLOPT_ENCODING => '', |
|
137
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
138
|
|
|
CURLOPT_TIMEOUT => 30, |
|
139
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
140
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST', |
|
141
|
|
|
CURLOPT_POSTFIELDS => json_encode($responseBody), |
|
142
|
|
|
)); |
|
143
|
|
|
$response = curl_exec($curl); |
|
144
|
|
|
|
|
145
|
|
|
$err = curl_error($curl); |
|
146
|
|
|
curl_close($curl); |
|
147
|
|
|
|
|
148
|
|
|
return $err; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|