Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed Aug 29, 2024
2 parents 7a15e7e + ad50d56 commit aec8b01
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ You can get all repositories from Artifactory filtered according to theirs type
params := services.NewRepositoriesFilterParams()
params.RepoType = "remote"
params.PackageType = "maven"
params.ProjectKey = "project-key"
err := servicesManager.GetAllRepositoriesFiltered(params)
```

Expand Down
31 changes: 27 additions & 4 deletions artifactory/services/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package services

import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
Expand Down Expand Up @@ -46,12 +46,11 @@ func (rs *RepositoriesService) IsExists(repoKey string) (exists bool, err error)

func (rs *RepositoriesService) GetAll() (*[]RepositoryDetails, error) {
log.Info("Getting all repositories ...")
return rs.GetWithFilter(RepositoriesFilterParams{RepoType: "", PackageType: ""})
return rs.GetWithFilter(RepositoriesFilterParams{})
}

func (rs *RepositoriesService) GetWithFilter(params RepositoriesFilterParams) (*[]RepositoryDetails, error) {
url := fmt.Sprintf("%s?type=%s&packageType=%s", apiRepositories, params.RepoType, params.PackageType)
body, err := rs.sendGet(url)
body, err := rs.sendGet(rs.createUrlWithFilter(params))
if err != nil {
return nil, err
}
Expand All @@ -60,6 +59,29 @@ func (rs *RepositoriesService) GetWithFilter(params RepositoriesFilterParams) (*
return repoDetails, errorutils.CheckError(err)
}

// This function is used to create the URL for the repositories API with the given filter params.
// The function expects to get a RepositoriesFilterParams struct that contains the desired filter params.
// The function returns the URL string.
func (rs *RepositoriesService)createUrlWithFilter(params RepositoriesFilterParams) string {
u := url.URL{
Path: apiRepositories,
}

queryParams := url.Values{}
if params.RepoType != "" {
queryParams.Add("type", params.RepoType)
}
if params.PackageType != "" {
queryParams.Add("packageType", params.PackageType)
}
if params.ProjectKey != "" {
queryParams.Add("project", params.ProjectKey)
}

u.RawQuery = queryParams.Encode()
return u.String()
}

func (rs *RepositoriesService) sendGet(api string) ([]byte, error) {
httpClientsDetails := rs.ArtDetails.CreateHttpClientDetails()
resp, body, _, err := rs.client.SendGet(rs.ArtDetails.GetUrl()+api, true, &httpClientsDetails)
Expand Down Expand Up @@ -113,6 +135,7 @@ func (rd RepositoryDetails) GetRepoType() string {
type RepositoriesFilterParams struct {
RepoType string
PackageType string
ProjectKey string
}

func NewRepositoriesFilterParams() RepositoriesFilterParams {
Expand Down
27 changes: 27 additions & 0 deletions artifactory/services/repositories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package services

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateUrlWithFilter(t *testing.T) {
rs := RepositoriesService{}
testCases := []struct {
params RepositoriesFilterParams
expected string
}{
{RepositoriesFilterParams{RepoType: "git", PackageType: "npm", ProjectKey: "123"}, "api/repositories?packageType=npm&project=123&type=git"},
{RepositoriesFilterParams{RepoType: "git", PackageType: "npm"}, "api/repositories?packageType=npm&type=git"},
{RepositoriesFilterParams{RepoType: "git"}, "api/repositories?type=git"},
{RepositoriesFilterParams{PackageType: "npm"}, "api/repositories?packageType=npm"},
{RepositoriesFilterParams{ProjectKey: "123"}, "api/repositories?project=123"},
{RepositoriesFilterParams{}, "api/repositories"},
}

for _, testCase := range testCases {
result := rs.createUrlWithFilter(testCase.params)
assert.Equal(t, testCase.expected, result, "For params %+v, expected %s, but got %s", testCase.params, testCase.expected, result)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/gookit/color v1.5.4
github.com/jfrog/archiver/v3 v3.6.1
github.com/jfrog/build-info-go v1.9.34
github.com/jfrog/build-info-go v1.9.35
github.com/jfrog/gofrog v1.7.5
github.com/minio/sha256-simd v1.0.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI=
github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw=
github.com/jfrog/build-info-go v1.9.34 h1:bPnW58VpclbpBe/x8XEu/2BIviEOoJrJ5PkRRcmU3Co=
github.com/jfrog/build-info-go v1.9.34/go.mod h1:6mdtqjREK76bHNODXakqKR/+ksJ9dvfLS7H57BZtnLY=
github.com/jfrog/build-info-go v1.9.35 h1:P53Ckbuin0GYrq0LWMY0GZSptJcQwiUyW6lqTbXKdcc=
github.com/jfrog/build-info-go v1.9.35/go.mod h1:6mdtqjREK76bHNODXakqKR/+ksJ9dvfLS7H57BZtnLY=
github.com/jfrog/gofrog v1.7.5 h1:dFgtEDefJdlq9cqTRoe09RLxS5Bxbe1Ev5+E6SmZHcg=
github.com/jfrog/gofrog v1.7.5/go.mod h1:jyGiCgiqSSR7k86hcUSu67XVvmvkkgWTmPsH25wI298=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
const (
Development = "development"
Agent = "jfrog-client-go"
Version = "1.45.0"
Version = "1.46.0"
)

type MinVersionProduct string
Expand Down

0 comments on commit aec8b01

Please sign in to comment.