diff --git a/artifactory/services/repositories.go b/artifactory/services/repositories.go index 960c23ced..9134237f3 100644 --- a/artifactory/services/repositories.go +++ b/artifactory/services/repositories.go @@ -3,6 +3,7 @@ package services import ( "encoding/json" "net/http" + "net/url" "github.com/jfrog/jfrog-client-go/auth" "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" @@ -49,6 +50,7 @@ func (rs *RepositoriesService) GetAll() (*[]RepositoryDetails, error) { } func (rs *RepositoriesService) GetWithFilter(params RepositoriesFilterParams) (*[]RepositoryDetails, error) { + log.Info("Getting repositories with filter ...") body, err := rs.sendGet(createWithFilterUrl(params)) if err != nil { return nil, err @@ -59,18 +61,26 @@ func (rs *RepositoriesService) GetWithFilter(params RepositoriesFilterParams) (* } func createWithFilterUrl(params RepositoriesFilterParams) string { - endpoint := apiRepositories + u := url.URL{ + Path: apiRepositories, + } + + queryParams := url.Values{} + if params.RepoType != "" { - endpoint += "?type=" + params.RepoType + log.Info("Getting repositories with type:", params.RepoType) + queryParams.Add("type", params.RepoType) } if params.PackageType != "" { - endpoint += "&packageType=" + params.PackageType + log.Info("Getting repositories with package type:", params.PackageType) + queryParams.Add("packageType", params.PackageType) } if params.ProjectKey != "" { log.Info("Getting repositories with project key:", params.ProjectKey) - endpoint += "&project=" + params.ProjectKey + queryParams.Add("project", params.ProjectKey) } - return endpoint + u.RawQuery = queryParams.Encode() + return u.String() } func (rs *RepositoriesService) sendGet(api string) ([]byte, error) { diff --git a/artifactory/services/repositories_test.go b/artifactory/services/repositories_test.go new file mode 100644 index 000000000..f3a8eae1c --- /dev/null +++ b/artifactory/services/repositories_test.go @@ -0,0 +1,26 @@ +package services + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCreateWithFilterUrl(t *testing.T) { + tests := []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 _, test := range tests { + result := createWithFilterUrl(test.params) + assert.Equal(t, test.expected, result, "For params %+v, expected %s, but got %s", test.params, test.expected, result) + } +}