From 0d65a0df3a37cea6617d6fccd06374b9e1ac3ff3 Mon Sep 17 00:00:00 2001 From: Tudor Macari Date: Tue, 5 Nov 2024 21:16:41 +0000 Subject: [PATCH] fix: [AH-602]: fix registry urls (#2931) * fix: [AH-602]: fix registry urls --- .../api/controller/metadata/artifact_mapper.go | 12 +++++++++--- .../api/controller/metadata/create_registry.go | 4 ++-- .../api/controller/metadata/get_artifacts.go | 4 ++-- .../api/controller/metadata/get_registries.go | 17 +++++++++++------ registry/app/api/controller/metadata/utils.go | 11 ++--------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/registry/app/api/controller/metadata/artifact_mapper.go b/registry/app/api/controller/metadata/artifact_mapper.go index cb59a4430..67060e6e6 100644 --- a/registry/app/api/controller/metadata/artifact_mapper.go +++ b/registry/app/api/controller/metadata/artifact_mapper.go @@ -19,6 +19,7 @@ import ( "errors" "path/filepath" + "github.com/harness/gitness/app/url" artifactapi "github.com/harness/gitness/registry/app/api/openapi/contracts/artifact" "github.com/harness/gitness/registry/types" @@ -26,11 +27,14 @@ import ( ) func GetArtifactMetadata( + ctx context.Context, artifacts []types.ArtifactMetadata, - registryURL string, + rootIdentifier string, + urlProvider url.Provider, ) []artifactapi.ArtifactMetadata { artifactMetadataList := make([]artifactapi.ArtifactMetadata, 0, len(artifacts)) for _, artifact := range artifacts { + registryURL := urlProvider.RegistryRefURL(ctx, GetRegistryRef(rootIdentifier, artifact.RepoName)) artifactMetadata := mapToArtifactMetadata(artifact, registryURL) artifactMetadataList = append(artifactMetadataList, *artifactMetadata) } @@ -129,17 +133,19 @@ func GetTagMetadata( } func GetAllArtifactResponse( + ctx context.Context, artifacts *[]types.ArtifactMetadata, count int64, pageNumber int64, pageSize int, - registryURL string, + rootIdentifier string, + urlProvider url.Provider, ) *artifactapi.ListArtifactResponseJSONResponse { var artifactMetadataList []artifactapi.ArtifactMetadata if artifacts == nil { artifactMetadataList = make([]artifactapi.ArtifactMetadata, 0) } else { - artifactMetadataList = GetArtifactMetadata(*artifacts, registryURL) + artifactMetadataList = GetArtifactMetadata(ctx, *artifacts, rootIdentifier, urlProvider) } pageCount := GetPageCount(count, pageSize) listArtifact := &artifactapi.ListArtifact{ diff --git a/registry/app/api/controller/metadata/create_registry.go b/registry/app/api/controller/metadata/create_registry.go index d5e5320af..d23b2d539 100644 --- a/registry/app/api/controller/metadata/create_registry.go +++ b/registry/app/api/controller/metadata/create_registry.go @@ -143,11 +143,11 @@ func (c *APIController) createVirtualRegistry( if err != nil { return throwCreateRegistry400Error(err), nil } - + repoURL := c.URLProvider.RegistryRefURL(ctx, GetRegistryRef(regInfo.RootIdentifier, repoEntity.Name)) return artifact.CreateRegistry201JSONResponse{ RegistryResponseJSONResponse: *CreateVirtualRepositoryResponse( repoEntity, c.getUpstreamProxyKeys(ctx, repoEntity.UpstreamProxies), - cleanupPolicies, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef), + cleanupPolicies, repoURL, ), }, nil } diff --git a/registry/app/api/controller/metadata/get_artifacts.go b/registry/app/api/controller/metadata/get_artifacts.go index 3f471f06f..37ce64d7a 100644 --- a/registry/app/api/controller/metadata/get_artifacts.go +++ b/registry/app/api/controller/metadata/get_artifacts.go @@ -88,8 +88,8 @@ func (c *APIController) GetAllArtifacts( }, nil } return artifact.GetAllArtifacts200JSONResponse{ - ListArtifactResponseJSONResponse: *GetAllArtifactResponse(artifacts, count, regInfo.pageNumber, regInfo.limit, - c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef)), + ListArtifactResponseJSONResponse: *GetAllArtifactResponse(ctx, artifacts, count, regInfo.pageNumber, regInfo.limit, + regInfo.RootIdentifier, c.URLProvider), }, nil } diff --git a/registry/app/api/controller/metadata/get_registries.go b/registry/app/api/controller/metadata/get_registries.go index 0d27ad733..2e5c84ba6 100644 --- a/registry/app/api/controller/metadata/get_registries.go +++ b/registry/app/api/controller/metadata/get_registries.go @@ -20,6 +20,7 @@ import ( apiauth "github.com/harness/gitness/app/api/auth" "github.com/harness/gitness/app/api/request" + "github.com/harness/gitness/app/url" "github.com/harness/gitness/registry/app/api/openapi/contracts/artifact" "github.com/harness/gitness/registry/app/pkg/commons" "github.com/harness/gitness/registry/app/store" @@ -112,21 +113,23 @@ func (c *APIController) GetAllRegistries( }, nil } return artifact.GetAllRegistries200JSONResponse{ - ListRegistryResponseJSONResponse: *GetAllRegistryResponse( + ListRegistryResponseJSONResponse: *GetAllRegistryResponse(ctx, repos, count, regInfo.pageNumber, - regInfo.limit, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef), + regInfo.limit, regInfo.RootIdentifier, c.URLProvider, ), }, nil } func GetAllRegistryResponse( + ctx context.Context, repos *[]store.RegistryMetadata, count int64, pageNumber int64, pageSize int, - registryURL string, + rootIdentifier string, + urlProvider url.Provider, ) *artifact.ListRegistryResponseJSONResponse { - repoMetadataList := GetRegistryMetadata(repos, registryURL) + repoMetadataList := GetRegistryMetadata(ctx, repos, rootIdentifier, urlProvider) pageCount := GetPageCount(count, pageSize) listRepository := &artifact.ListRegistry{ ItemCount: &count, @@ -143,8 +146,10 @@ func GetAllRegistryResponse( } func GetRegistryMetadata( + ctx context.Context, registryMetadatas *[]store.RegistryMetadata, - registryURL string, + rootIdentifier string, + urlProvider url.Provider, ) []artifact.RegistryMetadata { repoMetadataList := []artifact.RegistryMetadata{} for _, reg := range *registryMetadatas { @@ -174,7 +179,7 @@ func GetRegistryMetadata( PackageType: reg.PackageType, Type: reg.Type, LastModified: &modifiedAt, - Url: registryURL, + Url: urlProvider.RegistryRefURL(ctx, GetRegistryRef(rootIdentifier, reg.RegIdentifier)), ArtifactsCount: artifactCount, DownloadsCount: downloadCount, RegistrySize: &size, diff --git a/registry/app/api/controller/metadata/utils.go b/registry/app/api/controller/metadata/utils.go index 104332917..f4bb9ac2f 100644 --- a/registry/app/api/controller/metadata/utils.go +++ b/registry/app/api/controller/metadata/utils.go @@ -19,7 +19,6 @@ import ( "fmt" "math" "net/url" - "path" "regexp" "strconv" "strings" @@ -313,14 +312,8 @@ func GetSize(sizeVal int64) string { return size.String() } -func GetRepoURL(rootIdentifier, registry string, registryURL string) string { - parsedURL, err := url.Parse(registryURL) - if err != nil { - log.Error().Err(err).Msgf("Error parsing URL: %s", registryURL) - return "" - } - parsedURL.Path = path.Join(parsedURL.Path, strings.ToLower(rootIdentifier), registry) - return parsedURL.String() +func GetRegistryRef(rootIdentifier string, registryName string) string { + return rootIdentifier + "/" + registryName } func GetRepoURLWithoutProtocol(registryURL string) string {