Skip to content

Commit

Permalink
chore: applies suggestions from code review
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Chaia <[email protected]>
  • Loading branch information
leonardochaia committed Nov 11, 2024
1 parent f268d0c commit ef226cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/OrasProject.Oras/Registry/IMounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ public interface IMounter
/// <param name="getContent"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task MountAsync(Descriptor descriptor, string fromRepository, Func<CancellationToken, Task<Stream>>? getContent, CancellationToken cancellationToken);
Task MountAsync(Descriptor descriptor,
string fromRepository,
Func<CancellationToken, Task<Stream>>? getContent = null,
CancellationToken cancellationToken = default);
}
41 changes: 26 additions & 15 deletions src/OrasProject.Oras/Registry/Remote/BlobStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public async Task MountAsync(Descriptor descriptor, string fromRepository,
{
case HttpStatusCode.Created:
// 201, layer has been mounted
response.VerifyContentDigest(descriptor.Digest);
return;
case HttpStatusCode.Accepted:
{
Expand Down Expand Up @@ -235,21 +236,29 @@ public async Task MountAsync(Descriptor descriptor, string fromRepository,
//
// [spec]: https://github.com/opencontainers/distribution-spec/blob/v1.1.0/spec.md#mounting-a-blob-from-another-repository

Stream contents;
if (getContent != null)
async Task<Stream> GetContentStream()
{
contents = await getContent(cancellationToken).ConfigureAwait(false);
}
else
{
var referenceOptions = repository.Options with
Stream stream;
if (getContent != null)
{
stream = await getContent(cancellationToken).ConfigureAwait(false);
}
else
{
Reference = Reference.Parse(fromRepository),
};
contents = await new Repository(referenceOptions).FetchAsync(descriptor, cancellationToken);
var referenceOptions = repository.Options with
{
Reference = Reference.Parse(fromRepository),
};
stream = await new Repository(referenceOptions).FetchAsync(descriptor, cancellationToken).ConfigureAwait(false);
}

return stream;
}

await InternalPushAsync(url, descriptor, contents, cancellationToken).ConfigureAwait(false);
await using (var contents = await GetContentStream())
{
await InternalPushAsync(url, descriptor, contents, cancellationToken).ConfigureAwait(false);
}
}

private async Task InternalPushAsync(Uri url, Descriptor descriptor, Stream content,
Expand All @@ -267,11 +276,13 @@ private async Task InternalPushAsync(Uri url, Descriptor descriptor, Stream cont
// the descriptor media type is ignored as in the API doc.
req.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Octet);

using var response =
await Repository.Options.HttpClient.SendAsync(req, cancellationToken).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.Created)
using (var response =
await Repository.Options.HttpClient.SendAsync(req, cancellationToken).ConfigureAwait(false))
{
throw await response.ParseErrorResponseAsync(cancellationToken).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.Created)
{
throw await response.ParseErrorResponseAsync(cancellationToken).ConfigureAwait(false);

Check warning on line 284 in src/OrasProject.Oras/Registry/Remote/BlobStore.cs

View check run for this annotation

Codecov / codecov/patch

src/OrasProject.Oras/Registry/Remote/BlobStore.cs#L283-L284

Added lines #L283 - L284 were not covered by tests
}
}
}
}
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Registry/Remote/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,6 @@ internal Reference ParseReferenceFromContentReference(string reference)
/// <param name="getContent"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task MountAsync(Descriptor descriptor, string fromRepository, Func<CancellationToken, Task<Stream>>? getContent, CancellationToken cancellationToken)
=> ((IMounter)Blobs).MountAsync(descriptor, fromRepository, getContent, cancellationToken);
public async Task MountAsync(Descriptor descriptor, string fromRepository, Func<CancellationToken, Task<Stream>>? getContent, CancellationToken cancellationToken = default)
=> await ((IMounter)Blobs).MountAsync(descriptor, fromRepository, getContent, cancellationToken).ConfigureAwait(false);
}

0 comments on commit ef226cf

Please sign in to comment.