Skip to content

Commit

Permalink
changes from review and test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Oct 31, 2023
1 parent 7bba597 commit bda9e9e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 47 deletions.
12 changes: 7 additions & 5 deletions src/Console/Build/GenerateCodeBaselineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$algorithm = $input->getOption('algorithm');

$this->generateManifest($algorithm, $output);
try {
$this->generateManifest($algorithm, $output);
} catch (\Throwable $e) {
$output->writeln('<error>' . sprintf('Failed to generate manifest. Error was: %s', PHP_EOL . $e->getMessage()) . '</error>');
return 1;
}
return 0;
}

private function generateManifest(string $algorithm, OutputInterface $output): void
{
$start = microtime(true);
$manifest_output = GLPI_ROOT . '/version/' . VersionParser::getNormalizedVersion(GLPI_VERSION, false);
$checker = new SourceCodeIntegrityChecker();
$manifest = $checker->generateManifest($algorithm);
$manifest_json = json_encode($manifest, JSON_PRETTY_PRINT);
$manifest_length = strlen($manifest_json);
if (file_put_contents($manifest_output, $manifest_json) !== $manifest_length) {
$output->writeln('<error>' . sprintf('Failed to write manifest to %s', $manifest_output) . '</error>');
return;
throw new \RuntimeException(sprintf('Failed to write manifest to %s', $manifest_output));
}
$duration = microtime(true) - $start;
$output->writeln(sprintf('Manifest of %d files generated', count($manifest['files'])));
}
}
59 changes: 30 additions & 29 deletions src/System/Diagnostic/SourceCodeIntegrityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ private function getBaselineManifest(): ?array
{
$version = VersionParser::getNormalizedVersion(GLPI_VERSION, false);
$manifest = file_get_contents($this->getCheckRootDir() . '/version/' . $version);
$content = json_decode($manifest, true, 512, JSON_THROW_ON_ERROR);
try {
$content = json_decode($manifest, true, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable $e) {
throw new \RuntimeException('The source code file manifest is invalid', previous: $e);
}
if (!isset($content['algorithm'], $content['files']) || !is_string($content['algorithm']) || !is_array($content['files'])) {
throw new \RuntimeException('The source code file manifest is invalid');
}
Expand Down Expand Up @@ -159,27 +163,22 @@ public function getSummary(): array

private function getGLPIRelease(&$errors = []): string|null
{
$gh_releases_endpoint = 'https://api.github.com/repos/glpi-project/glpi/releases';
$version_to_get = VersionParser::getNormalizedVersion(GLPI_VERSION);
$gh_releases_endpoint = 'https://api.github.com/repos/glpi-project/glpi/releases/tags/' . $version_to_get;
$client = new Client();
$dest = null;
try {
$response = $client->request('GET', $gh_releases_endpoint);
$releases = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
foreach ($releases as $release) {
if ($release['tag_name'] === $version_to_get) {
foreach ($release['assets'] as $asset) {
if (str_starts_with($asset['name'], 'glpi-' . $version_to_get)) {
$dest = GLPI_TMP_DIR . '/' . $asset['name'];
// Check if the file already exists in the tmp dir
if (file_exists($dest)) {
break;
}
$url = $asset['browser_download_url'];
$client->request('GET', $url, ['sink' => $dest]);
break;
}
$release = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
foreach ($release['assets'] as $asset) {
if (str_starts_with($asset['name'], 'glpi-' . $version_to_get)) {
$dest = GLPI_TMP_DIR . '/' . $asset['name'];
// Check if the file already exists in the tmp dir
if (file_exists($dest)) {
break;
}
$url = $asset['browser_download_url'];
$client->request('GET', $url, ['sink' => $dest]);
break;
}
}
Expand All @@ -195,18 +194,20 @@ public function getDiff(array &$errors = [], bool $allow_download = false): stri
// ignore OK files in case they are present
$summary = array_filter($summary, static fn ($status) => $status !== self::STATUS_OK);
// Ensure the release is downloaded
if ($allow_download) {
$release_path = $this->getGLPIRelease($errors);
} else {
$release_path = GLPI_TMP_DIR . '/glpi-' . VersionParser::getNormalizedVersion(GLPI_VERSION) . '.tgz';
if (!file_exists($release_path)) {
$release_path = GLPI_TMP_DIR . '/glpi-' . VersionParser::getNormalizedVersion(GLPI_VERSION) . '.tgz';
if (!file_exists($release_path)) {
if ($allow_download) {
$release_path = $this->getGLPIRelease($errors);
if ($release_path === null) {
$errors[] = 'An error occurred while downloading the release';
return '';
}
} else {
$errors[] = 'The release is not downloaded and downloading it was not allowed';
return '';
}
}
if ($release_path === null) {
return '<error>An error occurred while downloading the release</error>';
}

$diff = '';

foreach ($summary as $file => $status) {
Expand All @@ -216,14 +217,14 @@ public function getDiff(array &$errors = [], bool $allow_download = false): stri
$current_file = 'b/' . $file;
$original_content = '';
$current_content = '';
// If status is missing, just add the info to the diff string
if ($status === self::STATUS_MISSING) {
$current_file = '/dev/null';
} else if ($status === self::STATUS_ADDED || !file_exists('phar://' . $release_path . '/glpi/' . $file)) {
if ($status === self::STATUS_ADDED || !file_exists('phar://' . $release_path . '/glpi/' . $file)) {
$original_file = '/dev/null';
$current_content = file_get_contents($this->getCheckRootDir() . '/' . $file);
} else {
$original_content = file_get_contents('phar://' . $release_path . '/glpi/' . $file);
}
if ($status === self::STATUS_MISSING || !file_exists($this->getCheckRootDir() . '/' . $file)) {
$current_file = '/dev/null';
} else {
$current_content = file_get_contents($this->getCheckRootDir() . '/' . $file);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public function testGetSummary()
unlink(vfsStream::url('check_root_dir/src/test2.php'));

$this->array($checker->getSummary())->isEqualTo([
'src/test.php' => 1,
'src/test2.php' => 2,
'src/test3.php' => 3,
'src/test.php' => 1, // 1 = STATUS_ALTERED
'src/test2.php' => 2, // 2 = STATUS_MISSING
'src/test3.php' => 3, // 3 = STATUS_ADDED
]);
}

Expand Down Expand Up @@ -154,17 +154,16 @@ public function testGetDiff()
continue;
}
$path = preg_replace($path_pattern, '', $file->getPathname());
$phar->addFromString($path, file_get_contents($file->getPathname()));
$phar->addFromString('glpi/' . $path, file_get_contents($file->getPathname()));
}
$phar->compress(\Phar::GZ);
// Rename from .tar.gz to .tgz because it changes the extension however it pleases
$this->boolean(rename(GLPI_TMP_DIR . '/glpi-' . $version_full . '.tar.gz', GLPI_TMP_DIR . '/glpi-' . $version_full . '.tgz'))->isTrue();

unlink(vfsStream::url('check_root_dir/src/test.php'));
file_put_contents(vfsStream::url('check_root_dir/src/test2.php'), <<<EOL
line 1
line b
line 3
line1
lineb
line3
EOL
);
file_put_contents(vfsStream::url('check_root_dir/src/test3.php'), 'added');
Expand All @@ -175,12 +174,13 @@ public function testGetDiff()
$this->array($errors)->isEqualTo([]);
$this->string(trim($diff))->isEqualTo(<<<EOL
diff --git a/src/test2.php b/src/test2.php
--- /dev/null
--- a/src/test2.php
+++ b/src/test2.php
@@ -1,0 +1,3 @@
+line 1
+line b
+line 3
@@ -1,3 +1,3 @@
line1
-line2
+lineb
line3
diff --git a/src/test3.php b/src/test3.php
--- /dev/null
Expand All @@ -191,6 +191,8 @@ public function testGetDiff()
diff --git a/src/test.php b/src/test.php
--- a/src/test.php
+++ /dev/null
@@ -1 +1,0 @@
-test1
EOL
);
}
Expand Down

0 comments on commit bda9e9e

Please sign in to comment.