Skip to content

Commit 1b71cde

Browse files
committed
Implement Container registry API
Implement the functions from the Container registry API described in https://docs.gitlab.com/ee/api/container_registry.html
1 parent 6b80588 commit 1b71cde

File tree

6 files changed

+362
-0
lines changed

6 files changed

+362
-0
lines changed

src/Api/Groups.php

+12
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,16 @@ public function search($id, array $parameters = [])
10311031

10321032
return $this->get('groups/'.self::encodePath($id).'/search', $resolver->resolve($parameters));
10331033
}
1034+
1035+
/**
1036+
* Get a list of registry repositories in a group
1037+
* @see https://docs.gitlab.com/ee/api/container_registry.html#within-a-group
1038+
*
1039+
* @param $id: The ID of a group
1040+
* @return mixed
1041+
*/
1042+
public function registryRepositories(int $id)
1043+
{
1044+
return $this->get('groups/'.self::encodePath($id).'/registry/repositories');
1045+
}
10341046
}

src/Api/Projects.php

+28
Original file line numberDiff line numberDiff line change
@@ -1832,4 +1832,32 @@ public function search($id, array $parameters = [])
18321832

18331833
return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters));
18341834
}
1835+
1836+
/**
1837+
* @see https://docs.gitlab.com/ee/api/container_registry.html#within-a-project
1838+
*
1839+
* @param int|string $project_id
1840+
* @param array $parameters {
1841+
* @var bool $tags If the parameter is included as true, each repository includes an array of "tags" in the response.
1842+
* @var bool $tags_count If the parameter is included as true, each repository includes "tags_count" in the response.
1843+
* }
1844+
*
1845+
* @return mixed
1846+
*/
1847+
public function registryRepositories($project_id, array $parameters = [])
1848+
{
1849+
$resolver = $this->createOptionsResolver();
1850+
$booleanNormalizer = function (Options $resolver, $value): string {
1851+
return $value ? 'true' : 'false';
1852+
};
1853+
1854+
$resolver->setDefined('tags')
1855+
->setAllowedTypes('tags', 'bool')
1856+
->setNormalizer('tags', $booleanNormalizer);
1857+
$resolver->setDefined('tags_count')
1858+
->setAllowedTypes('tags_count', 'bool')
1859+
->setNormalizer('tags_count', $booleanNormalizer);
1860+
1861+
return $this->get($this->getProjectPath($project_id, 'registry/repositories'), $resolver->resolve($parameters));
1862+
}
18351863
}

