feat: [AH-1245]: fix file existence check (#3788)

* fix test
* fix formatting
* fix lint issues
* feat: [AH-1245]: resolve PR comments
* feat: [AH-1245]: fix file existance check
This commit is contained in:
Tudor Macari 2025-05-16 09:56:54 +00:00 committed by Harness
parent a2647e29c4
commit 88a5e34000
4 changed files with 23 additions and 16 deletions

View File

@ -78,7 +78,7 @@ type LocalBase interface {
error,
)
Exists(ctx context.Context, info pkg.ArtifactInfo, version string, fileName string) bool
Exists(ctx context.Context, info pkg.ArtifactInfo, path string) bool
ExistsByFilePath(ctx context.Context, registryID int64, filePath string) (bool, error)
@ -157,7 +157,7 @@ func (l *localBase) MoveTempFile(
Code: 0,
}
err := l.CheckIfFileAlreadyExist(ctx, info, version, metadata, fileInfo.Filename)
err := l.CheckIfFileAlreadyExist(ctx, info, version, metadata, fileInfo.Filename, path)
if err != nil {
if !errors.IsConflict(err) {
return nil, "", err
@ -204,13 +204,13 @@ func (l *localBase) uploadInternal(
Code: 0,
}
err := l.CheckIfFileAlreadyExist(ctx, info, version, metadata, fileName)
err := l.CheckIfFileAlreadyExist(ctx, info, version, metadata, fileName, path)
if err != nil {
if !errors.IsConflict(err) {
return nil, "", err
}
_, sha256, err2 := l.GetSHA256(ctx, info, version, fileName)
_, sha256, err2 := l.GetSHA256(ctx, info, path)
if err2 != nil {
return responseHeaders, "", err2
}
@ -308,8 +308,11 @@ func (l *localBase) Download(
return responseHeaders, fileReader, redirectURL, nil
}
func (l *localBase) Exists(ctx context.Context, info pkg.ArtifactInfo, version string, fileName string) bool {
exists, _, _ := l.GetSHA256(ctx, info, version, fileName)
func (l *localBase) Exists(ctx context.Context, info pkg.ArtifactInfo, path string) bool {
exists, _, err := l.GetSHA256(ctx, info, path)
if err != nil {
log.Ctx(ctx).Err(err).Msgf("Failed to check if file: [%s] exists", path)
}
return exists
}
@ -334,12 +337,12 @@ func (l *localBase) CheckIfVersionExists(ctx context.Context, info pkg.PackageAr
return true, nil
}
func (l *localBase) GetSHA256(ctx context.Context, info pkg.ArtifactInfo, version string, fileName string) (
func (l *localBase) GetSHA256(ctx context.Context, info pkg.ArtifactInfo, path string) (
exists bool,
sha256 string,
err error,
) {
filePath := "/" + info.Image + "/" + version + "/" + fileName
filePath := "/" + path
sha256, err = l.fileManager.HeadFile(ctx, filePath, info.RegistryID)
if err != nil {
return false, "", err
@ -409,6 +412,7 @@ func (l *localBase) CheckIfFileAlreadyExist(
version string,
metadata metadata.Metadata,
fileName string,
path string,
) error {
image, err := l.imageDao.GetByName(ctx, info.RegistryID, info.Image)
if err != nil && !strings.Contains(err.Error(), "resource not found") {
@ -436,9 +440,8 @@ func (l *localBase) CheckIfFileAlreadyExist(
}
for _, file := range metadata.GetFiles() {
if file.Filename == fileName {
l.Exists(ctx, info, version, fileName)
return errors.Conflict("file: [%s] with Artifact: [%s], Version: [%s] and registry: [%s] already exist",
if file.Filename == fileName && l.Exists(ctx, info, path) {
return errors.Conflict("file: [%s] for Artifact: [%s], Version: [%s] and registry: [%s] already exist",
fileName, info.Image, version, info.RegIdentifier)
}
}

View File

@ -18,6 +18,7 @@ import (
"context"
"io"
"github.com/harness/gitness/registry/app/pkg"
"github.com/harness/gitness/registry/app/pkg/base"
"github.com/harness/gitness/registry/app/pkg/commons"
"github.com/harness/gitness/registry/app/pkg/types/npm"
@ -52,7 +53,8 @@ func NewLocalRegistryHelper(localRegistry LocalRegistry, localBase base.LocalBas
}
func (h *localRegistryHelper) FileExists(ctx context.Context, info npm.ArtifactInfo) bool {
return h.localBase.Exists(ctx, info.ArtifactInfo, info.Version, info.Filename)
return h.localBase.Exists(ctx, info.ArtifactInfo,
pkg.JoinWithSeparator("/", info.Image, info.Version, info.Filename))
}
func (h *localRegistryHelper) DownloadFile(ctx context.Context, info npm.ArtifactInfo) (

View File

@ -18,6 +18,7 @@ import (
"context"
"io"
"github.com/harness/gitness/registry/app/pkg"
"github.com/harness/gitness/registry/app/pkg/base"
"github.com/harness/gitness/registry/app/pkg/commons"
"github.com/harness/gitness/registry/app/pkg/types/python"
@ -53,7 +54,7 @@ func NewLocalRegistryHelper(localRegistry LocalRegistry, localBase base.LocalBas
}
func (h *localRegistryHelper) FileExists(ctx context.Context, info python.ArtifactInfo) bool {
return h.localBase.Exists(ctx, info.ArtifactInfo, info.Version, info.Filename)
return h.localBase.Exists(ctx, info.ArtifactInfo, pkg.JoinWithSeparator("/", info.Image, info.Version, info.Filename))
}
func (h *localRegistryHelper) DownloadFile(ctx context.Context, info python.ArtifactInfo) (

View File

@ -114,8 +114,8 @@ func (m *MockLocalBase) DeleteVersion(_ context.Context, _ pkg.PackageArtifactIn
panic("implement me")
}
func (m *MockLocalBase) Exists(ctx context.Context, info pkg.ArtifactInfo, version, filename string) bool {
args := m.Called(ctx, info, version, filename)
func (m *MockLocalBase) Exists(ctx context.Context, info pkg.ArtifactInfo, path string) bool {
args := m.Called(ctx, info, path)
return args.Bool(0)
}
@ -206,7 +206,8 @@ func TestLocalRegistryHelper_FileExists(t *testing.T) {
Filename: "package-1.0.0.whl",
}
mockLocalBase.On("Exists", ctx, artifactInfo.ArtifactInfo, artifactInfo.Version, artifactInfo.Filename).Return(true)
mockLocalBase.On("Exists", ctx, artifactInfo.ArtifactInfo,
artifactInfo.Image+"/"+artifactInfo.Version+"/"+artifactInfo.Filename).Return(true)
exists := helper.FileExists(ctx, artifactInfo)