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 5, 2024
2 parents 11e9718 + 726f14e commit c983a75
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 90 deletions.
8 changes: 4 additions & 4 deletions artifactory/services/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"github.com/jfrog/build-info-go/entities"
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/crypto"
ioutils "github.com/jfrog/gofrog/io"
"github.com/jfrog/gofrog/version"
"net/http"
Expand Down Expand Up @@ -495,11 +495,11 @@ func createLocalSymlink(localPath, localFileName, symlinkArtifact string, symlin
if !fileutils.IsPathExists(symlinkArtifact, false) {
return errorutils.CheckErrorf("symlink validation failed, target doesn't exist: " + symlinkArtifact)
}
var checksums map[biutils.Algorithm]string
if checksums, err = biutils.GetFileChecksums(symlinkArtifact, biutils.SHA1); err != nil {
var checksums map[crypto.Algorithm]string
if checksums, err = crypto.GetFileChecksums(symlinkArtifact, crypto.SHA1); err != nil {
return errorutils.CheckError(err)
}
if checksums[biutils.SHA1] != symlinkContentChecksum {
if checksums[crypto.SHA1] != symlinkContentChecksum {
return errorutils.CheckErrorf("symlink validation failed for target: " + symlinkArtifact)
}
}
Expand Down
10 changes: 5 additions & 5 deletions artifactory/services/fspatterns/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package fspatterns
import (
"bytes"
"fmt"
"github.com/jfrog/gofrog/crypto"
"github.com/jfrog/jfrog-client-go/utils/log"
"os"
"regexp"
"strings"

biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
Expand Down Expand Up @@ -192,15 +192,15 @@ func GetRootPath(pattern, target, archiveTarget string, patternType utils.Patter

// When handling symlink we want to simulate the creation of empty file
func CreateSymlinkFileDetails() (*fileutils.FileDetails, error) {
checksums, err := biutils.CalcChecksums(bytes.NewBuffer([]byte(fileutils.SymlinkFileContent)))
checksums, err := crypto.CalcChecksums(bytes.NewBuffer([]byte(fileutils.SymlinkFileContent)))
if err != nil {
return nil, errorutils.CheckError(err)
}

details := new(fileutils.FileDetails)
details.Checksum.Md5 = checksums[biutils.MD5]
details.Checksum.Sha1 = checksums[biutils.SHA1]
details.Checksum.Sha256 = checksums[biutils.SHA256]
details.Checksum.Md5 = checksums[crypto.MD5]
details.Checksum.Sha1 = checksums[crypto.SHA1]
details.Checksum.Sha256 = checksums[crypto.SHA256]
details.Size = int64(0)
return details, nil
}
8 changes: 4 additions & 4 deletions artifactory/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"errors"
"fmt"
"github.com/jfrog/gofrog/crypto"
"io"
"net/http"
"os"
Expand All @@ -14,7 +15,6 @@ import (
"sync"

"github.com/jfrog/build-info-go/entities"
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/parallel"
"github.com/jfrog/jfrog-client-go/artifactory/services/fspatterns"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
Expand Down Expand Up @@ -190,12 +190,12 @@ func createProperties(artifact clientutils.Artifact, uploadParams UploadParams)
}
// If Symlink target exists -> get SHA1 if isn't a directory
} else if !fileInfo.IsDir() {
var checksums map[biutils.Algorithm]string
checksums, err := biutils.GetFileChecksums(artifact.LocalPath, biutils.SHA1)
var checksums map[crypto.Algorithm]string
checksums, err := crypto.GetFileChecksums(artifact.LocalPath, crypto.SHA1)
if err != nil {
return nil, errorutils.CheckError(err)
}
artifactProps.AddProperty(utils.SymlinkSha1, checksums[biutils.SHA1])
artifactProps.AddProperty(utils.SymlinkSha1, checksums[crypto.SHA1])
}
artifactProps.AddProperty(utils.ArtifactorySymlink, artifactSymlink)
}
Expand Down
13 changes: 7 additions & 6 deletions artifactory/services/utils/multipartupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/jfrog/gofrog/crypto"
"io"
"net/http"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"time"

biUtils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/parallel"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
Expand Down Expand Up @@ -159,11 +160,11 @@ func (mu *MultipartUpload) UploadFileConcurrently(localPath, targetPath string,
}

if sha1 == "" {
var checksums map[biUtils.Algorithm]string
if checksums, err = biUtils.GetFileChecksums(localPath); errorutils.CheckError(err) != nil {
var checksums map[crypto.Algorithm]string
if checksums, err = crypto.GetFileChecksums(localPath); errorutils.CheckError(err) != nil {
return
}
sha1 = checksums[biUtils.SHA1]
sha1 = checksums[crypto.SHA1]
}

if progress != nil {
Expand Down Expand Up @@ -262,8 +263,8 @@ func (mu *MultipartUpload) uploadPart(logMsgPrefix, localPath string, fileSize,
}

func (mu *MultipartUpload) createMultipartUpload(repoKey, repoPath string, partSize int64) (token string, err error) {
url := fmt.Sprintf("%s%screate?repoKey=%s&repoPath=%s&partSizeMB=%d", mu.artifactoryUrl, uploadsApi, repoKey, repoPath, partSize/SizeMiB)
resp, body, err := mu.client.SendPost(url, []byte{}, mu.httpClientsDetails)
requestUrl := fmt.Sprintf("%s%screate?repoKey=%s&repoPath=%s&partSizeMB=%d", mu.artifactoryUrl, uploadsApi, url.QueryEscape(repoKey), url.QueryEscape(repoPath), partSize/SizeMiB)
resp, body, err := mu.client.SendPost(requestUrl, []byte{}, mu.httpClientsDetails)
if err != nil {
return
}
Expand Down
64 changes: 41 additions & 23 deletions artifactory/services/utils/multipartupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,48 @@ func TestUnsupportedVersion(t *testing.T) {
}

func TestCreateMultipartUpload(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check method
assert.Equal(t, http.MethodPost, r.Method)

// Check URL
assert.Equal(t, "/api/v1/uploads/create", r.URL.Path)
assert.Equal(t, fmt.Sprintf("repoKey=%s&repoPath=%s&partSizeMB=%d", repoKey, repoPath, partSizeMB), r.URL.RawQuery)

// Send response 200 OK
w.WriteHeader(http.StatusOK)
response, err := json.Marshal(createMultipartUploadResponse{Token: token})
assert.NoError(t, err)
_, err = w.Write(response)
assert.NoError(t, err)
})

// Create mock multipart upload with server
multipartUpload, cleanUp := createMockMultipartUpload(t, handler)
defer cleanUp()
testCases := []struct {
name string
repoKey string
repoPath string
urlExpectedRepoKey string
urlExpectedRepoPath string
}{
{"Single word names", repoKey, repoPath, repoKey, repoPath},
{"Spaced names", "repo with space", "path with space", "repo+with+space", "path+with+space"},
{"Names contains _", "repo_name", "path_name", "repo_name", "path_name"},
{"Names contains %", "repo%name", "path%name", "repo%25name", "path%25name"},
{"Names contains &", "repo&name", "path&name", "repo%26name", "path%26name"},
}

// Execute CreateMultipartUpload
actualToken, err := multipartUpload.createMultipartUpload(repoKey, repoPath, partSize)
assert.NoError(t, err)
assert.Equal(t, token, actualToken)
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check method
assert.Equal(t, http.MethodPost, r.Method)

// Check URL
assert.Equal(t, "/api/v1/uploads/create", r.URL.Path)
assert.Equal(t, fmt.Sprintf("repoKey=%s&repoPath=%s&partSizeMB=%d", testCase.urlExpectedRepoKey, testCase.urlExpectedRepoPath, partSizeMB), r.URL.RawQuery)

// Send response 200 OK
w.WriteHeader(http.StatusOK)
response, err := json.Marshal(createMultipartUploadResponse{Token: token})
assert.NoError(t, err)
_, err = w.Write(response)
assert.NoError(t, err)
})

// Create mock multipart upload with server
multipartUpload, cleanUp := createMockMultipartUpload(t, handler)
defer cleanUp()

// Execute CreateMultipartUpload
actualToken, err := multipartUpload.createMultipartUpload(testCase.repoKey, testCase.repoPath, partSize)
assert.NoError(t, err)
assert.Equal(t, token, actualToken)
})
}
}

