feat: [AH-295]: add STO details in AR summary api - refactor code to reuse in harness-code, update router (#2705)

* feat: [AH-295]: add STO details in AR summary api - rebase
* feat: [AH-295]: add STO details in AR summary api - handle errors
* feat: [AH-295]: add STO details in AR summary api - lint error
* feat: [AH-295]: add STO details in AR summary api - refactor code to reuse in harness-code, update router
* feat: [AH-295]: add STO details in AR summary api - refactor code to reuse in harness-code, update router
This commit is contained in:
Shivakumar Ningappa 2024-09-24 23:57:06 +00:00 committed by Harness
parent 115050d4dd
commit eea514eb4f

View File

@ -16,11 +16,13 @@ package metadata
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
apiauth "github.com/harness/gitness/app/api/auth" apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/request" "github.com/harness/gitness/app/api/request"
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact" "github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
"github.com/harness/gitness/registry/types"
"github.com/harness/gitness/types/enum" "github.com/harness/gitness/types/enum"
) )
@ -28,15 +30,34 @@ func (c *APIController) GetArtifactVersionSummary(
ctx context.Context, ctx context.Context,
r artifact.GetArtifactVersionSummaryRequestObject, r artifact.GetArtifactVersionSummaryRequestObject,
) (artifact.GetArtifactVersionSummaryResponseObject, error) { ) (artifact.GetArtifactVersionSummaryResponseObject, error) {
regInfo, _ := c.GetRegistryRequestBaseInfo(ctx, "", string(r.RegistryRef)) image, tag, isLatestTag, err := c.FetchArtifactSummary(ctx, r)
if err != nil {
return artifact.GetArtifactVersionSummary500JSONResponse{
InternalServerErrorJSONResponse: artifact.InternalServerErrorJSONResponse(
*GetErrorResponse(http.StatusInternalServerError, err.Error()),
),
}, nil
}
return artifact.GetArtifactVersionSummary200JSONResponse{
ArtifactVersionSummaryResponseJSONResponse: *GetArtifactVersionSummary(tag, image, isLatestTag),
}, nil
}
// FetchArtifactSummary helper function for common logic.
func (c *APIController) FetchArtifactSummary(
ctx context.Context,
r artifact.GetArtifactVersionSummaryRequestObject,
) (string, *types.TagMetadata, bool, error) {
regInfo, err := c.GetRegistryRequestBaseInfo(ctx, "", string(r.RegistryRef))
if err != nil {
return "", nil, false, fmt.Errorf("failed to get registry request base info: %w", err)
}
space, err := c.SpaceStore.FindByRef(ctx, regInfo.ParentRef) space, err := c.SpaceStore.FindByRef(ctx, regInfo.ParentRef)
if err != nil { if err != nil {
return artifact.GetArtifactVersionSummary400JSONResponse{ return "", nil, false, err
BadRequestJSONResponse: artifact.BadRequestJSONResponse(
*GetErrorResponse(http.StatusBadRequest, err.Error()),
),
}, nil
} }
session, _ := request.AuthSessionFrom(ctx) session, _ := request.AuthSessionFrom(ctx)
@ -47,31 +68,19 @@ func (c *APIController) GetArtifactVersionSummary(
session, session,
permissionChecks..., permissionChecks...,
); err != nil { ); err != nil {
return artifact.GetArtifactVersionSummary403JSONResponse{ return "", nil, false, err
UnauthorizedJSONResponse: artifact.UnauthorizedJSONResponse(
*GetErrorResponse(http.StatusForbidden, err.Error()),
),
}, nil
} }
image := string(r.Artifact) image := string(r.Artifact)
version := string(r.Version) version := string(r.Version)
tag, err := c.TagStore.GetTagMetadata(ctx, regInfo.parentID, regInfo.RegistryIdentifier, image, version) tag, err := c.TagStore.GetTagMetadata(ctx, regInfo.parentID, regInfo.RegistryIdentifier, image, version)
if err != nil { if err != nil {
return artifact.GetArtifactVersionSummary500JSONResponse{ return "", nil, false, err
InternalServerErrorJSONResponse: artifact.InternalServerErrorJSONResponse(
*GetErrorResponse(http.StatusInternalServerError, err.Error()),
),
}, nil
} }
latestTag, _ := c.TagStore.GetLatestTagName(ctx, regInfo.parentID, regInfo.RegistryIdentifier, image) latestTag, _ := c.TagStore.GetLatestTagName(ctx, regInfo.parentID, regInfo.RegistryIdentifier, image)
isLatestTag := latestTag == version isLatestTag := latestTag == version
return artifact.GetArtifactVersionSummary200JSONResponse{ return image, tag, isLatestTag, nil
ArtifactVersionSummaryResponseJSONResponse: *GetArtifactVersionSummary(tag, image, isLatestTag),
}, nil
} }