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

added filtering by artifact type with regex #258

Merged
merged 16 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
75 changes: 75 additions & 0 deletions extendedcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ package oras

import (
"context"
"encoding/json"
"errors"
"regexp"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/internal/copyutil"
"oras.land/oras-go/v2/internal/descriptor"
"oras.land/oras-go/v2/registry"
)

var (
Expand Down Expand Up @@ -196,3 +199,75 @@ func findRoots(ctx context.Context, storage content.GraphStorage, node ocispec.D
}
return roots, nil
}

// FilterArtifactType will configure opts.FindPredecessors to filter the predecessors
// whose artifact type matches a given regex pattern.
func (opts *ExtendedCopyGraphOptions) FilterArtifactType(regex *regexp.Regexp) {
fp := opts.FindPredecessors
opts.FindPredecessors = func(ctx context.Context, src content.GraphStorage, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
var predecessors []ocispec.Descriptor
var err error
if fp == nil {
// if src is a ReferrerFinder, use Referrers() to filter the predecessors.
rf, ok := src.(registry.ReferrerFinder)
if ok {
predecessors, err = findReferrersAndFilter(rf, ctx, desc, regex)
if err != nil {
return predecessors, nil
}
} else {
predecessors, err = src.Predecessors(ctx, desc)
}
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
} else {
predecessors, err = fp(ctx, src, desc)
}
if err != nil {
return nil, err
}
var filtered []ocispec.Descriptor
// for each predecessor, decode the manifest and check its artifact type.
for _, p := range predecessors {
if p.MediaType == artifactspec.MediaTypeArtifactManifest {
if err = func() error {
rc, err := src.Fetch(ctx, p)
if err != nil {
return err
}
defer rc.Close()
var manifest artifactspec.Manifest
if err := json.NewDecoder(rc).Decode(&manifest); err != nil {
return err
}
if regex.MatchString(manifest.ArtifactType) {
filtered = append(filtered, p)
}
return nil
}(); err != nil {
return nil, err
}
}
}
return filtered, nil
}
}

// findReferrersAndFilter filters the predecessors with Referrers.
func findReferrersAndFilter(rf registry.ReferrerFinder, ctx context.Context, desc ocispec.Descriptor, regex *regexp.Regexp) ([]ocispec.Descriptor, error) {
var predecessors []ocispec.Descriptor
if err := rf.Referrers(ctx, desc, "", func(referrers []artifactspec.Descriptor) error {
// for each page of the results, do the following:
for _, referrer := range referrers {
if regex.MatchString(referrer.ArtifactType) {
predecessors = append(predecessors, ocispec.Descriptor{
MediaType: referrer.MediaType,
Digest: referrer.Digest,
Size: referrer.Size,
})
}
}
return nil
}); err != nil {
return nil, err
}
return predecessors, nil
}
Loading