/
lint-pr
57 lines (47 loc) · 1.68 KB
/
lint-pr
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
#!/usr/bin/env php
<?php
if (empty($argv[1])) {
echo 'You must provide a base branch to check this PR against.';
echo "\n";
exit(1);
}
// Get a changelist of files from Git for this PR.
$fileList = shell_exec('git diff --name-only --diff-filter=ACMR origin/' . $argv[1] . ' HEAD');
$files = array_filter(explode("\n", $fileList));
foreach ($files as &$file) {
if (strpos($file, ' ') !== false) {
$file = str_replace(' ', '\\ ', $file);
}
}
// Run all changed files through the parallel-linter
$output = shell_exec('printf "%s\n" "' . implode('" "', $files) . '" | parallel-lint --no-colors --no-progress --stdin --json');
$output = json_decode($output, true);
if (!count($output["results"]["filesWithSyntaxError"])) {
echo "\e[0;32mFound no issues with code quality.\e[0m";
echo "\n";
exit(0);
} else {
$totalNumberOfIssues = count($output["results"]["filesWithSyntaxError"]);
// Render report
echo "\e[0;31mFound "
. (($totalNumberOfIssues === 1)
? '1 issue'
: $totalNumberOfIssues . ' issues')
. " with code quality.\e[0m";
echo "\n";
$errors = $output["results"]["errors"];
unset($file);
foreach ($output["results"]["filesWithSyntaxError"] as $file) {
echo "\n";
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
echo "\n\n";
$errors_in_file = array_filter($errors, fn ($elem) => $elem["file"] === $file);
foreach ($errors_in_file as $error) {
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
echo "\e[0;31mERR:\e[0m ";
echo $error['message'];
echo "\n";
}
}
exit(1);
}