Skip to content

Commit

Permalink
Apply CR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva committed Aug 21, 2024
1 parent 3fc5f9c commit 900d91b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
20 changes: 15 additions & 5 deletions artifactory/services/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions artifactory/services/repositories_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 900d91b

Please sign in to comment.