forked from PrestaShopCorp/github-action-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.php
executable file
·175 lines (152 loc) · 6.54 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
171
172
173
174
175
<?php
require_once __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
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;
use Symfony\Component\Console\Style\SymfonyStyle;
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')
->addArgument('archive', InputArgument::OPTIONAL, 'The archive of the module')
->setCode(function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$client = new Client([
'base_uri' => VALIDATOR_URL
]);
$apiKey = getenv('VALIDATOR_API_KEY');
if (empty($apiKey)) {
throw new Exception('No API Key is set to authenticate the request to the validator. Please set the env var VALIDATOR_API_KEY');
}
$multipart = [
[
'name' => 'key',
'contents' => $apiKey
]
];
// Call validator
try {
if ($input->hasArgument('archive')) {
$archive = $input->getArgument('archive');
if (empty($archive) || !file_exists($archive) || !is_readable($archive)) {
throw new Exception(sprintf('File %s was not found, or cannot be read', $archive));
}
$multipart[] = [
'name' => 'archive',
'contents' => fopen($archive, 'r'),
];
} else {
$multipart[] = [
'name' => 'github_link',
'contents' => $input->getArgument('github_link'),
];
$multipart[] = [
'name' => 'github_branch',
'contents' => $input->getArgument('github_branch'),
];
}
$response = $client->post('/api/modules', [
'multipart' => $multipart
]);
$stdResponse = json_decode($response->getBody()->getContents(), true);
} catch (Throwable $th) {
$io->writeln($th->getMessage());
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($output);
$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->error("$category");
$table->setStyle('box');
$table->render();
}
break;
case 'Structure':
$count = 0;
$table = new Table($output);
foreach ($reports as $key => $item) {
$table->addRow([$key]);
$count++;
}
if ($count >= 1) {
$io->error("$category");
$table->setStyle('box');
$table->render();
}
break;
default:
$count = 0;
$table = new Table($output);
$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->error("$category");
$table->setStyle('box');
$table->render();
}
break;
}
}
// TODO: use symfony/filesystem
if (true === $isValid) {
return Command::SUCCESS;
} else {
return Command::FAILURE;
}
})
->run();