feat : add GiteaRegistryService for listing container tags
This commit is contained in:
72
src/Service/GiteaRegistryService.php
Normal file
72
src/Service/GiteaRegistryService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
final readonly class GiteaRegistryService
|
||||
{
|
||||
public function __construct(
|
||||
private HttpClientInterface $httpClient,
|
||||
#[Autowire('%env(GITEA_API_URL)%')]
|
||||
private string $giteaApiUrl,
|
||||
#[Autowire('%env(GITEA_API_TOKEN)%')]
|
||||
private string $giteaApiToken,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* List available tags for a container image.
|
||||
*
|
||||
* @param string $registryImage e.g. "gitea.malio.fr/malio-dev/sirh"
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function listTags(string $registryImage): array
|
||||
{
|
||||
$parts = explode('/', $registryImage);
|
||||
|
||||
if (\count($parts) < 3) {
|
||||
throw new \InvalidArgumentException(sprintf('Invalid registry image format: "%s". Expected "registry/owner/package".', $registryImage));
|
||||
}
|
||||
|
||||
$owner = $parts[1];
|
||||
$package = implode('/', \array_slice($parts, 2));
|
||||
|
||||
$url = sprintf('%s/v2/%s/%s/tags/list', $this->giteaApiUrl, $owner, $package);
|
||||
|
||||
$response = $this->httpClient->request('GET', $url, [
|
||||
'headers' => [
|
||||
'Authorization' => sprintf('token %s', $this->giteaApiToken),
|
||||
],
|
||||
'timeout' => 10,
|
||||
]);
|
||||
|
||||
$data = $response->toArray();
|
||||
|
||||
$tags = $data['tags'] ?? [];
|
||||
|
||||
usort($tags, function (string $a, string $b): int {
|
||||
$aIsVersion = str_starts_with($a, 'v');
|
||||
$bIsVersion = str_starts_with($b, 'v');
|
||||
|
||||
if ($aIsVersion && $bIsVersion) {
|
||||
return version_compare(ltrim($b, 'v'), ltrim($a, 'v'));
|
||||
}
|
||||
|
||||
if ($aIsVersion) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ($bIsVersion) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return strcmp($a, $b);
|
||||
});
|
||||
|
||||
return $tags;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user