Skip to content

Commit

Permalink
feat: [AH-602]: refactoring (#2920)
Browse files Browse the repository at this point in the history
* rezolve PR comments
* rezolve PR comments
* refactoring
* feat: [AH-602]: refactoring
  • Loading branch information
tmacari-harness authored and Harness committed Nov 5, 2024
1 parent 0e5b3d6 commit ddd8b47
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 103 deletions.
13 changes: 10 additions & 3 deletions app/url/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ type Provider interface {
// GetAPIProto returns the proto for the API hostname
GetAPIProto(ctx context.Context) string

// RegistryURL returns the url for oci token endpoint
RegistryURL() string
// RegistryRefURL returns the registry url with registry ref.
RegistryRefURL(ctx context.Context, registryRef string) string

// RegistryBaseURL returns the registry base url.
RegistryBaseURL(ctx context.Context, hostnamePrefix string) string
}

// Provider provides the URLs of the Harness system.
Expand Down Expand Up @@ -235,7 +238,11 @@ func (p *provider) GetAPIProto(context.Context) string {
return p.apiURL.Scheme
}

func (p *provider) RegistryURL() string {
func (p *provider) RegistryRefURL(_ context.Context, registryRef string) string {
return p.registryURL.JoinPath(registryRef).String()
}

func (p *provider) RegistryBaseURL(_ context.Context, _ string) string {
return p.registryURL.String()
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/gitness/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 9 additions & 19 deletions registry/app/api/controller/metadata/artifact_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (

func GetArtifactMetadata(
artifacts []types.ArtifactMetadata,
rootIdentifier string,
registryURL string,
) []artifactapi.ArtifactMetadata {
artifactMetadataList := make([]artifactapi.ArtifactMetadata, 0, len(artifacts))
for _, artifact := range artifacts {
artifactMetadata := mapToArtifactMetadata(artifact, rootIdentifier, registryURL)
artifactMetadata := mapToArtifactMetadata(artifact, registryURL)
artifactMetadataList = append(artifactMetadataList, *artifactMetadata)
}
return artifactMetadataList
Expand All @@ -49,12 +48,11 @@ func GetRegistryArtifactMetadata(artifacts []types.ArtifactMetadata) []artifacta

func mapToArtifactMetadata(
artifact types.ArtifactMetadata,
rootIdentifier string,
registryURL string,
) *artifactapi.ArtifactMetadata {
lastModified := GetTimeInMs(artifact.ModifiedAt)
packageType := artifact.PackageType
pullCommand := GetPullCommand(rootIdentifier, artifact.RepoName, artifact.Name, artifact.Version,
pullCommand := GetPullCommand(artifact.Name, artifact.Version,
string(packageType), registryURL)
return &artifactapi.ArtifactMetadata{
RegistryIdentifier: artifact.RepoName,
Expand Down Expand Up @@ -102,8 +100,6 @@ func GetTagMetadata(
tags *[]types.TagMetadata,
latestTag string,
image string,
regIdentifier string,
rootIdentifier string,
registryURL string,
) []artifactapi.ArtifactVersionMetadata {
artifactVersionMetadataList := []artifactapi.ArtifactVersionMetadata{}
Expand All @@ -112,7 +108,7 @@ func GetTagMetadata(
size := GetImageSize(tag.Size)
digestCount := tag.DigestCount
isLatestVersion := latestTag == tag.Name
command := GetPullCommand(rootIdentifier, regIdentifier, image, tag.Name, string(tag.PackageType), registryURL)
command := GetPullCommand(image, tag.Name, string(tag.PackageType), registryURL)
packageType, err := toPackageType(string(tag.PackageType))
if err != nil {
log.Ctx(ctx).Error().Err(err).Msgf("Error converting package type %s", tag.PackageType)
Expand All @@ -137,14 +133,13 @@ func GetAllArtifactResponse(
count int64,
pageNumber int64,
pageSize int,
rootIdentifier string,
registryURL string,
) *artifactapi.ListArtifactResponseJSONResponse {
var artifactMetadataList []artifactapi.ArtifactMetadata
if artifacts == nil {
artifactMetadataList = make([]artifactapi.ArtifactMetadata, 0)
} else {
artifactMetadataList = GetArtifactMetadata(*artifacts, rootIdentifier, registryURL)
artifactMetadataList = GetArtifactMetadata(*artifacts, registryURL)
}
pageCount := GetPageCount(count, pageSize)
listArtifact := &artifactapi.ListArtifact{
Expand Down Expand Up @@ -215,15 +210,12 @@ func GetAllArtifactVersionResponse(
latestTag string,
image string,
count int64,
regInfo *RegistryRequestInfo,
pageNumber int64,
pageSize int,
rootIdentifier string,
registryURL string,
) *artifactapi.ListArtifactVersionResponseJSONResponse {
artifactVersionMetadataList := GetTagMetadata(
ctx, tags, latestTag, image,
regInfo.RegistryIdentifier, rootIdentifier, registryURL,
ctx, tags, latestTag, image, registryURL,
)
pageCount := GetPageCount(count, pageSize)
listArtifactVersions := &artifactapi.ListArtifactVersion{
Expand All @@ -245,11 +237,10 @@ func GetDockerArtifactDetails(
tag *types.TagDetail,
manifest *types.Manifest,
isLatestTag bool,
regInfo *RegistryRequestBaseInfo,
registryURL string,
) *artifactapi.DockerArtifactDetailResponseJSONResponse {
repoPath := getRepoPath(registry.Name, tag.ImageName, manifest.Digest.String())
pullCommand := GetDockerPullCommand(regInfo.RootIdentifier, registry.Name, tag.ImageName, tag.Name, registryURL)
pullCommand := GetDockerPullCommand(tag.ImageName, tag.Name, registryURL)
createdAt := GetTimeInMs(tag.CreatedAt)
modifiedAt := GetTimeInMs(tag.UpdatedAt)
size := GetSize(manifest.TotalSize)
Expand All @@ -262,7 +253,7 @@ func GetDockerArtifactDetails(
ModifiedAt: &modifiedAt,
RegistryPath: repoPath,
PullCommand: &pullCommand,
Url: GetTagURL(regInfo.RootIdentifier, tag.ImageName, tag.Name, registry.Name, registryURL),
Url: GetTagURL(tag.ImageName, tag.Name, registryURL),
Size: &size,
}

Expand All @@ -278,11 +269,10 @@ func GetHelmArtifactDetails(
tag *types.TagDetail,
manifest *types.Manifest,
isLatestTag bool,
rootIdentifier string,
registryURL string,
) *artifactapi.HelmArtifactDetailResponseJSONResponse {
repoPath := getRepoPath(registry.Name, tag.ImageName, manifest.Digest.String())
pullCommand := GetHelmPullCommand(rootIdentifier, registry.Name, tag.ImageName, tag.Name, registryURL)
pullCommand := GetHelmPullCommand(tag.ImageName, tag.Name, registryURL)
createdAt := GetTimeInMs(tag.CreatedAt)
modifiedAt := GetTimeInMs(tag.UpdatedAt)
size := GetSize(manifest.TotalSize)
Expand All @@ -295,7 +285,7 @@ func GetHelmArtifactDetails(
ModifiedAt: &modifiedAt,
RegistryPath: repoPath,
PullCommand: &pullCommand,
Url: GetTagURL(rootIdentifier, tag.ImageName, tag.Name, registry.Name, registryURL),
Url: GetTagURL(tag.ImageName, tag.Name, registryURL),
Size: &size,
}

Expand Down
3 changes: 1 addition & 2 deletions registry/app/api/controller/metadata/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ func CreateVirtualRepositoryResponse(
registry *types.Registry,
upstreamProxyKeys []string,
cleanupPolicies *[]types.CleanupPolicy,
rootIdentifier string,
registryURL string,
) *api.RegistryResponseJSONResponse {
createdAt := GetTimeInMs(registry.CreatedAt)
Expand All @@ -308,7 +307,7 @@ func CreateVirtualRepositoryResponse(
Data: api.Registry{
Identifier: registry.Name,
Description: &registry.Description,
Url: GetRepoURL(rootIdentifier, registry.Name, registryURL),
Url: registryURL,
PackageType: registry.PackageType,
AllowedPattern: &allowedPattern,
BlockedPattern: &blockedPattern,
Expand Down
2 changes: 1 addition & 1 deletion registry/app/api/controller/metadata/create_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *APIController) createVirtualRegistry(
return artifact.CreateRegistry201JSONResponse{
RegistryResponseJSONResponse: *CreateVirtualRepositoryResponse(
repoEntity, c.getUpstreamProxyKeys(ctx, repoEntity.UpstreamProxies),
cleanupPolicies, regInfo.RootIdentifier, c.URLProvider.RegistryURL(),
cleanupPolicies, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion registry/app/api/controller/metadata/get_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *APIController) GetAllArtifacts(
}
return artifact.GetAllArtifacts200JSONResponse{
ListArtifactResponseJSONResponse: *GetAllArtifactResponse(artifacts, count, regInfo.pageNumber, regInfo.limit,
regInfo.RootIdentifier, c.URLProvider.RegistryURL()),
c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef)),
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *APIController) GetDockerArtifactDetails(
return artifact.GetDockerArtifactDetails200JSONResponse{
DockerArtifactDetailResponseJSONResponse: *GetDockerArtifactDetails(
registry, tag, m,
latestTag.ID == tag.ID, regInfo, c.URLProvider.RegistryURL(),
latestTag.ID == tag.ID, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *APIController) GetHelmArtifactDetails(
return artifact.GetHelmArtifactDetails200JSONResponse{
HelmArtifactDetailResponseJSONResponse: *GetHelmArtifactDetails(
registry, tag, m,
latestTag.ID == tag.ID, regInfo.RootIdentifier, c.URLProvider.RegistryURL(),
latestTag.ID == tag.ID, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (c *APIController) GetAllArtifactVersions(

return artifact.GetAllArtifactVersions200JSONResponse{
ListArtifactVersionResponseJSONResponse: *GetAllArtifactVersionResponse(
ctx, tags, latestTag, image, count,
regInfo, regInfo.pageNumber, regInfo.limit, regInfo.RootIdentifier, c.URLProvider.RegistryURL(),
ctx, tags, latestTag, image, count, regInfo.pageNumber, regInfo.limit,
c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand Down
22 changes: 9 additions & 13 deletions registry/app/api/controller/metadata/get_client_setup_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/request"
"github.com/harness/gitness/app/paths"
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
"github.com/harness/gitness/registry/app/common"
"github.com/harness/gitness/types/enum"
Expand Down Expand Up @@ -95,25 +94,21 @@ func (c *APIController) GetClientSetupDetails(

return artifact.GetClientSetupDetails200JSONResponse{
ClientSetupDetailsResponseJSONResponse: *GetClientSetupDetails(
ctx, packageType, regInfo,
string(r.RegistryRef), imageParam, tagParam, c.URLProvider.RegistryURL(),
ctx, packageType, imageParam, tagParam, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}

func GetClientSetupDetails(
ctx context.Context,
packageType string,
_ *RegistryRequestBaseInfo,
regRef string,
image *artifact.ArtifactParam,
tag *artifact.VersionParam,
registryURL string,
) *artifact.ClientSetupDetailsResponseJSONResponse {
session, _ := request.AuthSessionFrom(ctx)
username := session.Principal.Email
hostname := common.GenerateSetupClientHostname(registryURL)
regRef = strings.ToLower(regRef)
hostname, regRef := common.GenerateSetupClientHostnameAndRegistry(registryURL)

// Fixme: Use ENUMS
if packageType == "HELM" {
Expand Down Expand Up @@ -264,8 +259,12 @@ func GetClientSetupDetails(
}

func replacePlaceholders(
clientSetupDetails artifact.ClientSetupDetails, username string, hostname string,
regRef string, image *artifact.ArtifactParam, tag *artifact.VersionParam,
clientSetupDetails artifact.ClientSetupDetails,
username string,
hostname string,
regRef string,
image *artifact.ArtifactParam,
tag *artifact.VersionParam,
) {
for _, s := range clientSetupDetails.Sections {
if s.Steps == nil {
Expand All @@ -291,9 +290,6 @@ func replaceText(
image *artifact.ArtifactParam,
tag *artifact.VersionParam,
) {
rootSpace, _, _ := paths.DisectRoot(regRef)
_, registryName, _ := paths.DisectLeaf(regRef)
repoRef := rootSpace + "/" + registryName
if username != "" {
(*st.Commands)[i] = strings.ReplaceAll((*st.Commands)[i], "<USERNAME>", username)
}
Expand All @@ -303,7 +299,7 @@ func replaceText(
if regRef != "" {
(*st.Commands)[i] = strings.ReplaceAll(
(*st.Commands)[i],
"<REPOSITORY_REFERENCE>", repoRef,
"<REPOSITORY_REFERENCE>", regRef,
)
}
if image != nil {
Expand Down
8 changes: 3 additions & 5 deletions registry/app/api/controller/metadata/get_registries.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *APIController) GetAllRegistries(
return artifact.GetAllRegistries200JSONResponse{
ListRegistryResponseJSONResponse: *GetAllRegistryResponse(
repos, count, regInfo.pageNumber,
regInfo.limit, regInfo.RootIdentifier, c.URLProvider.RegistryURL(),
regInfo.limit, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand All @@ -124,10 +124,9 @@ func GetAllRegistryResponse(
count int64,
pageNumber int64,
pageSize int,
rootIdentifier string,
registryURL string,
) *artifact.ListRegistryResponseJSONResponse {
repoMetadataList := GetRegistryMetadata(repos, rootIdentifier, registryURL)
repoMetadataList := GetRegistryMetadata(repos, registryURL)
pageCount := GetPageCount(count, pageSize)
listRepository := &artifact.ListRegistry{
ItemCount: &count,
Expand All @@ -145,7 +144,6 @@ func GetAllRegistryResponse(

func GetRegistryMetadata(
registryMetadatas *[]store.RegistryMetadata,
rootIdentifier string,
registryURL string,
) []artifact.RegistryMetadata {
repoMetadataList := []artifact.RegistryMetadata{}
Expand Down Expand Up @@ -176,7 +174,7 @@ func GetRegistryMetadata(
PackageType: reg.PackageType,
Type: reg.Type,
LastModified: &modifiedAt,
Url: GetRepoURL(rootIdentifier, reg.RegIdentifier, registryURL),
Url: registryURL,
ArtifactsCount: artifactCount,
DownloadsCount: downloadCount,
RegistrySize: &size,
Expand Down
2 changes: 1 addition & 1 deletion registry/app/api/controller/metadata/get_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *APIController) GetRegistry(
repoEntity, c.getUpstreamProxyKeys(
ctx,
repoEntity.UpstreamProxies,
), cleanupPolicies, regInfo.RootIdentifier, c.URLProvider.RegistryURL(),
), cleanupPolicies, c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion registry/app/api/controller/metadata/update_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *APIController) updateVirtualRegistry(
RegistryResponseJSONResponse: *CreateVirtualRepositoryResponse(
modifiedRepoEntity,
c.getUpstreamProxyKeys(ctx, modifiedRepoEntity.UpstreamProxies), cleanupPolicies,
regInfo.RootIdentifier, c.URLProvider.RegistryURL(),
c.URLProvider.RegistryRefURL(ctx, regInfo.RegistryRef),
),
}, nil
}
Expand Down
Loading

0 comments on commit ddd8b47

Please sign in to comment.