diff --git a/pkg/cmd/attestation/download/download.go b/pkg/cmd/attestation/download/download.go index 7cb1e15b9..c3dc8426f 100644 --- a/pkg/cmd/attestation/download/download.go +++ b/pkg/cmd/attestation/download/download.go @@ -2,6 +2,7 @@ package download import ( "encoding/json" + "errors" "fmt" "os" @@ -128,14 +129,13 @@ func runDownload(opts *Options) error { } attestations, err := verification.GetRemoteAttestations(c) if err != nil { + if errors.Is(err, api.ErrNoAttestations{}) { + fmt.Fprintf(opts.Logger.IO.Out, "No attestations found for %s\n", opts.ArtifactPath) + return nil + } return fmt.Errorf("failed to fetch attestations: %w", err) } - if attestations == nil { - fmt.Fprintf(opts.Logger.IO.Out, "No attestations found for %s\n", opts.ArtifactPath) - return nil - } - filePath := createJSONLinesFilePath(artifact.DigestWithAlg(), opts.OutputPath) fmt.Fprintf(opts.Logger.IO.Out, "Writing attestations to file %s.\nAny previous content will be overwritten\n\n", filePath) diff --git a/pkg/cmd/attestation/download/download_test.go b/pkg/cmd/attestation/download/download_test.go index 800403401..5455eb3dd 100644 --- a/pkg/cmd/attestation/download/download_test.go +++ b/pkg/cmd/attestation/download/download_test.go @@ -252,7 +252,7 @@ func TestRunDownload(t *testing.T) { opts := baseOpts opts.APIClient = api.MockClient{ OnGetByOwnerAndDigest: func(repo, digest string, limit int) ([]*api.Attestation, error) { - return nil, nil + return nil, api.ErrNoAttestations{} }, } @@ -264,6 +264,18 @@ func TestRunDownload(t *testing.T) { require.NoFileExists(t, artifact.DigestWithAlg()) }) + t.Run("failed to fetch attestations", func(t *testing.T) { + opts := baseOpts + opts.APIClient = api.MockClient{ + OnGetByOwnerAndDigest: func(repo, digest string, limit int) ([]*api.Attestation, error) { + return nil, fmt.Errorf("failed to fetch attestations") + }, + } + + err := runDownload(&opts) + require.Error(t, err) + }) + t.Run("cannot download OCI artifact", func(t *testing.T) { opts := baseOpts opts.ArtifactPath = "oci://ghcr.io/github/test"