Skip to content

Commit 2cc1f6d

Browse files
Add container registry API (#885)
1 parent ff191bb commit 2cc1f6d

8 files changed

Lines changed: 398 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
* Add support for merge request resource label event endpoints
2929
* Add support for merge request and merge request note award emoji endpoints
3030
* Add support for `MergeRequests::remove`
31+
* Add support for container registry endpoints
3132
* Add support for `Environments::stopStale`
3233
* Add support for `last_activity_after` and `last_activity_before` in `Groups::projects`
3334
* Fix `Projects::pipelines` date filters to include time information

‎src/Api/Groups.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,13 @@ public function packages(int|string $group_id, array $parameters = []): mixed
733733
return $this->get('groups/'.self::encodePath($group_id).'/packages', $resolver->resolve($parameters));
734734
}
735735

736+
public function registryRepositories(int|string $group_id, array $parameters = []): mixed
737+
{
738+
$resolver = $this->createOptionsResolver();
739+
740+
return $this->get('groups/'.self::encodePath($group_id).'/registry/repositories', $resolver->resolve($parameters));
741+
}
742+
736743
private function getGroupSearchResolver(): OptionsResolver
737744
{
738745
$resolver = $this->getSubgroupSearchResolver();

‎src/Api/Projects.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,32 @@ public function removeJobTokenScopeAllowlistGroup(int|string $project_id, int $t
13451345
return $this->delete($this->getProjectPath($project_id, 'job_token_scope/groups_allowlist/'.self::encodePath($target_group_id)));
13461346
}
13471347

1348+
/**
1349+
* @param array $parameters {
1350+
*
1351+
* @var bool $tags include an array of tags in each repository
1352+
* @var bool $tags_count include tags_count in each repository
1353+
* }
1354+
*/
1355+
public function registryRepositories(int|string $project_id, array $parameters = []): mixed
1356+
{
1357+
$resolver = $this->createOptionsResolver();
1358+
$booleanNormalizer = function (Options $resolver, $value): string {
1359+
return $value ? 'true' : 'false';
1360+
};
1361+
1362+
$resolver->setDefined('tags')
1363+
->setAllowedTypes('tags', 'bool')
1364+
->setNormalizer('tags', $booleanNormalizer)
1365+
;
1366+
$resolver->setDefined('tags_count')
1367+
->setAllowedTypes('tags_count', 'bool')
1368+
->setNormalizer('tags_count', $booleanNormalizer)
1369+
;
1370+
1371+
return $this->get($this->getProjectPath($project_id, 'registry/repositories'), $resolver->resolve($parameters));
1372+
}
1373+
13481374
public function protectedTags(int|string $project_id): mixed
13491375
{
13501376
return $this->get('projects/'.self::encodePath($project_id).'/protected_tags');

‎src/Api/Registry.php‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Gitlab API library.
7+
*
8+
* (c) Matt Humphrey <matth@windsor-telecom.co.uk>
9+
* (c) Graham Campbell <hello@gjcampbell.co.uk>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Gitlab\Api;
16+
17+
use Symfony\Component\OptionsResolver\Options;
18+
use Symfony\Component\OptionsResolver\OptionsResolver;
19+
20+
class Registry extends AbstractApi
21+
{
22+
/**
23+
* @param array $parameters {
24+
*
25+
* @var bool $tags include an array of tags in the response
26+
* @var bool $tags_count include tags_count in the response
27+
* @var bool $size include the deduplicated size in the response
28+
* }
29+
*/
30+
public function repository(int|string $repository_id, array $parameters = []): mixed
31+
{
32+
$resolver = self::createRepositoryResolver();
33+
34+
return $this->get('registry/repositories/'.self::encodePath($repository_id), $resolver->resolve($parameters));
35+
}
36+
37+
public function removeRepository(int|string $project_id, int $repository_id): mixed
38+
{
39+
return $this->delete($this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($repository_id)));
40+
}
41+
42+
public function repositoryTags(int|string $project_id, int $repository_id, array $parameters = []): mixed
43+
{
44+
$resolver = $this->createOptionsResolver();
45+
46+
return $this->get(
47+
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($repository_id).'/tags'),
48+
$resolver->resolve($parameters)
49+
);
50+
}
51+
52+
public function repositoryTag(int|string $project_id, int $repository_id, string $tag_name): mixed
53+
{
54+
return $this->get($this->getProjectPath(
55+
$project_id,
56+
'registry/repositories/'.self::encodePath($repository_id).'/tags/'.self::encodePath($tag_name)
57+
));
58+
}
59+
60+
public function removeRepositoryTag(int|string $project_id, int $repository_id, string $tag_name): mixed
61+
{
62+
return $this->delete($this->getProjectPath(
63+
$project_id,
64+
'registry/repositories/'.self::encodePath($repository_id).'/tags/'.self::encodePath($tag_name)
65+
));
66+
}
67+
68+
/**
69+
* @param array $parameters {
70+
*
71+
* @var string $name_regex_delete regex of tag names to delete
72+
* @var string $name_regex_keep regex of tag names to keep
73+
* @var int $keep_n number of latest matching tags to keep
74+
* @var string $older_than delete tags older than this human-readable duration
75+
* }
76+
*/
77+
public function removeRepositoryTags(int|string $project_id, int $repository_id, array $parameters): mixed
78+
{
79+
$resolver = new OptionsResolver();
80+
$resolver->setRequired('name_regex_delete')
81+
->setAllowedTypes('name_regex_delete', 'string')
82+
;
83+
$resolver->setDefined('name_regex_keep')
84+
->setAllowedTypes('name_regex_keep', 'string')
85+
;
86+
$resolver->setDefined('keep_n')
87+
->setAllowedTypes('keep_n', 'int')
88+
;
89+
$resolver->setDefined('older_than')
90+
->setAllowedTypes('older_than', 'string')
91+
;
92+
93+
return $this->delete(
94+
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($repository_id).'/tags'),
95+
$resolver->resolve($parameters)
96+
);
97+
}
98+
99+
private static function createRepositoryResolver(): OptionsResolver
100+
{
101+
$resolver = new OptionsResolver();
102+
$booleanNormalizer = function (Options $resolver, $value): string {
103+
return $value ? 'true' : 'false';
104+
};
105+
106+
$resolver->setDefined('tags')
107+
->setAllowedTypes('tags', 'bool')
108+
->setNormalizer('tags', $booleanNormalizer)
109+
;
110+
$resolver->setDefined('tags_count')
111+
->setAllowedTypes('tags_count', 'bool')
112+
->setNormalizer('tags_count', $booleanNormalizer)
113+
;
114+
$resolver->setDefined('size')
115+
->setAllowedTypes('size', 'bool')
116+
->setNormalizer('size', $booleanNormalizer)
117+
;
118+
119+
return $resolver;
120+
}
121+
}

