Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions src/Api/RegistryRepositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Jan-Hendrik Willms <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gitlab\Api;

class RegistryRepositories extends AbstractApi
{
public const ACCESS_LEVEL_ENABLED = 'enabled';
public const ACCESS_LEVEL_PRIVATE = 'private';
public const ACCESS_LEVEL_DISABLED = 'disabled';

/**
* @param string|int $project_id
* @param array $parameters
*
* @var bool $tags include tags
* @var bool $tags_count include tags count
*
* @return mixed
*/
public function all($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();

$resolver->setDefined('tags')
->setAllowedTypes('tags', ['bool']);

$resolver->setDefined('tags_count')
->setAllowedTypes('tags_count', ['bool']);

return $this->get(
$this->getProjectPath($project_id, 'registry/repositories'),
$resolver->resolve($parameters)
);
}

/**
* @param string|int $project_id
* @param array $parameters
*
* @var string $container_registry_access_level access level to set (enabled, private or disabled)
*
* @return mixed
*/
public function changeContainerRegistryVisibility($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();

$resolver->setDefined('container_registry_access_level')
->setAllowedValues('container_registry_access_level', [
self::ACCESS_LEVEL_ENABLED,
self::ACCESS_LEVEL_PRIVATE,
self::ACCESS_LEVEL_DISABLED,
]);

return $this->put(
$this->getProjectPath($project_id, ''),
$resolver->resolve($parameters)
);
}

/**
* @param string|int $registry_repository_id
* @param array $parameters
*
* @var bool $tags include tags
* @var bool $tags_count include tags count
* @var bool $size include size
*
* @return mixed
*/
public function show($registry_repository_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();

$resolver->setDefined('tags')
->setAllowedTypes('tags', ['bool']);

$resolver->setDefined('tags_count')
->setAllowedTypes('tags_count', ['bool']);

$resolver->setDefined('size')
->setAllowedTypes('size', ['bool']);

return $this->get(
'registry/repositories/'.self::encodePath($registry_repository_id),
$resolver->resolve($parameters)
);
}

/**
* @param string|int $project_id
* @param string|int $registry_repository_id
*
* @return mixed
*/
public function remove($project_id, $registry_repository_id)
{
return $this->delete(
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($registry_repository_id))
);
}

/**
* @param string|int $project_id
* @param string|int $registry_repository_id
*
* @return mixed
*/
public function tags($project_id, $registry_repository_id)
{
return $this->get(
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($registry_repository_id).'/tags')
);
}

/**
* @param string|int $project_id
* @param string|int $registry_repository_id
* @param string $tag_name
*
* @return mixed
*/
public function tag($project_id, $registry_repository_id, $tag_name)
{
return $this->get(
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($registry_repository_id).'/tags/'.self::encodePath($tag_name))
);
}

/**
* @param string|int $project_id
* @param string|int $registry_repository_id
* @param string $tag_name
*
* @return mixed
*/
public function removeTag($project_id, $registry_repository_id, $tag_name)
{
return $this->delete(
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($registry_repository_id).'/tags/'.self::encodePath($tag_name))
);
}

/**
* @param string|int $project_id
* @param string|int $registry_repository_id
* @param array $parameters
*
* @var string $name_regex_delete delete all tags matching this regex
* @var string $name_regex_keep keep all tags matching this regex
* @var int $keep_n keep n latest tags for each matching tag
* @var string $older_than delete tags older than the given time (written in human readable form 1h, 1d, 1month)
*
* @return mixed
*/
public function removeTags($project_id, $registry_repository_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();

$resolver->setDefined('name_regex_delete')
->setRequired('name_regex_delete')
->setAllowedTypes('name_regex_delete', 'string');

$resolver->setDefined('name_regex_keep')
->setAllowedTypes('name_regex_keep', 'string');

$resolver->setDefined('keep_n')
->setAllowedTypes('keep_n', 'int');

$resolver->setDefined('older_than')
->setAllowedTypes('older_than', 'string');

return $this->delete(
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($registry_repository_id).'/tags'),
$resolver->resolve($parameters)
);
}
}
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Gitlab\Api\Milestones;
use Gitlab\Api\ProjectNamespaces;
use Gitlab\Api\Projects;
use Gitlab\Api\RegistryRepositories;
use Gitlab\Api\Repositories;
use Gitlab\Api\RepositoryFiles;
use Gitlab\Api\ResourceIterationEvents;
Expand Down Expand Up @@ -336,6 +337,14 @@ public function projects(): Projects
return new Projects($this);
}

/**
* @return RegistryRepositories
*/
public function registryRepositories(): RegistryRepositories
{
return new RegistryRepositories($this);
}

/**
* @return Repositories
*/
Expand Down
164 changes: 164 additions & 0 deletions tests/Api/RegistryRepositoriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Jan-Hendrik Willms <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gitlab\Tests\Api;

use Gitlab\Api\RegistryRepositories;

class RegistryRepositoriesTest extends TestCase
{
protected function getApiClass()
{
return RegistryRepositories::class;
}

/**
* @test
*/
public function shouldChangeContainerRegistryVisibility(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/', ['container_registry_access_level' => 'private'])
;

$api->changeContainerRegistryVisibility(1, ['container_registry_access_level' => 'private']);
}

/**
* @test
*/
public function shouldGetAllRegistryRepositories(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/registry/repositories')
;

$api->all(1);
}

/**
* @test
*/
public function shouldGetAllRegistryRepositoriesWithParams(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/registry/repositories', ['tags' => true, 'tags_count' => true])
;

$api->all(1, ['tags' => true, 'tags_count' => true]);
}

/**
* @test
*/
public function shouldGetRegistryRepository(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('registry/repositories/1')
;

$api->show(1);
}

/**
* @test
*/
public function shouldGetRegistryRepositoryWithParams(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('registry/repositories/1', ['tags' => true, 'tags_count' => true, 'size' => true])
;

$api->show(1, ['tags' => true, 'tags_count' => true, 'size' => true]);
}

/**
* @test
*/
public function shouldRemoveRegistryRepository(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('projects/1/registry/repositories/2')
;

$api->remove(1, 2);
}

/**
* @test
*/
public function shouldGetAllRegistryRepositoryTags(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/registry/repositories/2/tags')
;

$api->tags(1, 2);
}

/**
* @test
*/
public function shouldGetRegistryRepositoryTag(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/registry/repositories/2/tags/foo')
;

$api->tag(1, 2, 'foo');
}

/**
* @test
*/
public function shouldRemoveRegistryRepositoryTag(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('projects/1/registry/repositories/2/tags/foo')
;

$api->removeTag(1, 2, 'foo');
}

/**
* @test
*/
public function shouldRemoveRegistryRepositoryTags(): void
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('projects/1/registry/repositories/2/tags', ['name_regex_delete' => 'test-.+', 'name_regex_keep' => 'test-foo', 'keep_n' => 1, 'older_than' => '1month'])
;

$api->removeTags(1, 2, ['name_regex_delete' => 'test-.+', 'name_regex_keep' => 'test-foo', 'keep_n' => 1, 'older_than' => '1month']);
}
}