Skip to content

Commit

Permalink
Add Artifactory API to get package artifact lead file (jfrog#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
oshratZairi authored Nov 13, 2024
1 parent f0d689e commit b60e50e
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
- [Getting Info of a File in Artifactory](#getting-info-of-a-file-in-artifactory)
- [Getting a listing of files and folders within a folder in Artifactory](#getting-a-listing-of-files-and-folders-within-a-folder-in-artifactory)
- [Getting Storage Summary Info of Artifactory](#getting-storage-summary-info-of-artifactory)
- [Getting package artifact Lead File](#getting-package-artifact-lead-file)
- [Triggering Storage Info Recalculation in Artifactory](#triggering-storage-info-recalculation-in-artifactory)
- [Access APIs](#access-apis)
- [Creating Access Service Manager](#creating-access-service-manager)
Expand Down Expand Up @@ -1439,6 +1440,12 @@ serviceManager.FileList("repo/path/", optionalParams)
storageInfo, err := serviceManager.GetStorageInfo()
```

#### Getting Package Artifact Lead File

```go
leadArtifact, err := serviceManager.GetPackageLeadFile()
```

#### Triggering Storage Info Recalculation in Artifactory

```go
Expand Down
5 changes: 5 additions & 0 deletions artifactory/emptymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type ArtifactoryServicesManager interface {
GetStorageInfo() (*utils.StorageInfo, error)
CalculateStorageInfo() error
ImportReleaseBundle(string) error
GetPackageLeadFile(leadFileParams services.LeadFileParams) ([]byte, error)
}

// By using this struct, you have the option of overriding only some of the ArtifactoryServicesManager
Expand Down Expand Up @@ -470,6 +471,10 @@ func (esm *EmptyArtifactoryServicesManager) ImportReleaseBundle(string) error {
panic("Failed: Method is not implemented")
}

func (esm *EmptyArtifactoryServicesManager) GetPackageLeadFile(services.LeadFileParams) ([]byte, error) {
panic("Failed: Method is not implemented")
}

// Compile time check of interface implementation.
// Since EmptyArtifactoryServicesManager can be used by tests external to this project, we want this project's tests to fail,
// if EmptyArtifactoryServicesManager stops implementing the ArtifactoryServicesManager interface.
Expand Down
6 changes: 6 additions & 0 deletions artifactory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (sm *ArtifactoryServicesManagerImp) GetRepository(repoKey string, repoDetai
return repositoriesService.Get(repoKey, repoDetails)
}

func (sm *ArtifactoryServicesManagerImp) GetPackageLeadFile(leadFileParams services.LeadFileParams) ([]byte, error) {
packageService := services.NewPackageService(sm.client)
packageService.ArtDetails = sm.config.GetServiceDetails()
return packageService.GetPackageLeadFile(leadFileParams)
}

func (sm *ArtifactoryServicesManagerImp) GetAllRepositories() (*[]services.RepositoryDetails, error) {
repositoriesService := services.NewRepositoriesService(sm.client)
repositoriesService.ArtDetails = sm.config.GetServiceDetails()
Expand Down
53 changes: 53 additions & 0 deletions artifactory/services/packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package services

import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientUtils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"net/http"
)

const apiLeadFile = "api/packagesSearch/leadFile"

type PackageService struct {
Client *jfroghttpclient.JfrogHttpClient
ArtDetails auth.ServiceDetails
}

func NewPackageService(client *jfroghttpclient.JfrogHttpClient) *PackageService {
return &PackageService{Client: client}
}

func (ps *PackageService) GetJfrogHttpClient() *jfroghttpclient.JfrogHttpClient {
return ps.Client
}

func (ps *PackageService) GetPackageLeadFile(leadFileRequest LeadFileParams) ([]byte, error) {
requestUrl, err := clientUtils.BuildUrl(ps.ArtDetails.GetUrl(), apiLeadFile, nil)
if err != nil {
return nil, err
}

requestContent, err := json.Marshal(leadFileRequest)
if err != nil {
return nil, errorutils.CheckError(err)
}

httpClientsDetails := ps.ArtDetails.CreateHttpClientDetails()
httpClientsDetails.SetContentTypeApplicationJson()

resp, body, err := ps.Client.SendPost(requestUrl, requestContent, &httpClientsDetails)
if err != nil {
return nil, err
}
return body, errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK)
}

type LeadFileParams struct {
PackageVersion string `json:"package_version"`
PackageName string `json:"package_name"`
PackageRepoName string `json:"package_repo_name"`
PackageType string `json:"package_type"`
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
65 changes: 65 additions & 0 deletions tests/artifactoryPackage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tests

import (
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

var leadFileRequest = services.LeadFileParams{
PackageVersion: "1.0.0",
PackageName: "test-package",
PackageRepoName: "test-repo",
PackageType: "test-type",
}

func TestPackage(t *testing.T) {
initArtifactoryTest(t)
t.Run("TestGetLeadFileSuccessfully", TestGetLeadFileSuccessfully)
}

func TestGetLeadFileSuccessfully(t *testing.T) {
handlerFunc := createDefaultHandlerFunc(t)
mockServer, packageService := createMockPackageServer(t, handlerFunc)

expectedLeadFile := "path/to/lead/file"
defer mockServer.Close()

leadFilePath, err := packageService.GetPackageLeadFile(leadFileRequest)
assert.NoError(t, err)
assert.Equal(t, expectedLeadFile, string(leadFilePath))
}

func createDefaultHandlerFunc(t *testing.T) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/api/packagesSearch/leadFile" {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
writeMockLeadFileResponse(t, w, []byte("path/to/lead/file"))
} else {
t.Errorf("Unexpected URL: got %s, want %s", r.RequestURI, "/api/packagesSearch/leadFile")
http.Error(w, "Not Found", http.StatusNotFound)
}
}
}

func writeMockLeadFileResponse(t *testing.T, w http.ResponseWriter, payload []byte) {
_, err := w.Write(payload)
assert.NoError(t, err)
}

func createMockPackageServer(t *testing.T, testHandler http.HandlerFunc) (*httptest.Server, *services.PackageService) {
testServer := httptest.NewServer(testHandler)

serviceDetails := auth.NewArtifactoryDetails()
serviceDetails.SetUrl(testServer.URL + "/")

packageService, err := jfroghttpclient.JfrogClientBuilder().Build()

assert.NoError(t, err)
return testServer, &services.PackageService{Client: packageService, ArtDetails: serviceDetails}
}
2 changes: 2 additions & 0 deletions tests/jfrogclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestMain(m *testing.M) {
func setupIntegrationTests() {
flag.Parse()
log.SetLogger(log.NewLogger(log.DEBUG, nil))

if *TestArtifactory || *TestDistribution || *TestXray || *TestRepositories || *TestMultipartUpload {
createArtifactoryUploadManager()
createArtifactorySearchManager()
Expand All @@ -44,6 +45,7 @@ func setupIntegrationTests() {
createArtifactoryUpdateFederatedRepositoryManager()
createArtifactoryDeleteRepositoryManager()
createArtifactoryGetRepositoryManager()
createArtifactoryGetPackageManager()
createArtifactoryReplicationCreateManager()
createArtifactoryReplicationUpdateManager()
createArtifactoryReplicationGetManager()
Expand Down
9 changes: 9 additions & 0 deletions tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var (
testsUpdateFederatedRepositoryService *services.FederatedRepositoryService
testsDeleteRepositoryService *services.DeleteRepositoryService
testsRepositoriesService *services.RepositoriesService
testsPackageService *services.PackageService
testsCreateReplicationService *services.CreateReplicationService
testsUpdateReplicationService *services.UpdateReplicationService
testsReplicationGetService *services.GetReplicationService
Expand Down Expand Up @@ -365,6 +366,14 @@ func createArtifactoryGetRepositoryManager() {
testsRepositoriesService.ArtDetails = artDetails
}

func createArtifactoryGetPackageManager() {
artDetails := GetRtDetails()
client, err := createJfrogHttpClient(&artDetails)
failOnHttpClientCreation(err)
testsPackageService = services.NewPackageService(client)
testsPackageService.ArtDetails = artDetails
}

func createArtifactoryReplicationCreateManager() {
artDetails := GetRtDetails()
client, err := createJfrogHttpClient(&artDetails)
Expand Down

0 comments on commit b60e50e

Please sign in to comment.