From 0dbde148b74fe7fe52e92bcac02606f5460d5e0d Mon Sep 17 00:00:00 2001 From: tristan Date: Mon, 6 Apr 2026 15:22:22 +0200 Subject: [PATCH] feat : add GiteaRegistryService for listing container tags --- src/Service/GiteaRegistryService.php | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/Service/GiteaRegistryService.php diff --git a/src/Service/GiteaRegistryService.php b/src/Service/GiteaRegistryService.php new file mode 100644 index 0000000..d32c6cf --- /dev/null +++ b/src/Service/GiteaRegistryService.php @@ -0,0 +1,72 @@ + + */ + 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; + } +}