Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Container registry API #814

Open
wants to merge 5 commits into
base: 11.14
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/Api/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,18 @@ public function search($id, array $parameters = [])

return $this->get('groups/'.self::encodePath($id).'/search', $resolver->resolve($parameters));
}

/**
* Get a list of registry repositories in a group.
*
* @see https://docs.gitlab.com/ee/api/container_registry.html#within-a-group
*
* @param $id: The ID of a group
*
* @return mixed
*/
public function registryRepositories(int $id)
{
return $this->get('groups/'.self::encodePath($id).'/registry/repositories');
}
}
29 changes: 29 additions & 0 deletions src/Api/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -1832,4 +1832,33 @@ public function search($id, array $parameters = [])

return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters));
}

/**
* @see https://docs.gitlab.com/ee/api/container_registry.html#within-a-project
*
* @param int|string $project_id
* @param array $parameters {
*
* @var bool $tags if the parameter is included as true, each repository includes an array of "tags" in the response
* @var bool $tags_count If the parameter is included as true, each repository includes "tags_count" in the response.
* }
*
* @return mixed
*/
public function registryRepositories($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function (Options $resolver, $value): string {
return $value ? 'true' : 'false';
};

$resolver->setDefined('tags')
->setAllowedTypes('tags', 'bool')
->setNormalizer('tags', $booleanNormalizer);
$resolver->setDefined('tags_count')
->setAllowedTypes('tags_count', 'bool')
->setNormalizer('tags_count', $booleanNormalizer);

return $this->get($this->getProjectPath($project_id, 'registry/repositories'), $resolver->resolve($parameters));
}
}
142 changes: 142 additions & 0 deletions src/Api/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

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

namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;

class Registry extends AbstractApi
{
/**
* @see https://docs.gitlab.com/ee/api/container_registry.html#get-details-of-a-single-repository
*
* @param int|string $repository_id The ID of the registry repository accessible by the authenticated user
* @param array $parameters {
*
* @var bool $tags
* @var bool $tags_count
* @var bool $size
* }
*
* @return mixed
*/
public function repositories($repository_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function (Options $resolver, $value): string {
return $value ? 'true' : 'false';
};

$resolver->setDefined('tags')
->setAllowedTypes('tags', 'bool')
->setNormalizer('tags', $booleanNormalizer);
$resolver->setDefined('tags_count')
->setAllowedTypes('tags_count', 'bool')
->setNormalizer('tags_count', $booleanNormalizer);
$resolver->setDefined('size')
->setAllowedTypes('size', 'bool')
->setNormalizer('size', $booleanNormalizer);

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

/**
* @see https://docs.gitlab.com/ee/api/container_registry.html#list-registry-repository-tags
*
* @param int|string $project_id
* @param int $repository_id
*
* @return mixed
*/
public function repositoryTags($project_id, int $repository_id)
{
return $this->get(
$this->getProjectPath($project_id, 'registry/repositories/'.self::encodePath($repository_id).'/tags')
);
}

/**
* @see https://docs.gitlab.com/ee/api/container_registry.html#get-details-of-a-registry-repository-tag
*
* @param int|string $project_id
* @param int $repository_id
* @param string $tag_name
*
* @return mixed
*/
public function repositoryTag($project_id, int $repository_id, string $tag_name)
{
return $this->get(
$this->getProjectPath(
$project_id,
'registry/repositories/'.self::encodePath($repository_id).'/tags/'.self::encodePath($tag_name)
)
);
}

/**
* @see https://docs.gitlab.com/ee/api/container_registry.html#delete-a-registry-repository-tag
*
* @param int|string $project_id
* @param int $repository_id
* @param string $tag_name
*
* @return mixed
*/
public function removeRepositoryTag($project_id, int $repository_id, string $tag_name)
{
return $this->delete(
$this->getProjectPath(
$project_id,
'registry/repositories/'.self::encodePath($repository_id).'/tags/'.self::encodePath($tag_name)
)
);
}

/**
* @see https://docs.gitlab.com/ee/api/container_registry.html#delete-registry-repository-tags-in-bulk
*
* @param int|string $project_id
* @param int $repository_id
* @param array $parameters {
*
* @var string $name_regex_delete
* @var string $name_regex_keep
* @var int $keep_n
* @var string $older_than
* }
*
* @return mixed
*/
public function removeRepositoryTags($project_id, int $repository_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$resolver->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($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\Registry;
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 Registry
*/
public function registry(): Registry
{
return new Registry($this);
}

/**
* @return Repositories
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Api/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,4 +972,24 @@ public function shouldSearchGroups(): void
'sort' => 'desc',
]));
}

/**
* @test
*/
public function shouldGetGroupRegistryRepositories(): void
{
$expectedArray = [
['id' => 1, 'name' => 'A registry'],
['id' => 2, 'name' => 'Another registry'],
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('groups/1/registry/repositories')
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->registryRepositories(1));
}
}
41 changes: 41 additions & 0 deletions tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3049,4 +3049,45 @@ public function shouldSearchGroups(): void
'sort' => 'desc',
]));
}

/**
* @test
*/
public function shouldGetProjectRegistryRepositories(): void
{
$expectedArray = [
['id' => 1, 'name' => 'A registry'],
['id' => 2, 'name' => 'Another registry'],
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/123/registry/repositories')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->registryRepositories(123));
}

/**
* @test
*/
public function shouldGetProjectRegistryRepositoriesTags(): void
{
$expectedArray = [
['id' => 1, 'name' => 'A registry', 'tags' => ['1.0', '1.1'], 'tags_count' => 2],
['id' => 2, 'name' => 'Another registry', 'tags' => ['2.0', '2.1'], 'tags_count' => 2],
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/123/registry/repositories', [
'tags' => true,
'tags_count' => true,
])
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->registryRepositories(123, ['tags' => true, 'tags_count' => true]));
}
}
Loading
Loading