src/Api/Registry.php

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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 <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
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+
* @see https://docs.gitlab.com/ee/api/container_registry.html#get-details-of-a-single-repository
24+
*
25+
* @param int|string $repository_id The ID of the registry repository accessible by the authenticated user.
26+
* @param array $parameters {
27+
* @var bool $tags
28+
* @var bool $tags_count
29+
* @var bool $size
30+
* }
31+
* @return mixed
32+
*/
33+
public function repositories($repository_id, array $parameters = [])
34+
{
35+
$resolver = $this->createOptionsResolver();
36+
$booleanNormalizer = function (Options $resolver, $value): string {
37+
return $value ? 'true' : 'false';
38+
};
39+
40+
$resolver->setDefined('tags')
41+
->setAllowedTypes('tags', 'bool')
42+
->setNormalizer('tags', $booleanNormalizer);
43+
$resolver->setDefined('tags_count')
44+
->setAllowedTypes('tags_count', 'bool')
45+
->setNormalizer('tags_count', $booleanNormalizer);
46+
$resolver->setDefined('size')
47+
->setAllowedTypes('size', 'bool')
48+
->setNormalizer('size', $booleanNormalizer);
49+
50+
return $this->get('registry/repositories/'.self::encodePath($repository_id), $resolver->resolve($parameters));
51+
}
52+
53+
/**
54+
* @see https://docs.gitlab.com/ee/api/container_registry.html#list-registry-repository-tags
55+
*
56+
* @param int|string $project_id
57+
* @param int $repository_id
58+
* @return mixed
59+
*/
60+
public function repositoryTags($project_id, int $repository_id)
61+
{
62+
return $this->get(
63+
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($repository_id).'/tags')
64+
);
65+
}
66+
67+
68+
/**
69+
* @see https://docs.gitlab.com/ee/api/container_registry.html#get-details-of-a-registry-repository-tag
70+
*
71+
* @param int|string $project_id
72+
* @param int $repository_id
73+
* @param string $tag_name
74+
* @return mixed
75+
*/
76+
public function repositoryTag($project_id, int $repository_id, string $tag_name)
77+
{
78+
return $this->get(
79+
$this->getProjectPath(
80+
$project_id,
81+
'registry/repositories/'.self::encodePath($repository_id).'/tags/'.self::encodePath($tag_name)
82+
)
83+
);
84+
}
85+
86+
87+
/**
88+
* @see https://docs.gitlab.com/ee/api/container_registry.html#delete-a-registry-repository-tag
89+
*
90+
* @param int|string $project_id
91+
* @param int $repository_id
92+
* @param string $tag_name
93+
* @return mixed
94+
*/
95+
public function removeRepositoryTag($project_id, int $repository_id, string $tag_name)
96+
{
97+
return $this->delete(
98+
$this->getProjectPath(
99+
$project_id,
100+
'registry/repositories/'.self::encodePath($repository_id).'/tags/'.self::encodePath($tag_name)
101+
)
102+
);
103+
}
104+
105+
/**
106+
* @see https://docs.gitlab.com/ee/api/container_registry.html#delete-registry-repository-tags-in-bulk
107+
*
108+
* @param int|string $project_id
109+
* @param int $repository_id
110+
* @param array $parameters {
111+
* @var string $name_regex_delete
112+
* @var string $name_regex_keep
113+
* @var int $keep_n
114+
* @var string $older_than
115+
* }
116+
* @return mixed
117+
*/
118+
public function removeRepositoryTags($project_id, int $repository_id, array $parameters = [])
119+
{
120+
$resolver = $this->createOptionsResolver();
121+
$resolver->setRequired('name_regex_delete')
122+
->setAllowedTypes('name_regex_delete', 'string');
123+
$resolver->setDefined('name_regex_keep')
124+
->setAllowedTypes('name_regex_keep', 'string');
125+
$resolver->setDefined('keep_n')
126+
->setAllowedTypes('keep_n', 'int');
127+
$resolver->setDefined('older_than')
128+
->setAllowedTypes('older_than', 'string');
129+
130+
131+
return $this->delete(
132+
$this->getProjectPath(
133+
$project_id,
134+
'registry/repositories/'.self::encodePath($repository_id).'/tags'
135+
),
136+
$resolver->resolve($parameters)
137+
);
138+
}
139+
}

tests/Api/GroupsTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -972,4 +972,24 @@ public function shouldSearchGroups(): void
972972
'sort' => 'desc',
973973
]));
974974
}
975+
976+
/**
977+
* @test
978+
*/
979+
public function shouldGetGroupRegistryRepositories(): void
980+
{
981+
$expectedArray = [
982+
['id' => 1, 'name' => 'A registry'],
983+
['id' => 2, 'name' => 'Another registry'],
984+
];
985+
986+
$api = $this->getApiMock();
987+
$api->expects($this->once())
988+
->method('get')
989+
->with('groups/1/registry/repositories')
990+
->will($this->returnValue($expectedArray))
991+
;
992+
993+
$this->assertEquals($expectedArray, $api->registryRepositories(1));
994+
}
975995
}

tests/Api/ProjectsTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -3049,4 +3049,45 @@ public function shouldSearchGroups(): void
30493049
'sort' => 'desc',
30503050
]));
30513051
}
3052+
3053+
/**
3054+
* @test
3055+
*/
3056+
public function shouldGetProjectRegistryRepositories(): void
3057+
{
3058+
$expectedArray = [
3059+
['id' => 1, 'name' => 'A registry'],
3060+
['id' => 2, 'name' => 'Another registry'],
3061+
];
3062+
3063+
$api = $this->getApiMock();
3064+
$api->expects($this->once())
3065+
->method('get')
3066+
->with('projects/123/registry/repositories')
3067+
->will($this->returnValue($expectedArray));
3068+
3069+
$this->assertEquals($expectedArray, $api->registryRepositories(123));
3070+
}
3071+
3072+
/**
3073+
* @test
3074+
*/
3075+
public function shouldGetProjectRegistryRepositoriesTags(): void
3076+
{
3077+
$expectedArray = [
3078+
['id' => 1, 'name' => 'A registry', 'tags' => ['1.0', '1.1'], 'tags_count' => 2],
3079+
['id' => 2, 'name' => 'Another registry', 'tags' => ['2.0', '2.1'], 'tags_count' => 2]
3080+
];
3081+
3082+
$api = $this->getApiMock();
3083+
$api->expects($this->once())
3084+
->method('get')
3085+
->with('projects/123/registry/repositories', [
3086+
'tags' => true,
3087+
'tags_count' => true
3088+
])
3089+
->will($this->returnValue($expectedArray));
3090+
3091+
$this->assertEquals($expectedArray, $api->registryRepositories(123, ['tags' => true, 'tags_count' => true]));
3092+
}
30523093
}

