-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.php
executable file
·97 lines (83 loc) · 2.99 KB
/
run.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
#!/usr/bin/env php -d memory_limit=-1
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kanopi\SonarQube\RunReport;
use Kanopi\SonarQube\SonarQube;
use mikehaertl\wkhtmlto\Pdf;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
$sonarQubeHost = $_ENV['SONARQUBE_HOST'] ?? 'https://sonarcloud.io';
$sonarQubeUser = $_ENV['SONARQUBE_USER'];
$sonarQubePass = $_ENV['SONARQUBE_PASS'];
$sonarQubeProjects = explode(',', $_ENV['SONARQUBE_PROJECTS']);
$sonarQubeReportDir = $_ENV['SONARQUBE_REPORT_DIR'] ?? './';
$sonarQubeFile = $sonarQubeReportDir . ($_ENV['SONARQUBE_REPORT_FILE'] ?? 'report.pdf');
$sonarQubeExtraParams = json_decode($_ENV['SONARQUBE_EXTRA_PARAMS'], true) ?? [];
if (empty($sonarQubeHost) || empty($sonarQubeUser) || empty($sonarQubeProjects)) {
echo "One of the following variables are not defined: SONARQUBE_HOST, SONARQUBE_USER, SONARQUBE_PROJECTS";
die;
}
// Create Logger
$logger = new Logger('sonarqube-report');
$streamHandler = new StreamHandler('php://stdout', Logger::DEBUG);
$lineFormatter = new LineFormatter(
"[%datetime%] %level_name% > %message% %context% %extra%\n",
"Y-m-d H:i:s"
);
$streamHandler->setFormatter($lineFormatter);
$logger->pushHandler($streamHandler);
$debug = !empty($_ENV['DEBUG_REPORT']);
// Only put in debug mode if the dev libraries are installed.
if ($debug && class_exists('\VCR\VCR')) {
// Change matching query for VCR Cassette
\VCR\VCR::configure()->enableRequestMatchers(['method', 'url', 'query_string',]);
// Turn on the VCR service.
\VCR\VCR::turnOn();
$logger->debug('Starting VCR');
// Store report in a specific location.
\VCR\VCR::insertCassette( 'sonarqube.yml');
$logger->debug('Inserting Cassette');
}
// Create PDF Element.
$pdf = new Pdf([
'header-line',
'header-font-size' => 9,
'header-spacing' => 3,
'footer-left' => 'Generated by Kanopi Studios',
'footer-right'=>'[page] of [topage] pages',
'footer-font-size' => 9,
'footer-spacing' => 3,
'footer-line',
'javascript-delay' => '5000'
]);
// Create Twig
$filesystemLoader = new FilesystemLoader(__DIR__ . '/templates');
$environment = new Environment($filesystemLoader);
// Create SonarQube service.
$sonarQube = SonarQube::create($sonarQubeHost, $sonarQubeUser, $sonarQubePass, $sonarQubeExtraParams);
$logger->info('Starting');
try {
// Create a new run report service.
(new RunReport(
$sonarQube,
$logger,
$environment,
$pdf
))->createReport($sonarQubeProjects, $sonarQubeFile);
$logger->info('Complete');
} catch (Exception $exception) {
$logger->error($exception->getMessage());
}
if ($debug) {
// Eject the tape from recording anymore.
\VCR\VCR::eject();
$logger->debug('Cassette Ejected');
// Turn off the VCR not needed anymore.
\VCR\VCR::turnOff();
$logger->debug('VCR Turned Off');
}