Skip to content

Commit

Permalink
Adding support in artifactory API to get package lead file (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
oshratZairi authored Nov 14, 2024
1 parent 3f907f0 commit 4435ca5
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 107 deletions.
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

0 comments on commit 4435ca5

Please sign in to comment.