drone/registry/app/api/controller/metadata/artifact_mapper.go
Arvind Choudhary 479c9b9fe7 feat: [AH-307]: GC interface Integration, event framework; schema changes; UI changes (#2688)
* fix
* Merge branch 'main' into AH-307-plus-url-support-2_no_rbac
* resolve PR comments
* resolve PR comments
* resolve PR comments
* feat: [AH-346]: new api changes for version list and digest list (#2726)

* feat: [AH-346]: new api changes for version list and digest list
* resolve pr comments
* resolve pr comments
* feat: [AH-346]: new api yaml integration (#2716)

* feat: [AH-346]: new api yaml integration
* Merge branch 'main' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2_no_rbac
* fix wire check
* fix lint issues
* fix: [AH-357]: migrations
* changes for global artifact listing (#2708)

* changes for global artifact listing
* Merge branch 'main' into AH-307-plus-url-support-2_no_rbac
* merged main
* Merge branch 'main' into AH-307-plus-url-support-2_no_rbac
* [AH-307]: Updated lint
* fix comment
* add new method to spacestore
* feat: [AH-307]: fix after rebase with main
* [AH-307]: Removing comments
* [AH-307]: linting fixes
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events (#2657)

* feat: [AH-286]: publish artifact event - no row found is not error
* feat: [AH-286]: publish artifact event - no row found is not error
* feat: [AH-286]: publish artifact event - lint errors, move publishing event outside DB transaction
* feat: [AH-286]: publish artifact event - review comments
* feat: [AH-286]: publish artifact event - address review comments
* feat: [AH-286]: publish artifact event - keep payload generic
* feat: [AH-286]: publish artifact event - as sqlite locks DB, perform db operation outside goroutine publishing of events
* feat: [AH-286]: publish artifact event - make publishing event async
* feat: [AH-286]: publish artifact event - use api types
* feat: [AH-286]: Publish event for SSCA to trigger scans - no need to export spacePathStore
* feat: [AH-286]: Publish event for SSCA to trigger scans - send spacePath instead of parentID
* feat: [AH-286]: Publish event for SSCA to trigger scans - rename scanner as generic reporter
* feat: [AH-286]: Publish event for SSCA to trigger scans - rename scanner as generic reporter
* feat: [AH-286]: publish artifact event - reuse redis.Send()
* feat: [AH-286]: Publish event for SSCA to trigger scans - review comments
* feat: [AH-286]: Publish event for SSCA to trigger scans - remove unused interface
* feat: [AH-286]: Publish event for SSCA to trigger scans - update msg format
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events
* feat: [AH-286]: Publish event for SSCA to trigger scans - extract acctID/orgID/projectID from spacepathStore
* feat: [AH-286]: publish artifact event - remove protobuf reference, fix lint errors
* feat: [AH-286]: publish artifact event - fix msg format
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events
* feat: [AH-321]: make repo form disabled for rbac (#2687)

* feat: [AH-321]: make repo form disabled for rbac
* fix wire-gen
* GC refactoring
* feat: [AH-340]: update UI as per the product feedbacks (#2685)

* feat: [AH-340]: update UI as per the product feedbacks
* feat: [AH-44]: add module data while redirecting to pipeline execution page
* feat: [AH-44]: add build pipeline details in overview cards
* feat: [AH-44]: update view for prod and non prod tag
* feat: [AH-44]: rearrange filters on artifact list apge
* feat: [AH-10]: add schema for overview cards, update artifact list, add ai search input, update api for registry artifact list and update mapping for deployments table
* feat: [AH-307]: add secretSpacePath in upstream password field while sending to BE (#2631)

* feat: [AH-307]: add secretSpacePath in upstream password field while sending to BE
* feat: [AH-299]: support new changes for artifact list page (#2630)

* feat: update har service api version
* feat: [AH-30]: integrate API schema for deployments list content
* feat: [AH-300]: update tag colors for prod and non prod tags
* feat: [AH-300]: Add Deployments table in artiface version details page
* feat: [AH-299]: support new changes for artifact list page
* feat: [AH-299]: support new changes for artifact list page
* feat: [AH-321]: support artifact registry rbac permission on UI (#2671)

* feat: [AH-321]: support artifact registry rbac permission on UI
* enable rbac (#2664)

* fix scope
* enable rbac
* feat: [AH-307]: hide code tab from version details page for both docker and helm
* feat: [AH-240]: add custom handling for enterprise auth type field
* Merge branch 'AH-307-plus-url-support-2_no_rbac' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2_no_rbac
* feat: [AH-307]: send space_ref in query param while creating registries
* lowercase rootRef
* [AH-307]: updated route
* [AH-307]: Added logs
* [AH-307]: Added logs
* feat: [AH-317]: add space_ref query param
* local
* Merge commit
* Merge commit
* Merge commit
* Added comments
* Revert changes
* Merge commit
* Merge branch 'main' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2
* Merge branch 'AH-306d' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2
* fix space path handling
* Merge branch 'main' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2
* Updated URLs to support slashes with + separator
* fix: [AH-306c]: fix anonymous flow
* fix: [AH-306c]: fix anonymous flow
* feat: [AH-307]: plus url support on UI

(cherry picked from commit 3fb6add3ce03498b6668b5f8f6d547e1acedaec4)
* [AH-307]: Added examples

(cherry picked from commit e83e41303da536f421be333be04aed09fbf75f5f)
* [AH-307]: Added Regex request rewrite support

(cherry picked from commit ed7b155256bdcd1134bc228b5705556a1233add6)
* fix: [AH-306c]: fix anonymous flow
2024-09-24 12:47:53 +00:00

349 lines
11 KiB
Go

// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metadata
import (
"context"
"errors"
"path/filepath"
artifactapi "github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
"github.com/harness/gitness/registry/types"
"github.com/rs/zerolog/log"
)
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)
artifactMetadataList = append(artifactMetadataList, *artifactMetadata)
}
return artifactMetadataList
}
func GetRegistryArtifactMetadata(artifacts []types.ArtifactMetadata) []artifactapi.RegistryArtifactMetadata {
artifactMetadataList := make([]artifactapi.RegistryArtifactMetadata, 0, len(artifacts))
for _, artifact := range artifacts {
artifactMetadata := mapToRegistryArtifactMetadata(artifact)
artifactMetadataList = append(artifactMetadataList, *artifactMetadata)
}
return artifactMetadataList
}
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,
string(packageType), registryURL)
return &artifactapi.ArtifactMetadata{
RegistryIdentifier: artifact.RepoName,
Name: artifact.Name,
Version: &artifact.Version,
Labels: &artifact.Labels,
LastModified: &lastModified,
PackageType: &packageType,
DownloadsCount: &artifact.DownloadCount,
PullCommand: &pullCommand,
}
}
func mapToRegistryArtifactMetadata(artifact types.ArtifactMetadata) *artifactapi.RegistryArtifactMetadata {
lastModified := GetTimeInMs(artifact.ModifiedAt)
packageType := artifact.PackageType
return &artifactapi.RegistryArtifactMetadata{
RegistryIdentifier: artifact.RepoName,
Name: artifact.Name,
LatestVersion: artifact.LatestVersion,
Labels: &artifact.Labels,
LastModified: &lastModified,
PackageType: &packageType,
DownloadsCount: &artifact.DownloadCount,
}
}
func toPackageType(packageTypeStr string) (artifactapi.PackageType, error) {
switch packageTypeStr {
case string(artifactapi.PackageTypeDOCKER):
return artifactapi.PackageTypeDOCKER, nil
case string(artifactapi.PackageTypeGENERIC):
return artifactapi.PackageTypeGENERIC, nil
case string(artifactapi.PackageTypeHELM):
return artifactapi.PackageTypeHELM, nil
case string(artifactapi.PackageTypeMAVEN):
return artifactapi.PackageTypeMAVEN, nil
default:
return "", errors.New("invalid package type")
}
}
func GetTagMetadata(
ctx context.Context,
tags *[]types.TagMetadata,
latestTag string,
image string,
regIdentifier string,
rootIdentifier string,
registryURL string,
) []artifactapi.ArtifactVersionMetadata {
artifactVersionMetadataList := []artifactapi.ArtifactVersionMetadata{}
for _, tag := range *tags {
modifiedAt := GetTimeInMs(tag.ModifiedAt)
size := GetImageSize(tag.Size)
digestCount := tag.DigestCount
isLatestVersion := latestTag == tag.Name
command := GetPullCommand(rootIdentifier, regIdentifier, 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)
continue
}
artifactVersionMetadata := &artifactapi.ArtifactVersionMetadata{
PackageType: &packageType,
Name: tag.Name,
Size: &size,
LastModified: &modifiedAt,
DigestCount: &digestCount,
IslatestVersion: &isLatestVersion,
PullCommand: &command,
}
artifactVersionMetadataList = append(artifactVersionMetadataList, *artifactVersionMetadata)
}
return artifactVersionMetadataList
}
func GetAllArtifactResponse(
artifacts *[]types.ArtifactMetadata,
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)
}
pageCount := GetPageCount(count, pageSize)
listArtifact := &artifactapi.ListArtifact{
ItemCount: &count,
PageCount: &pageCount,
PageIndex: &pageNumber,
PageSize: &pageSize,
Artifacts: artifactMetadataList,
}
response := &artifactapi.ListArtifactResponseJSONResponse{
Data: *listArtifact,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetAllArtifactByRegistryResponse(
artifacts *[]types.ArtifactMetadata,
count int64,
pageNumber int64,
pageSize int,
) *artifactapi.ListRegistryArtifactResponseJSONResponse {
var artifactMetadataList []artifactapi.RegistryArtifactMetadata
if artifacts == nil {
artifactMetadataList = make([]artifactapi.RegistryArtifactMetadata, 0)
} else {
artifactMetadataList = GetRegistryArtifactMetadata(*artifacts)
}
pageCount := GetPageCount(count, pageSize)
listArtifact := &artifactapi.ListRegistryArtifact{
ItemCount: &count,
PageCount: &pageCount,
PageIndex: &pageNumber,
PageSize: &pageSize,
Artifacts: artifactMetadataList,
}
response := &artifactapi.ListRegistryArtifactResponseJSONResponse{
Data: *listArtifact,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetAllArtifactLabelsResponse(
artifactLabels *[]string,
count int64,
pageNumber int64,
pageSize int,
) *artifactapi.ListArtifactLabelResponseJSONResponse {
pageCount := GetPageCount(count, pageSize)
listArtifactLabels := &artifactapi.ListArtifactLabel{
ItemCount: &count,
PageCount: &pageCount,
PageIndex: &pageNumber,
PageSize: &pageSize,
Labels: *artifactLabels,
}
response := &artifactapi.ListArtifactLabelResponseJSONResponse{
Data: *listArtifactLabels,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetAllArtifactVersionResponse(
ctx context.Context,
tags *[]types.TagMetadata,
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,
)
pageCount := GetPageCount(count, pageSize)
listArtifactVersions := &artifactapi.ListArtifactVersion{
ItemCount: &count,
PageCount: &pageCount,
PageIndex: &pageNumber,
PageSize: &pageSize,
ArtifactVersions: &artifactVersionMetadataList,
}
response := &artifactapi.ListArtifactVersionResponseJSONResponse{
Data: *listArtifactVersions,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetDockerArtifactDetails(
registry *types.Registry,
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)
createdAt := GetTimeInMs(tag.CreatedAt)
modifiedAt := GetTimeInMs(tag.UpdatedAt)
size := GetSize(manifest.TotalSize)
artifactDetail := &artifactapi.DockerArtifactDetail{
ImageName: tag.ImageName,
Version: tag.Name,
PackageType: registry.PackageType,
IsLatestVersion: &isLatestTag,
CreatedAt: &createdAt,
ModifiedAt: &modifiedAt,
RegistryPath: repoPath,
PullCommand: &pullCommand,
Url: GetTagURL(regInfo.RootIdentifier, tag.ImageName, tag.Name, registry.Name, registryURL),
Size: &size,
}
response := &artifactapi.DockerArtifactDetailResponseJSONResponse{
Data: *artifactDetail,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetHelmArtifactDetails(
registry *types.Registry,
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)
createdAt := GetTimeInMs(tag.CreatedAt)
modifiedAt := GetTimeInMs(tag.UpdatedAt)
size := GetSize(manifest.TotalSize)
artifactDetail := &artifactapi.HelmArtifactDetail{
Artifact: &tag.ImageName,
Version: tag.Name,
PackageType: registry.PackageType,
IsLatestVersion: &isLatestTag,
CreatedAt: &createdAt,
ModifiedAt: &modifiedAt,
RegistryPath: repoPath,
PullCommand: &pullCommand,
Url: GetTagURL(rootIdentifier, tag.ImageName, tag.Name, registry.Name, registryURL),
Size: &size,
}
response := &artifactapi.HelmArtifactDetailResponseJSONResponse{
Data: *artifactDetail,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetArtifactSummary(artifact types.ArtifactMetadata) *artifactapi.ArtifactSummaryResponseJSONResponse {
downloads := int64(0)
createdAt := GetTimeInMs(artifact.CreatedAt)
modifiedAt := GetTimeInMs(artifact.ModifiedAt)
artifactVersionSummary := &artifactapi.ArtifactSummary{
CreatedAt: &createdAt,
ModifiedAt: &modifiedAt,
DownloadsCount: &downloads,
ImageName: artifact.Name,
Labels: &artifact.Labels,
PackageType: artifact.PackageType,
}
response := &artifactapi.ArtifactSummaryResponseJSONResponse{
Data: *artifactVersionSummary,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func GetArtifactVersionSummary(
tag *types.TagMetadata,
artifactName string,
isLatestTag bool,
) *artifactapi.ArtifactVersionSummaryResponseJSONResponse {
artifactVersionSummary := &artifactapi.ArtifactVersionSummary{
ImageName: artifactName,
IsLatestVersion: &isLatestTag,
PackageType: tag.PackageType,
Version: tag.Name,
}
response := &artifactapi.ArtifactVersionSummaryResponseJSONResponse{
Data: *artifactVersionSummary,
Status: artifactapi.StatusSUCCESS,
}
return response
}
func getRepoPath(registry string, image string, tag string) string {
return filepath.Join(registry, image, tag)
}