tests/Api/RegistryTest.php

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
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\Tests\Api;
16+
17+
use Gitlab\Api\Registry;
18+
19+
class RegistryTest extends TestCase
20+
{
21+
protected function getApiClass(): string
22+
{
23+
return Registry::class;
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function shouldGetSingleRepository(): void
30+
{
31+
$expectedArray = ['id' => 1, 'name' => 'John Doe'];
32+
33+
$api = $this->getApiMock();
34+
$api->expects($this->once())
35+
->method('get')
36+
->with('registry/repositories/1')
37+
->willReturn($expectedArray);
38+
39+
$this->assertEquals($expectedArray, $api->repositories(1));
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function shouldGetSingleRepositoryWithParams(): void
46+
{
47+
$expectedArray = ['id' => 1, 'name' => 'John Doe', 'tags' => ['tag1', 'tag2'], 'tags_count' => 2];
48+
49+
$api = $this->getApiMock();
50+
$api->expects($this->once())
51+
->method('get')
52+
->with('registry/repositories/1', ['tags' => 'true', 'tags_count' => 'true'])
53+
->willReturn($expectedArray);
54+
55+
$this->assertEquals($expectedArray, $api->repositories(1, ['tags' => true, 'tags_count' => true]));
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function shouldGetRepositoryTags(): void
62+
{
63+
$expectedArray = [['name' => "A", 'path' => 'group/project:A'], ['name' => "B", 'path' => 'group/project:B']];
64+
65+
$api = $this->getApiMock();
66+
$api->expects($this->once())
67+
->method('get')
68+
->with('projects/1/registry/repositories/1/tags')
69+
->willReturn($expectedArray);
70+
71+
$this->assertEquals($expectedArray, $api->repositoryTags(1, 1));
72+
}
73+
74+
75+
/**
76+
* @test
77+
*/
78+
public function shouldGetRepositoryTag(): void
79+
{
80+
$expectedArray = ['name' => "A", 'path' => 'group/project:A'];
81+
82+
$api = $this->getApiMock();
83+
$api->expects($this->once())
84+
->method('get')
85+
->with('projects/1/registry/repositories/1/tags/A')
86+
->willReturn($expectedArray);
87+
88+
$this->assertEquals($expectedArray, $api->repositoryTag(1, 1, 'A'));
89+
}
90+
91+
/**
92+
* @test
93+
*/
94+
public function shouldRemoveRepositoryTag(): void
95+
{
96+
$expectedBool = true;
97+
98+
$api = $this->getApiMock();
99+
$api->expects($this->once())
100+
->method('delete')
101+
->with('projects/1/registry/repositories/1/tags/A')
102+
->will($this->returnValue($expectedBool));
103+
104+
$this->assertEquals($expectedBool, $api->removeRepositoryTag(1, 1, 'A'));
105+
}
106+
107+
/**
108+
* @test
109+
*/
110+
public function shouldRemoveRepositoryTags(): void
111+
{
112+
$expectedBool = true;
113+
114+
$api = $this->getApiMock();
115+
$api->expects($this->once())
116+
->method('delete')
117+
->with('projects/1/registry/repositories/1/tags', ['name_regex_delete' => '.*', 'keep_n' => 12])
118+
->will($this->returnValue($expectedBool));
119+
120+
$this->assertEquals($expectedBool, $api->removeRepositoryTags(1, 1, ['name_regex_delete' => '.*', 'keep_n' => 12]));
121+
}
122+
}

0 commit comments

Comments
 (0)