‎src/Client.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Gitlab\Api\PersonalAccessTokens;
3434
use Gitlab\Api\ProjectNamespaces;
3535
use Gitlab\Api\Projects;
36+
use Gitlab\Api\Registry;
3637
use Gitlab\Api\Repositories;
3738
use Gitlab\Api\RepositoryFiles;
3839
use Gitlab\Api\ResourceIterationEvents;
@@ -251,6 +252,11 @@ public function projects(): Projects
251252
return new Projects($this);
252253
}
253254

255+
public function registry(): Registry
256+
{
257+
return new Registry($this);
258+
}
259+
254260
public function repositories(): Repositories
255261
{
256262
return new Repositories($this);

‎tests/Api/GroupsTest.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,42 @@ public function shouldGetPackages(): void
771771
$this->assertEquals($expectedArray, $api->packages(1));
772772
}
773773

774+
#[Test]
775+
public function shouldGetGroupRegistryRepositories(): void
776+
{
777+
$expectedArray = [
778+
['id' => 1, 'name' => 'A registry'],
779+
['id' => 2, 'name' => 'Another registry'],
780+
];
781+
782+
$api = $this->getApiMock();
783+
$api->expects($this->once())
784+
->method('get')
785+
->with('groups/1/registry/repositories', [])
786+
->willReturn($expectedArray)
787+
;
788+
789+
$this->assertEquals($expectedArray, $api->registryRepositories(1));
790+
}
791+
792+
#[Test]
793+
public function shouldGetGroupRegistryRepositoriesWithPagination(): void
794+
{
795+
$expectedArray = [
796+
['id' => 1, 'name' => 'A registry'],
797+
['id' => 2, 'name' => 'Another registry'],
798+
];
799+
800+
$api = $this->getApiMock();
801+
$api->expects($this->once())
802+
->method('get')
803+
->with('groups/1/registry/repositories', ['page' => 2, 'per_page' => 15])
804+
->willReturn($expectedArray)
805+
;
806+
807+
$this->assertEquals($expectedArray, $api->registryRepositories(1, ['page' => 2, 'per_page' => 15]));
808+
}
809+
774810
#[Test]
775811
public function shouldGetGroupMergeRequests(): void
776812
{

‎tests/Api/ProjectsTest.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,6 +3000,57 @@ public function shouldRemoveJobTokenScopeAllowlistGroup(): void
30003000
$this->assertEquals($expectedString, $api->removeJobTokenScopeAllowlistGroup(1, 42));
30013001
}
30023002

