fix: [AH-602]: fix registry urls (#2931)

* fix: [AH-602]: fix registry urls
This commit is contained in:
Tudor Macari 2024-11-05 21:16:41 +00:00 committed by Harness
parent 3404495893
commit 0d65a0df3a
5 changed files with 26 additions and 22 deletions

View File

@ -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{

View File

@ -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
}

View File

@ -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
}

View File

@ -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,

View File

@ -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 {