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

Adding support in artifactory API to get package lead file #27

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 25 additions & 3 deletions evidence/create_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jfrog/jfrog-client-go/metadata"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"strings"
)

const leadArtifactQueryTemplate = `{
Expand Down Expand Up @@ -65,11 +66,11 @@ func (c *createEvidencePackage) Run() error {
return err
}

leadArtifact, err := c.getPackageVersionLeadArtifact(packageType, metadataClient)
leadArtifactPath, err := c.getPackageVersionLeadArtifact(packageType, metadataClient, artifactoryClient)
if err != nil {
return err
}
leadArtifactPath := c.buildLeadArtifactPath(leadArtifact)

leadArtifactChecksum, err := c.getFileChecksum(leadArtifactPath, artifactoryClient)
if err != nil {
return err
Expand All @@ -95,7 +96,28 @@ func (c *createEvidencePackage) getPackageType(artifactoryClient artifactory.Art
return request.PackageType, nil
}

func (c *createEvidencePackage) getPackageVersionLeadArtifact(packageType string, metadataClient metadata.Manager) (string, error) {
func (c *createEvidencePackage) getPackageVersionLeadArtifact(packageType string, metadataClient metadata.Manager, artifactoryClient artifactory.ArtifactoryServicesManager) (string, error) {
leadFileRequest := services.LeadFileParams{
PackageType: strings.ToUpper(packageType),
PackageRepoName: c.packageRepoName,
PackageName: c.packageName,
PackageVersion: c.packageVersion,
}

leadArtifact, err := artifactoryClient.GetPackageLeadFile(leadFileRequest)
if err != nil {
leadArtifactPath, err := c.getPackageVersionLeadArtifactFromMetaData(packageType, metadataClient)
if err != nil {
return "", err
}
return c.buildLeadArtifactPath(leadArtifactPath), nil
}
leadArtifactPath := strings.ReplaceAll(string(leadArtifact), ":", "/")

return leadArtifactPath, nil
}

func (c *createEvidencePackage) getPackageVersionLeadArtifactFromMetaData(packageType string, metadataClient metadata.Manager) (string, error) {
body, err := metadataClient.GraphqlQuery(c.createQuery(packageType))
if err != nil {
return "", err
Expand Down
87 changes: 82 additions & 5 deletions evidence/create_package_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package evidence

import (
"fmt"
"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/metadata"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,10 +25,33 @@ func (m *mockMetadataServiceManagerGoodResponse) GraphqlQuery(_ []byte) ([]byte,
return []byte(response), nil
}

func TestPackage(t *testing.T) {
type mockMetadataServiceManagerBadResponse struct{}

func (m *mockMetadataServiceManagerBadResponse) GraphqlQuery(_ []byte) ([]byte, error) {
return nil, fmt.Errorf("HTTP %d: Not Found", http.StatusNotFound)
}

type mockArtifactoryServicesManagerGoodResponse struct {
artifactory.EmptyArtifactoryServicesManager
}

func (m *mockArtifactoryServicesManagerGoodResponse) GetPackageLeadFile(services.LeadFileParams) ([]byte, error) {
return []byte("docker-local/MyLibrary/1.0.0/test.1.0.0.docker"), nil
}

type mockArtifactoryServicesManagerBadResponse struct {
artifactory.EmptyArtifactoryServicesManager
}

func (m *mockArtifactoryServicesManagerBadResponse) GetPackageLeadFile(services.LeadFileParams) ([]byte, error) {
return nil, fmt.Errorf("HTTP %d: Not Found", http.StatusNotFound)
}

func TestGetLeadFileFromMetadataService(t *testing.T) {
tests := []struct {
name string
metadataClientMock metadata.Manager
artifactoryClientMock *mockArtifactoryServicesManagerBadResponse
packageName string
packageVersion string
repoName string
Expand All @@ -33,8 +60,9 @@ func TestPackage(t *testing.T) {
expectError bool
}{
{
name: "Get lead artifact successfully",
name: "Get lead artifact successfully from metadata service",
metadataClientMock: &mockMetadataServiceManagerGoodResponse{},
artifactoryClientMock: &mockArtifactoryServicesManagerBadResponse{},
packageName: "test",
packageVersion: "1.0.0",
repoName: "nuget-local",
Expand All @@ -45,6 +73,7 @@ func TestPackage(t *testing.T) {
{
name: "Duplicate package name and version in the same repository",
metadataClientMock: &mockMetadataServiceManagerDuplicateRepositories{},
artifactoryClientMock: &mockArtifactoryServicesManagerBadResponse{},
packageName: "test",
packageVersion: "1.0.0",
repoName: "nuget-local",
Expand All @@ -61,15 +90,63 @@ func TestPackage(t *testing.T) {
packageVersion: tt.packageVersion,
packageRepoName: tt.repoName,
}
leadArtifact, err := c.getPackageVersionLeadArtifact(tt.packageType, tt.metadataClientMock)
leadArtifactPath := c.buildLeadArtifactPath(leadArtifact)
leadArtifactPath, err := c.getPackageVersionLeadArtifact(tt.packageType, tt.metadataClientMock, tt.artifactoryClientMock)

if tt.expectError {
assert.Error(t, err)
assert.Empty(t, leadArtifact)
assert.Empty(t, leadArtifactPath)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedLeadArtifactPath, leadArtifactPath)
}
})
}
}

func TestGetLeadArtifactFromArtifactoryServiceSuccess(t *testing.T) {
metadataClientMock := &mockMetadataServiceManagerGoodResponse{}
artifactoryClientMock := &mockArtifactoryServicesManagerGoodResponse{}

c := &createEvidencePackage{
packageName: "test",
packageVersion: "1.0.0",
packageRepoName: "nuget-local",
}

leadArtifactPath, err := c.getPackageVersionLeadArtifact("nuget", metadataClientMock, artifactoryClientMock)

assert.NoError(t, err)
assert.Equal(t, "docker-local/MyLibrary/1.0.0/test.1.0.0.docker", leadArtifactPath)
}

func TestGetLeadFileFromArtifactFailsFromMetadataSuccess(t *testing.T) {
metadataClientMock := &mockMetadataServiceManagerGoodResponse{}
artifactoryClientMock := &mockArtifactoryServicesManagerBadResponse{}

c := &createEvidencePackage{
packageName: "test",
packageVersion: "1.0.0",
packageRepoName: "nuget-local",
}

leadArtifactPath, err := c.getPackageVersionLeadArtifact("nuget", metadataClientMock, artifactoryClientMock)

assert.NoError(t, err)
assert.Equal(t, "nuget-local/MyLibrary/1.0.0/test.1.0.0.nupkg", leadArtifactPath)
}

func TestGetLeadArtifactFailsBothServices(t *testing.T) {
metadataClientMock := &mockMetadataServiceManagerBadResponse{}
artifactoryClientMock := &mockArtifactoryServicesManagerBadResponse{}

c := &createEvidencePackage{
packageName: "test",
packageVersion: "1.0.0",
packageRepoName: "nuget-local",
}

leadArtifactPath, err := c.getPackageVersionLeadArtifact("nuget", metadataClientMock, artifactoryClientMock)

assert.Error(t, err)
assert.Empty(t, leadArtifactPath)
}
43 changes: 21 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
module github.com/jfrog/jfrog-cli-artifactory

go 1.22.3
go 1.23.3

require (
github.com/jfrog/build-info-go v1.9.32
github.com/jfrog/gofrog v1.7.5
github.com/jfrog/jfrog-cli-core/v2 v2.54.2
github.com/jfrog/jfrog-client-go v1.43.2
github.com/jfrog/build-info-go v1.10.5
github.com/jfrog/gofrog v1.7.6
github.com/jfrog/jfrog-cli-core/v2 v2.56.8
github.com/jfrog/jfrog-client-go v1.48.0
github.com/pkg/errors v0.9.1
github.com/secure-systems-lab/go-securesystemslib v0.8.0
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.15
github.com/urfave/cli v1.22.16
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.26.0
golang.org/x/crypto v0.29.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/CycloneDX/cyclonedx-go v0.9.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/ProtonMail/go-crypto v1.1.2 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/c-bata/go-prompt v0.2.5 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dsnet/compress v0.0.1 // indirect
Expand All @@ -36,14 +36,14 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jedib0t/go-pretty/v6 v6.5.9 // indirect
github.com/jedib0t/go-pretty/v6 v6.6.1 // indirect
github.com/jfrog/archiver/v3 v3.6.1 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
Expand Down Expand Up @@ -82,19 +82,18 @@ require (
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.26.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240811150357-12a9330a2d67

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240811142930-ab9715567376
//replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240811150357-12a9330a2d67
//replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240811142930-ab9715567376
Loading