3003+
#[Test]
3004+
public function shouldGetProjectRegistryRepositories(): void
3005+
{
3006+
$expectedArray = [
3007+
['id' => 1, 'name' => 'A registry'],
3008+
['id' => 2, 'name' => 'Another registry'],
3009+
];
3010+
3011+
$api = $this->getApiMock();
3012+
$api->expects($this->once())
3013+
->method('get')
3014+
->with('projects/123/registry/repositories', [])
3015+
->willReturn($expectedArray);
3016+
3017+
$this->assertEquals($expectedArray, $api->registryRepositories(123));
3018+
}
3019+
3020+
#[Test]
3021+
public function shouldGetProjectRegistryRepositoriesWithTags(): void
3022+
{
3023+
$expectedArray = [
3024+
['id' => 1, 'name' => 'A registry', 'tags' => ['1.0', '1.1'], 'tags_count' => 2],
3025+
['id' => 2, 'name' => 'Another registry', 'tags' => ['2.0', '2.1'], 'tags_count' => 2],
3026+
];
3027+
3028+
$api = $this->getApiMock();
3029+
$api->expects($this->once())
3030+
->method('get')
3031+
->with('projects/123/registry/repositories', ['tags' => 'true', 'tags_count' => 'true'])
3032+
->willReturn($expectedArray);
3033+
3034+
$this->assertEquals($expectedArray, $api->registryRepositories(123, ['tags' => true, 'tags_count' => true]));
3035+
}
3036+
3037+
#[Test]
3038+
public function shouldGetProjectRegistryRepositoriesWithPagination(): void
3039+
{
3040+
$expectedArray = [
3041+
['id' => 1, 'name' => 'A registry'],
3042+
['id' => 2, 'name' => 'Another registry'],
3043+
];
3044+
3045+
$api = $this->getApiMock();
3046+
$api->expects($this->once())
3047+
->method('get')
3048+
->with('projects/123/registry/repositories', ['page' => 2, 'per_page' => 15])
3049+
->willReturn($expectedArray);
3050+
3051+
$this->assertEquals($expectedArray, $api->registryRepositories(123, ['page' => 2, 'per_page' => 15]));
3052+
}
3053+
30033054
#[Test]
30043055
public function shouldUploadAvatar(): void
30053056
{

0 commit comments

Comments
 (0)