drone/registry/app/api/utils/path_utils.go
Pragyesh Mishra c91ba3d8d3 [fix]: [AH-1262]: Fix get files api for maven when artifact id contains dot (#3730)
* [fix]: [AH-1262]: Fix get files api when artifact id contains dot
* [fix]: [AH-1262]: Fix get files api when artifact id contains dot
* [fix]: [AH-1262]: Fix get files api when artifact id contains dot
2025-04-30 13:53:49 +00:00

77 lines
2.3 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 utils
import (
"fmt"
"strings"
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
)
func GetMavenFilePath(imageName string, version string) string {
parts := strings.SplitN(imageName, ":", 2)
filePathPrefix := "/"
if len(parts) == 2 {
groupID := strings.ReplaceAll(parts[0], ".", "/")
filePathPrefix += groupID + "/" + parts[1]
}
if version != "" {
filePathPrefix += "/" + version
}
return filePathPrefix
}
func GetGenericFilePath(imageName string, version string) string {
filePathPrefix := "/" + imageName
if version != "" {
filePathPrefix += "/" + version
}
return filePathPrefix
}
func GetRpmFilePath(imageName string, version string) string {
lastDotIndex := strings.LastIndex(version, ".")
rpmVersion := version[:lastDotIndex]
rpmArch := version[lastDotIndex+1:]
return "/" + imageName + "/" + rpmVersion + "/" + rpmArch
}
func GetFilePath(
packageType artifact.PackageType,
imageName string, version string,
) (string, error) {
switch packageType {
case artifact.PackageTypeDOCKER:
return "", fmt.Errorf("docker package type not supported")
case artifact.PackageTypeHELM:
return "", fmt.Errorf("helm package type not supported")
case artifact.PackageTypeNPM:
return GetGenericFilePath(imageName, version), nil
case artifact.PackageTypeMAVEN:
return GetMavenFilePath(imageName, version), nil
case artifact.PackageTypePYTHON:
return GetGenericFilePath(imageName, version), nil
case artifact.PackageTypeGENERIC:
return GetGenericFilePath(imageName, version), nil
case artifact.PackageTypeNUGET:
return "", fmt.Errorf("nuget package type not supported")
case artifact.PackageTypeRPM:
return GetRpmFilePath(imageName, version), nil
default:
return "", fmt.Errorf("unsupported package type: %s", packageType)
}
}