-
Notifications
You must be signed in to change notification settings - Fork 85
Add container registry api support #1108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Mock.Internals; | ||
| using NGitLab.Models; | ||
| using ModelContainerRegistryTag = NGitLab.Models.ContainerRegistryTag; | ||
| using ModelContainerRepository = NGitLab.Models.ContainerRepository; | ||
|
|
||
| namespace NGitLab.Mock.Clients; | ||
|
|
||
| internal sealed class ContainerRegistryClient(ClientContext context, ProjectId projectId) : ClientBase(context), IContainerRegistryClient | ||
| { | ||
| public GitLabCollectionResponse<ModelContainerRepository> GetRepositoriesAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| using (Context.BeginOperationScope()) | ||
| { | ||
| var project = GetProject(projectId, ProjectPermission.View); | ||
| var result = project.ContainerRepositories | ||
| .Select(r => r.ToClientContainerRepository()) | ||
| .ToList(); | ||
| return GitLabCollectionResponse.Create(result); | ||
| } | ||
| } | ||
|
|
||
| public GitLabCollectionResponse<ModelContainerRegistryTag> GetTagsAsync(long repositoryId, CancellationToken cancellationToken = default) | ||
| { | ||
| using (Context.BeginOperationScope()) | ||
| { | ||
| var project = GetProject(projectId, ProjectPermission.View); | ||
| var repo = project.ContainerRepositories.FirstOrDefault(r => r.Id == repositoryId) | ||
| ?? throw GitLabException.NotFound(); | ||
| var result = repo.Tags | ||
| .Select(t => t.ToClientContainerRegistryTag()) | ||
| .ToList(); | ||
| return GitLabCollectionResponse.Create(result); | ||
| } | ||
| } | ||
|
|
||
| public Task DeleteTagAsync(long repositoryId, string tagName, CancellationToken cancellationToken = default) | ||
| { | ||
| using (Context.BeginOperationScope()) | ||
| { | ||
| var project = GetProject(projectId, ProjectPermission.Edit); | ||
| var repo = project.ContainerRepositories.FirstOrDefault(r => r.Id == repositoryId) | ||
| ?? throw GitLabException.NotFound(); | ||
| repo.Tags.RemoveAll(t => string.Equals(t.Name, tagName, System.StringComparison.Ordinal)); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| public Task DeleteRepositoryAsync(long repositoryId, CancellationToken cancellationToken = default) | ||
| { | ||
| using (Context.BeginOperationScope()) | ||
| { | ||
| var project = GetProject(projectId, ProjectPermission.Edit); | ||
| var repo = project.ContainerRepositories.FirstOrDefault(r => r.Id == repositoryId) | ||
| ?? throw GitLabException.NotFound(); | ||
| project.ContainerRepositories.Remove(repo); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace NGitLab.Mock.Config; | ||
|
|
||
| /// <summary> | ||
| /// Describe a container repository in a GitLab project | ||
| /// </summary> | ||
| public class GitLabContainerRepository : GitLabObject | ||
| { | ||
| public string Name { get; set; } | ||
|
|
||
| public List<string> Tags { get; } = []; | ||
| } | ||
|
|
||
| public class GitLabContainerRepositoriesCollection : GitLabCollection<GitLabContainerRepository> | ||
| { | ||
| internal GitLabContainerRepositoriesCollection(GitLabProject parent) | ||
| : base(parent) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab.Mock; | ||
|
|
||
| public sealed class ContainerRegistryTagEntry | ||
| { | ||
| public string Name { get; set; } | ||
|
|
||
| public Models.ContainerRegistryTag ToClientContainerRegistryTag() | ||
| { | ||
| return new Models.ContainerRegistryTag | ||
| { | ||
| Name = Name, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab.Mock; | ||
|
|
||
| public sealed class ContainerRepository : GitLabObject | ||
| { | ||
| public new Project Parent => (Project)base.Parent; | ||
|
|
||
| public long Id { get; set; } | ||
|
|
||
| public string Name { get; set; } | ||
|
|
||
| public List<ContainerRegistryTagEntry> Tags { get; } = []; | ||
|
|
||
| public Models.ContainerRepository ToClientContainerRepository() | ||
| { | ||
| return new Models.ContainerRepository | ||
| { | ||
| Id = Id, | ||
| Name = Name, | ||
| ProjectId = Parent?.Id ?? 0, | ||
| TagsCount = Tags.Count, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace NGitLab.Mock; | ||
|
|
||
| public sealed class ContainerRepositoryCollection : Collection<ContainerRepository> | ||
| { | ||
| public ContainerRepositoryCollection(GitLabObject parent) | ||
| : base(parent) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab; | ||
|
|
||
| public interface IContainerRegistryClient | ||
| { | ||
| /// <summary> | ||
| /// Returns all container repositories for the project. | ||
| /// </summary> | ||
| GitLabCollectionResponse<ContainerRepository> GetRepositoriesAsync(CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Returns all tags for the given repository. | ||
| /// </summary> | ||
| GitLabCollectionResponse<ContainerRegistryTag> GetTagsAsync(long repositoryId, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Deletes a single tag from a repository. | ||
| /// </summary> | ||
| Task DeleteTagAsync(long repositoryId, string tagName, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Deletes a repository and all its tags. | ||
| /// </summary> | ||
| Task DeleteRepositoryAsync(long repositoryId, CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Extensions; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab.Impl; | ||
|
|
||
| public class ContainerRegistryClient : IContainerRegistryClient | ||
| { | ||
| private readonly API _api; | ||
| private readonly string _projectId; | ||
|
|
||
| public ContainerRegistryClient(API api, ProjectId projectId) | ||
| { | ||
| _api = api; | ||
| _projectId = projectId.ValueAsUriParameter(); | ||
| } | ||
|
|
||
| public GitLabCollectionResponse<ContainerRepository> GetRepositoriesAsync(CancellationToken cancellationToken = default) | ||
| => _api.Get().GetAllAsync<ContainerRepository>($"/projects/{_projectId}/registry/repositories"); | ||
|
|
||
| public GitLabCollectionResponse<ContainerRegistryTag> GetTagsAsync(long repositoryId, CancellationToken cancellationToken = default) | ||
| => _api.Get().GetAllAsync<ContainerRegistryTag>($"/projects/{_projectId}/registry/repositories/{repositoryId.ToStringInvariant()}/tags"); | ||
|
|
||
| public Task DeleteTagAsync(long repositoryId, string tagName, CancellationToken cancellationToken = default) | ||
| => _api.Delete().ExecuteAsync($"/projects/{_projectId}/registry/repositories/{repositoryId.ToStringInvariant()}/tags/{tagName}", cancellationToken); | ||
|
|
||
| public Task DeleteRepositoryAsync(long repositoryId, CancellationToken cancellationToken = default) | ||
| => _api.Delete().ExecuteAsync($"/projects/{_projectId}/registry/repositories/{repositoryId.ToStringInvariant()}", cancellationToken); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using System; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NGitLab.Models; | ||
|
|
||
| public class ContainerRegistryTag | ||
| { | ||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } | ||
|
|
||
| [JsonPropertyName("path")] | ||
| public string Path { get; set; } | ||
|
|
||
| [JsonPropertyName("location")] | ||
| public string Location { get; set; } | ||
|
|
||
| [JsonPropertyName("revision")] | ||
| public string Revision { get; set; } | ||
|
|
||
| [JsonPropertyName("short_revision")] | ||
| public string ShortRevision { get; set; } | ||
|
|
||
| [JsonPropertyName("digest")] | ||
| public string Digest { get; set; } | ||
|
|
||
| [JsonPropertyName("created_at")] | ||
| public DateTime CreatedAt { get; set; } | ||
|
|
||
| [JsonPropertyName("total_size")] | ||
| public long TotalSize { get; set; } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the possibility to set it in the GitLabHelpers (to construct GitLabConfig), thus, to be able to use the mocked client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed ^^