diff --git a/artifactory/services/fspatterns/utils.go b/artifactory/services/fspatterns/utils.go index df1a3e8f5..d87698b86 100644 --- a/artifactory/services/fspatterns/utils.go +++ b/artifactory/services/fspatterns/utils.go @@ -15,7 +15,7 @@ import ( ) // Return all the existing paths of the provided root path -func ListFiles(rootPath string, isRecursive, includeDirs, isSymlink bool, excludePathPattern string) ([]string, error) { +func ListFiles(rootPath string, isRecursive, includeDirs, excludeWithRelativePath, isSymlink bool, excludePathPattern string) ([]string, error) { var paths []string var err error if isRecursive { @@ -26,7 +26,11 @@ func ListFiles(rootPath string, isRecursive, includeDirs, isSymlink bool, exclud if err != nil { return paths, err } - return filterFiles(paths, excludePathPattern) + var rootFilter string + if excludeWithRelativePath { + rootFilter = rootPath + } + return filterFiles(rootFilter, paths, excludePathPattern) } // Transform to regexp and prepare Exclude patterns to be used, exclusion patterns must be absolute paths. @@ -49,13 +53,13 @@ func PrepareExcludePathPattern(exclusions []string, patternType utils.PatternTyp return excludePathPattern } -func filterFiles(files []string, excludePathPattern string) (filteredFiles []string, err error) { +func filterFiles(rootPath string, files []string, excludePathPattern string) (filteredFiles []string, err error) { var excludedPath bool for i := 0; i < len(files); i++ { if files[i] == "." { continue } - excludedPath, err = isPathExcluded(files[i], excludePathPattern) + excludedPath, err = isPathExcluded(strings.TrimPrefix(files[i], rootPath), excludePathPattern) if err != nil { return } diff --git a/artifactory/services/fspatterns/utils_test.go b/artifactory/services/fspatterns/utils_test.go index 66f598ff3..fbfb1bea9 100644 --- a/artifactory/services/fspatterns/utils_test.go +++ b/artifactory/services/fspatterns/utils_test.go @@ -12,12 +12,19 @@ func TestFilterFiles(t *testing.T) { data := []struct { files []string ExcludePattern string + root string result []string }{ - {[]string{"file1", filepath.Join("dir", "file1"), "file2.zip"}, "^*.zip$", []string{"file1", filepath.Join("dir", "file1")}}, + {[]string{"file1", filepath.Join("dir", "file1"), "file2.zip"}, "^*.zip$", "", []string{"file1", filepath.Join("dir", "file1")}}, + {[]string{ + "file1", + "test.zip", + filepath.Join("test", "file1"), + filepath.Join("dir", "test", "should-be-filter"), + }, "(^.*test.*$)", "test", []string{"file1", "test.zip", filepath.Join("test", "file1")}}, } for _, d := range data { - got, err := filterFiles(d.files, d.ExcludePattern) + got, err := filterFiles(d.root, d.files, d.ExcludePattern) assert.NoError(t, err) assert.Len(t, got, len(d.result)) assert.Contains(t, got, d.files[0]) diff --git a/artifactory/services/upload.go b/artifactory/services/upload.go index 0abf34585..10efdb304 100644 --- a/artifactory/services/upload.go +++ b/artifactory/services/upload.go @@ -306,7 +306,7 @@ func scanFilesByPattern(uploadParams UploadParams, rootPath string, progressMgr if errorutils.CheckError(err) != nil { return err } - paths, err := fspatterns.ListFiles(rootPath, uploadParams.IsRecursive(), uploadParams.IsIncludeDirs(), uploadParams.IsSymlink(), excludePathPattern) + paths, err := fspatterns.ListFiles(rootPath, uploadParams.IsRecursive(), uploadParams.IsIncludeDirs(), false, uploadParams.IsSymlink(), excludePathPattern) if err != nil { return err } diff --git a/go.mod b/go.mod index 247abdfd8..c3bdba052 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/go-git/go-git/v5 v5.9.0 github.com/golang-jwt/jwt/v4 v4.5.0 github.com/gookit/color v1.5.4 - github.com/jfrog/build-info-go v1.9.13 + github.com/jfrog/build-info-go v1.9.15 github.com/jfrog/gofrog v1.3.1 github.com/mholt/archiver/v3 v3.5.1 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 4544a5af0..8fe2dd04c 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jfrog/build-info-go v1.9.13 h1:OeoGzPVK/O4TOUYk35uL4bXg/hleyqMrjGjjmyLOYrg= -github.com/jfrog/build-info-go v1.9.13/go.mod h1:ujJ8XQZMdT2tMkLSMJNyDd1pCY+duwHdjV+9or9FLIg= +github.com/jfrog/build-info-go v1.9.15 h1:DN7DKZq6H5FlHfL3Lu8fo4t2INgczRgT09dJiZjJ1oo= +github.com/jfrog/build-info-go v1.9.15/go.mod h1:XVFk2rCYhIdc7+hIGE8TC3le5PPM+xYHU22udoE2b7Q= github.com/jfrog/gofrog v1.3.1 h1:QqAwQXCVReT724uga1AYqG/ZyrNQ6f+iTxmzkb+YFQk= github.com/jfrog/gofrog v1.3.1/go.mod h1:IFMc+V/yf7rA5WZ74CSbXe+Lgf0iApEQLxRZVzKRUR0= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= diff --git a/http/httpclient/client.go b/http/httpclient/client.go index 24f24adb8..dc58ba463 100644 --- a/http/httpclient/client.go +++ b/http/httpclient/client.go @@ -46,6 +46,10 @@ func (jc *HttpClient) GetRetries() int { return jc.retries } +func (jc *HttpClient) GetClient() *http.Client { + return jc.client +} + func (jc *HttpClient) GetRetryWaitTime() int { return jc.retryWaitMilliSecs } diff --git a/utils/usage/reportusage.go b/utils/usage/reportusage.go index 765486f8c..4b228f007 100644 --- a/utils/usage/reportusage.go +++ b/utils/usage/reportusage.go @@ -1,14 +1,16 @@ package usage import ( + "bytes" "encoding/json" "errors" + "fmt" + "io" "net/http" - "github.com/jfrog/jfrog-client-go/artifactory/services/utils" "github.com/jfrog/jfrog-client-go/http/httpclient" "github.com/jfrog/jfrog-client-go/utils/errorutils" - "github.com/jfrog/jfrog-client-go/utils/io/httputils" + "github.com/jfrog/jfrog-client-go/utils/log" ) const ( @@ -51,11 +53,37 @@ func CreateUsageData(productId, accountId, clientId string, features ...string) } func sendRequestToEcosystemService(content []byte) (resp *http.Response, respBody []byte, err error) { + client, req, err := createEcosystemRequestInfo(content) + if err != nil { + return + } + log.Debug(fmt.Sprintf("Sending HTTP %s request to: %s", req.Method, req.URL)) + resp, err = client.Do(req) + err = errorutils.CheckError(err) + if err != nil { + return + } + if resp == nil { + err = errorutils.CheckErrorf("Ecosystem-Usage Received empty response from server") + return + } + defer func() { + if resp.Body != nil { + err = errors.Join(err, errorutils.CheckError(resp.Body.Close())) + } + }() + respBody, _ = io.ReadAll(resp.Body) + return +} + +func createEcosystemRequestInfo(content []byte) (c *http.Client, req *http.Request, err error) { var client *httpclient.HttpClient if client, err = httpclient.ClientBuilder().Build(); err != nil { return } - details := httputils.HttpClientDetails{} - utils.AddHeader("Content-Type", "application/json", &details.Headers) - return client.SendPost(ecosystemUsageApiPath, content, details, "Ecosystem-Usage") + if req, err = http.NewRequest(http.MethodPost, ecosystemUsageApiPath, bytes.NewBuffer(content)); err != nil { + return + } + req.Header.Set("Content-Type", "application/json") + return client.GetClient(), req, errorutils.CheckError(err) } diff --git a/utils/utils.go b/utils/utils.go index 42b5f2d60..886a2e9c5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -27,7 +27,7 @@ import ( const ( Development = "development" Agent = "jfrog-client-go" - Version = "1.34.3" + Version = "1.34.4" ) type MinVersionProduct string