drone/registry/app/api/controller/metadata/get_artifacts_summary.go
Tudor Macari c58fdbc4d0 fix: [AH-1153]: fix download count per image and per manifest (#3609)
* fix: [AH-1153]: fix query
* fix: [AH-1153]: fix download count per image and per manifest
2025-04-07 18:46:44 +00:00

128 lines
3.9 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"
"net/http"
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/request"
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
"github.com/harness/gitness/registry/types"
"github.com/harness/gitness/types/enum"
)
func (c *APIController) GetArtifactSummary(
ctx context.Context,
r artifact.GetArtifactSummaryRequestObject,
) (artifact.GetArtifactSummaryResponseObject, error) {
regInfo, err := c.RegistryMetadataHelper.GetRegistryRequestBaseInfo(ctx, "", string(r.RegistryRef))
if err != nil {
return artifact.GetArtifactSummary400JSONResponse{
BadRequestJSONResponse: artifact.BadRequestJSONResponse(
*GetErrorResponse(http.StatusBadRequest, err.Error()),
),
}, nil
}
space, err := c.SpaceFinder.FindByRef(ctx, regInfo.ParentRef)
if err != nil {
return artifact.GetArtifactSummary400JSONResponse{
BadRequestJSONResponse: artifact.BadRequestJSONResponse(
*GetErrorResponse(http.StatusBadRequest, err.Error()),
),
}, nil
}
session, _ := request.AuthSessionFrom(ctx)
permissionChecks := c.RegistryMetadataHelper.GetPermissionChecks(space, regInfo.RegistryIdentifier,
enum.PermissionRegistryView)
if err = apiauth.CheckRegistry(
ctx,
c.Authorizer,
session,
permissionChecks...,
); err != nil {
return artifact.GetArtifactSummary403JSONResponse{
UnauthorizedJSONResponse: artifact.UnauthorizedJSONResponse(
*GetErrorResponse(http.StatusForbidden, err.Error()),
),
}, nil
}
image := string(r.Artifact)
registry, err := c.RegistryRepository.Get(ctx, regInfo.RegistryID)
if err != nil {
return artifact.GetArtifactSummary500JSONResponse{
InternalServerErrorJSONResponse: artifact.InternalServerErrorJSONResponse(
*GetErrorResponse(http.StatusInternalServerError, err.Error()),
),
}, nil
}
metadata, err := c.getImageMetadata(ctx, registry, image)
if err != nil {
return artifact.GetArtifactSummary500JSONResponse{
InternalServerErrorJSONResponse: artifact.InternalServerErrorJSONResponse(
*GetErrorResponse(http.StatusInternalServerError, err.Error()),
),
}, nil
}
return artifact.GetArtifactSummary200JSONResponse{
ArtifactSummaryResponseJSONResponse: *GetArtifactSummary(*metadata),
}, nil
}
func (c *APIController) getImageMetadata(
ctx context.Context,
registry *types.Registry,
image string,
) (*types.ImageMetadata, error) {
img, err := c.ImageStore.GetByName(ctx, registry.ID, image)
if err != nil {
return nil, err
}
downloadCount, err := c.DownloadStatRepository.GetTotalDownloadsForImage(ctx, img.ID)
if err != nil {
return nil, err
}
imgMetadata := &types.ImageMetadata{
Name: image,
DownloadCount: downloadCount,
RepoName: registry.Name,
PackageType: registry.PackageType,
CreatedAt: img.CreatedAt,
}
if registry.PackageType == artifact.PackageTypeDOCKER || registry.PackageType == artifact.PackageTypeHELM {
latestTag, err := c.TagStore.GetLatestTag(ctx, registry.ID, image)
if err != nil {
return nil, err
}
imgMetadata.LatestVersion = latestTag.Name
imgMetadata.ModifiedAt = latestTag.UpdatedAt
} else {
latestArtifact, err := c.ArtifactStore.GetLatestByImageID(ctx, img.ID)
if err != nil {
return nil, err
}
imgMetadata.LatestVersion = latestArtifact.Version
imgMetadata.ModifiedAt = latestArtifact.UpdatedAt
}
return imgMetadata, nil
}