Buildbot: Move checksum to JSON #7

Merged
Bart van der Braak merged 2 commits from move-checksum-to-json into develop 2024-07-16 10:09:54 +02:00
3 changed files with 25 additions and 6 deletions

View File

@ -22,6 +22,7 @@ class Build {
public $file_size;
public $file_extension;
public $release_cycle;
public $sha256_checksum;
bartvdbraak marked this conversation as resolved Outdated

Don't refer to a time-line type of a thing. The property is new from the perspective of this PR, but once it lands the property is not new, is kist there. Simplty public $sha256_checksum;.

Don't refer to a time-line type of a thing. The property is new from the perspective of this PR, but once it lands the property is not new, is kist there. Simplty `public $sha256_checksum;`.
public $directory_lister;
public function __construct(string $file_path) {
@ -83,6 +84,14 @@ class Build {
$this->file_size = filesize($file_path);
$this->file_extension = $file_extension;
// Check for existing .sha256 file.
$sha256_file = $file_path . '.sha256';
if (file_exists($sha256_file)) {
$this->sha256_checksum = trim(str_replace(["\r", "\n", "\t"], '', file_get_contents($sha256_file)));
} else {
$this->sha256_checksum = null;
}
bartvdbraak marked this conversation as resolved Outdated

Computing hash of files is not cheap, is not something we should be doing for an end-point which is considered fast.
If the checksum file is missing, it is an indication of some bigger problem, so might as well keep the field empty.

Computing hash of files is not cheap, is not something we should be doing for an end-point which is considered fast. If the checksum file is missing, it is an indication of some bigger problem, so might as well keep the field empty.
// Release cycle.
//
// NOTE: Keep it last, so the rule matcher can access all possible

View File

@ -8,11 +8,11 @@ class BuildsRenderer {
$this->builds = $builds;
}
public function renderJSON() {
public function renderJSON($version = 1) {
/* Return a JSON formatted list of builds. */
$builds_json = array();
foreach ($this->builds as $build) {
$builds_json[] = array(
$build_data = array(
'url' => $this->getFileNameURL($build),
'app' => $build->app,
'version' => $build->version,
@ -29,6 +29,16 @@ class BuildsRenderer {
'file_extension' => $build->file_extension,
'release_cycle' => $build->release_cycle,
);
if ($version == 2) {
// If version 2 is requested, skip any .sha256 files and add a checksum key-value entry
if (endsWith($build->file_name, '.sha256')) {
bartvdbraak marked this conversation as resolved Outdated

We have function endsWith(string $haystack, string $needle) utility function, so can simply do if (endsWith($build->file_name, '.sha256')).

We have `function endsWith(string $haystack, string $needle)` utility function, so can simply do `if (endsWith($build->file_name, '.sha256'))`.
continue;
}
$build_data['checksum'] = $build->sha256_checksum;
}
$builds_json[] = $build_data;
}
echo(json_encode($builds_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

View File

@ -15,16 +15,16 @@ function handleNotFoundRequest() {
function renderDownloadResponseAsJSON($lister) {
header('Content-Type: application/json; charset=utf-8');
// Require v=1 to be specified
if (!isset($_GET['v']) || $_GET['v'] != '1') {
$data = [ 'error' => 'Missing supported format version, for example v=1'];
// Require v=1 or v=2 to be specified
if (!isset($_GET['v']) || ($_GET['v'] != '1' && $_GET['v'] != '2')) {
$data = ['error' => 'Invalid version specified. Please provide a supported version using v=1 or v=2.'];
echo json_encode( $data );
http_response_code(400);
return;
}
$builds = $lister->getBuilds();
$renderer = new BuildsRenderer($builds);
$renderer->renderJSON();
$renderer->renderJSON($_GET['v']);
return;
}