-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.php
executable file
·170 lines (148 loc) · 6.77 KB
/
init.php
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
CONST VALIDATOR_URL = 'https://validator.prestashop.com';
(new SingleCommandApplication())
->setName('ValidatorDecider') // Optional
->setVersion('1.0.0') // Optional
->addArgument('github_link', InputArgument::OPTIONAL, 'The github_link')
->addArgument('github_branch', InputArgument::OPTIONAL, 'The github_branch')
->setCode(function (InputInterface $input, OutputInterface $output) {
$buffer = new BufferedOutput($output->getVerbosity());
$io = new SymfonyStyle($input, $buffer);
$client = new \GuzzleHttp\Client([
'base_uri' => VALIDATOR_URL
]);
// Call validator
try {
$response = $client->post('/api/modules', [
'multipart' => [
[
'name' => 'github_link',
'contents' => $input->getArgument('github_link'),
],
[
'name' => 'github_branch',
'contents' => $input->getArgument('github_branch'),
],
[
'name' => 'key',
'contents' => getenv('VALIDATOR_API_KEY')
]
]
]);
$stdResponse = json_decode($response->getBody()->getContents(), true);
} catch (\Throwable $th) {
$io->writeln($th->getMessage());
// /!\ this will trigger auto close of validation addons side
// maybe we should return success and accept that validator can be down ?
return Command::FAILURE;
}
// Format response
$io->title('Validator Results');
$isValid = true;
foreach ($stdResponse as $category => $reports) {
switch ($category) {
case 'Details':
// do nothing
break;
case 'Security':
$count = 0;
$table = new Table($buffer);
$table->setHeaders(['File', 'Error']);
foreach ($reports as $item) {
foreach ($item as $rule) {
if (is_array($rule)) {
foreach ($rule as $error) {
foreach ($error as $errorDataStructure) {
if (is_array($errorDataStructure)) {
foreach ($errorDataStructure as $filesErrors) {
$table->addRows([
[$filesErrors['file'].':'.$filesErrors['line'], $filesErrors['message']],
]);
$count++;
}
}
}
}
}
}
}
if ($count >= 1) {
$io->writeln('<details>');
$io->writeln("<summary>$category</summary>");
$io->writeln("<pre>");
$table->setStyle('box');
// $table->setColumnMaxWidth() to change width
$table->render();
$io->writeln('</details>');
}
break;
case 'Structure':
$count = 0;
$table = new Table($buffer);
foreach ($reports as $key => $item) {
$table->addRow([$key]);
$count++;
}
if ($count >= 1) {
$io->writeln('<details>');
$io->writeln("<summary>$category</summary>");
$io->writeln("<pre>");
$table->setStyle('box');
$table->render();
$io->writeln('</details>');
}
break;
default:
$count = 0;
$table = new Table($buffer);
$table->setHeaders(['File', 'Error']);
if (is_array($reports)) {
foreach ($reports as $key => $item) {
foreach ($item as $rule) {
if (is_array($rule)) {
foreach ($rule as $errors) {
foreach ($errors['content'] as $error) {
$table->addRows([
[$error['file'].':'.$error['line'], $error['message']],
]);
$count++;
if (false !== strpos($error['message'], 'should be removed')
|| false !== strpos($error['message'], 'index.php file not found')
) {
$isValid = false;
}
}
}
}
}
}
}
if ($count >= 1) {
$io->writeln('<details>');
$io->writeln("<summary>$category</summary>");
$io->writeln("<pre>");
$table->setStyle('box');
$table->render();
$io->writeln('</details>');
}
break;
}
}
// TODO: use symfony/filesystem
file_put_contents('result_validator.txt', $buffer->fetch());
if (true === $isValid) {
return Command::SUCCESS;
} else {
return Command::FAILURE;
}
})
->run();