func TestUploadPartsConcurrentlyTooManyAttempts(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions artifactory/services/utils/searchutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ func (item ResultItem) GetItemRelativePath() string {
if item.Path == "." {
return path.Join(item.Repo, item.Name)
}

url := item.Repo
url = path.Join(url, item.Path, item.Name)
url := path.Join(item.Repo, item.Path, item.Name)
if item.Type == string(Folder) {
url = appendFolderSuffix(url)
}
Expand Down
4 changes: 2 additions & 2 deletions 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.31
github.com/jfrog/build-info-go v1.9.32
github.com/jfrog/gofrog v1.7.5
github.com/minio/sha256-simd v1.0.1
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -58,6 +58,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240729133409-38a2c49a0d75
// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240804091815-7407ceb49077

// replace github.com/jfrog/gofrog => github.com/jfrog/gofrog dev
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.31 h1:1pLC19hc9AEdWA87D+EcvMTLsDeMa390Z8TrNpCO4K8=
github.com/jfrog/build-info-go v1.9.31/go.mod h1:DZCElS/UhaSJHn0K1YzRUOJqiqVS4bjAEnGQSFncwNw=
github.com/jfrog/build-info-go v1.9.32 h1:PKXAMe84sMdob6eBtwwGz47Fz2cmjMwMPoHW8xuk08Q=
github.com/jfrog/build-info-go v1.9.32/go.mod h1:JTGnENexG1jRhKWCkQtZuDb0PerlzlSzF5OmMLG9kfc=
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
7 changes: 4 additions & 3 deletions utils/io/fileutils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"encoding/json"
"errors"
biutils "github.com/jfrog/build-info-go/utils"
"io"
"net/url"
"os"
Expand All @@ -14,7 +15,7 @@ import (
"strings"

"github.com/jfrog/build-info-go/entities"
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/crypto"
gofrog "github.com/jfrog/gofrog/io"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)
Expand Down Expand Up @@ -396,11 +397,11 @@ func GetFileDetailsFromReader(reader io.Reader, includeChecksums bool) (details
}

func calcChecksumDetailsFromReader(reader io.Reader) (entities.Checksum, error) {
checksums, err := biutils.CalcChecksums(reader)
checksums, err := crypto.CalcChecksums(reader)
if err != nil {
return entities.Checksum{}, errorutils.CheckError(err)
}
return entities.Checksum{Md5: checksums[biutils.MD5], Sha1: checksums[biutils.SHA1], Sha256: checksums[biutils.SHA256]}, nil
return entities.Checksum{Md5: checksums[crypto.MD5], Sha1: checksums[crypto.SHA1], Sha256: checksums[crypto.SHA256]}, nil
}

type FileDetails struct {
Expand Down
74 changes: 60 additions & 14 deletions utils/io/fileutils/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,68 @@ func TestListFilesRecursiveWalkIntoDirSymlink(t *testing.T) {
if io.IsWindows() {
t.Skip("Running on windows, skipping...")
}
expectedFileList := []string{
"testdata/dirsymlinks",
"testdata/dirsymlinks/d1",
"testdata/dirsymlinks/d1/File_F1",
"testdata/dirsymlinks/d1/linktoparent",
"testdata/dirsymlinks/d1/linktoparent/d1",
"testdata/dirsymlinks/d1/linktoparent/d1/File_F1",
"testdata/dirsymlinks/d1/linktoparent/d2",
"testdata/dirsymlinks/d1/linktoparent/d2/d1link",
"testdata/dirsymlinks/d1/linktoparent/d2/d1link/File_F1",
"testdata/dirsymlinks/d2",
}

parentTempDir := createSymlinksTreeForTest(t)
expectedFileList := generateExpectedSymlinksFileList(parentTempDir)

// This directory and its subdirectories contain a symlink to a parent directory and a symlink to a sibling directory.
testDirPath := filepath.Join("testdata", "dirsymlinks")
filesList, err := ListFilesRecursiveWalkIntoDirSymlink(testDirPath, true)
filesList, err := ListFilesRecursiveWalkIntoDirSymlink(parentTempDir, true)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(expectedFileList, filesList))
}

// Creates the following tree structure in a temp directory, and returns its path:
/*
├── d1
│ ├── File_F1
│ └── linkToParent -> ../
└── d2
└── linkToD1 -> ../d1/
*/
func createSymlinksTreeForTest(t *testing.T) string {
parentTempDir := t.TempDir()

// Create the "d1" directory
d1Path := filepath.Join(parentTempDir, "d1")
assert.NoError(t, os.Mkdir(d1Path, 0755))

// Create "File_F1" inside "d1"
fileF1, err := os.Create(filepath.Join(d1Path, "File_F1"))
assert.NoError(t, err)
assert.NoError(t, fileF1.Close())

// Create symlink "linkToParent" in "d1" pointing to the parent temp directory
linkToParentPath := filepath.Join(d1Path, "linkToParent")
assert.NoError(t, os.Symlink(parentTempDir, linkToParentPath))

// Create the "d2" directory
d2Path := filepath.Join(parentTempDir, "d2")
assert.NoError(t, os.Mkdir(d2Path, 0755))

// Create symlink "linkToD1" in "d2" pointing to the "d1" directory
d1LinkPath := filepath.Join(d2Path, "linkToD1")
assert.NoError(t, os.Symlink(d1Path, d1LinkPath))

return parentTempDir
}

// Generates the expected output of file list based on the provided parent dir.
func generateExpectedSymlinksFileList(parentDir string) []string {
expectedFileList := []string{
"",
"/d1",
"/d1/File_F1",
"/d1/linkToParent",
"/d1/linkToParent/d1",
"/d1/linkToParent/d1/File_F1",
"/d1/linkToParent/d2",
"/d1/linkToParent/d2/linkToD1",
"/d1/linkToParent/d2/linkToD1/File_F1",
"/d2",
}

for i, filePath := range expectedFileList {
expectedFileList[i] = parentDir + filePath
}
return expectedFileList
}
Loading

0 comments on commit c983a75

Please sign in to comment.