remove duplicate predicate filtering code
Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
parent
a9cc7b481e
commit
a856a796f0
4 changed files with 15 additions and 21 deletions
|
|
@ -144,10 +144,9 @@ func runDownload(opts *Options) error {
|
|||
|
||||
// Apply predicate type filter to returned attestations
|
||||
if opts.PredicateType != "" {
|
||||
filteredAttestations := verification.FilterAttestations(opts.PredicateType, attestations)
|
||||
|
||||
if len(filteredAttestations) == 0 {
|
||||
return fmt.Errorf("no attestations found with predicate type: %s", opts.PredicateType)
|
||||
filteredAttestations, err := verification.FilterAttestations(opts.PredicateType, attestations)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to filter attestations: %v", err)
|
||||
}
|
||||
|
||||
attestations = filteredAttestations
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ type IntotoStatement struct {
|
|||
PredicateType string `json:"predicateType"`
|
||||
}
|
||||
|
||||
func FilterAttestations(predicateType string, attestations []*api.Attestation) []*api.Attestation {
|
||||
func FilterAttestations(predicateType string, attestations []*api.Attestation) ([]*api.Attestation, error) {
|
||||
filteredAttestations := []*api.Attestation{}
|
||||
|
||||
for _, each := range attestations {
|
||||
|
|
@ -118,5 +118,9 @@ func FilterAttestations(predicateType string, attestations []*api.Attestation) [
|
|||
}
|
||||
}
|
||||
|
||||
return filteredAttestations
|
||||
if len(filteredAttestations) == 0 {
|
||||
return nil, fmt.Errorf("no attestations found with predicate type: %s", predicateType)
|
||||
}
|
||||
|
||||
return filteredAttestations, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -157,10 +157,11 @@ func TestFilterAttestations(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
filtered := FilterAttestations("https://slsa.dev/provenance/v1", attestations)
|
||||
|
||||
filtered, err := FilterAttestations("https://slsa.dev/provenance/v1", attestations)
|
||||
require.Len(t, filtered, 1)
|
||||
require.NoError(t, err)
|
||||
|
||||
filtered = FilterAttestations("NonExistentPredicate", attestations)
|
||||
filtered, err = FilterAttestations("NonExistentPredicate", attestations)
|
||||
require.Len(t, filtered, 0)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,16 +9,6 @@ import (
|
|||
"github.com/cli/cli/v2/pkg/cmd/attestation/verification"
|
||||
)
|
||||
|
||||
func filterByPredicateType(predicateType string, attestations []*api.Attestation) ([]*api.Attestation, string, error) {
|
||||
// Apply predicate type filter to returned attestations
|
||||
filteredAttestations := verification.FilterAttestations(predicateType, attestations)
|
||||
if len(filteredAttestations) == 0 {
|
||||
msg := fmt.Sprintf("✗ No attestations found with predicate type: %s\n", predicateType)
|
||||
return nil, msg, fmt.Errorf("no matching predicate found")
|
||||
}
|
||||
return filteredAttestations, "", nil
|
||||
}
|
||||
|
||||
func getAttestations(o *Options, a artifact.DigestedArtifact) ([]*api.Attestation, string, error) {
|
||||
if o.FetchAttestationsFromGitHubAPI() {
|
||||
params := api.FetchParams{
|
||||
|
|
@ -58,9 +48,9 @@ func getAttestations(o *Options, a artifact.DigestedArtifact) ([]*api.Attestatio
|
|||
return nil, errMsg, err
|
||||
}
|
||||
|
||||
filtered, errMsg, err := filterByPredicateType(o.PredicateType, attestations)
|
||||
filtered, err := verification.FilterAttestations(o.PredicateType, attestations)
|
||||
if err != nil {
|
||||
return nil, errMsg, err
|
||||
return nil, err.Error(), err
|
||||
}
|
||||
|
||||
pluralAttestation := text.Pluralize(len(filtered), "attestation")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue