Skip to content

Commit

Permalink
feat(referrers): support referrers API (#180)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Pan <[email protected]>
  • Loading branch information
pat-pan authored Feb 27, 2025
1 parent fab26f8 commit e3cefca
Show file tree
Hide file tree
Showing 13 changed files with 1,220 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace OrasProject.Oras.Exceptions;

/// <summary>
/// InvalidReferenceException is thrown when the reference is invlid
/// InvalidReferenceException is thrown when the reference is invalid.
/// </summary>
public class InvalidReferenceException : FormatException
{
Expand Down
36 changes: 36 additions & 0 deletions src/OrasProject.Oras/Exceptions/InvalidResponseException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;

namespace OrasProject.Oras.Exceptions;

/// <summary>
/// InvalidResponseException is thrown when the response is invalid.
/// </summary>
public class InvalidResponseException : FormatException
{
public InvalidResponseException()
{
}

public InvalidResponseException(string? message)
: base(message)
{
}

public InvalidResponseException(string? message, Exception? inner)
: base(message, inner)
{
}
}
2 changes: 1 addition & 1 deletion src/OrasProject.Oras/Registry/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static bool TryParse(string reference, [NotNullWhen(true)] out Reference?
return false;
}
}

public Reference(Reference other)
{
if (other == null)
Expand Down
32 changes: 1 addition & 31 deletions src/OrasProject.Oras/Registry/Remote/ManifestStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Oci;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -274,7 +272,7 @@ private async Task UpdateReferrersIndex(Descriptor subject,
{
// 1. pull the original referrers index list using referrers tag schema
var referrersTag = Referrers.BuildReferrersTag(subject);
var (oldDesc, oldReferrers) = await PullReferrersIndexList(referrersTag, cancellationToken).ConfigureAwait(false);
var (oldDesc, oldReferrers) = await Repository.PullReferrersIndexList(referrersTag, cancellationToken).ConfigureAwait(false);

// 2. apply the referrer change to referrers list
var (updatedReferrers, updateRequired) =
Expand Down Expand Up @@ -309,34 +307,6 @@ private async Task UpdateReferrersIndex(Descriptor subject,
await DeleteAsync(oldDesc, cancellationToken).ConfigureAwait(false);

Check warning on line 307 in src/OrasProject.Oras/Registry/Remote/ManifestStore.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Possible null reference argument for parameter 'target' in 'Task ManifestStore.DeleteAsync(Descriptor target, CancellationToken cancellationToken = default(CancellationToken))'.

Check warning on line 307 in src/OrasProject.Oras/Registry/Remote/ManifestStore.cs

View workflow job for this annotation

GitHub Actions / Analyze (8.0.x)

Possible null reference argument for parameter 'target' in 'Task ManifestStore.DeleteAsync(Descriptor target, CancellationToken cancellationToken = default(CancellationToken))'.

Check warning on line 307 in src/OrasProject.Oras/Registry/Remote/ManifestStore.cs

View workflow job for this annotation

GitHub Actions / Analyze (8.0.x)

Possible null reference argument for parameter 'target' in 'Task ManifestStore.DeleteAsync(Descriptor target, CancellationToken cancellationToken = default(CancellationToken))'.
}

/// <summary>
/// PullReferrersIndexList retrieves the referrers index list associated with the given referrers tag.
/// It fetches the index manifest from the repository, deserializes it into an `Index` object,
/// and returns the descriptor along with the list of manifests (referrers). If the referrers index is not found,
/// an empty descriptor and an empty list are returned.
/// </summary>
/// <param name="referrersTag"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
internal async Task<(Descriptor?, IList<Descriptor>)> PullReferrersIndexList(String referrersTag, CancellationToken cancellationToken = default)
{
try
{
var (desc, content) = await FetchAsync(referrersTag, cancellationToken).ConfigureAwait(false);
var index = JsonSerializer.Deserialize<Index>(content);
if (index == null)
{
throw new JsonException($"null index manifests list when pulling referrers index list for referrers tag {referrersTag}");
}
return (desc, index.Manifests);
}
catch (NotFoundException)
{
return (null, ImmutableArray<Descriptor>.Empty);
}
}


/// <summary>
/// Pushes the manifest content, matching the expected descriptor.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions src/OrasProject.Oras/Registry/Remote/Referrers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

using System.Collections.Generic;
using System.Linq;
using OrasProject.Oras.Content;
using OrasProject.Oras.Oci;

Expand Down Expand Up @@ -105,4 +106,40 @@ internal static (IList<Descriptor>, bool) ApplyReferrerChanges(IList<Descriptor>

return (updatedReferrers, updateRequired);
}

/// <summary>
/// IsReferrersFilterApplied checks if requstedFilter is in the applied filters list.
/// </summary>
/// <param name="appliedFilters"></param>
/// <param name="requestedFilter"></param>
/// <returns></returns>
internal static bool IsReferrersFilterApplied(string? appliedFilters, string requestedFilter) {
if (string.IsNullOrEmpty(appliedFilters) || string.IsNullOrEmpty(requestedFilter))
{
return false;
}

var filters = appliedFilters.Split(",");
foreach (var filter in filters)
{
if (filter == requestedFilter)
{
return true;
}
}

return false;
}

/// <summary>
/// FilterReferrers filters out a list of referrers based on the specified artifact type
/// </summary>
/// <param name="referrers"></param>
/// <param name="artifactType"></param>
/// <returns></returns>
internal static IList<Descriptor> FilterReferrers(IList<Descriptor> referrers, string? artifactType)
{
return string.IsNullOrEmpty(artifactType) ? referrers : referrers.Where(referrer => referrer.ArtifactType == artifactType).ToList();
}
}

Loading

0 comments on commit e3cefca

Please sign in to comment.