feat: [AH-895]: Added support for upstream registries to not show push commands (#3564)

* [AH-895]: Added support for upstream registries to not show push commands
This commit is contained in:
Arvind Choudhary 2025-03-18 15:56:22 +00:00 committed by Harness
parent 70823cc3b4
commit e6ac0ef996

View File

@ -26,6 +26,8 @@ import (
"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/app/common" "github.com/harness/gitness/registry/app/common"
"github.com/harness/gitness/types/enum" "github.com/harness/gitness/types/enum"
"github.com/rs/zerolog/log"
) )
func (c *APIController) GetClientSetupDetails( func (c *APIController) GetClientSetupDetails(
@ -74,10 +76,20 @@ func (c *APIController) GetClientSetupDetails(
packageType := string(reg.PackageType) packageType := string(reg.PackageType)
response := c.GenerateClientSetupDetails(
ctx, packageType, imageParam, tagParam, regInfo.RegistryRef,
regInfo.RegistryType,
)
if response == nil {
return artifact.GetClientSetupDetails400JSONResponse{
BadRequestJSONResponse: artifact.BadRequestJSONResponse(
*GetErrorResponse(http.StatusBadRequest, "Package type not supported"),
),
}, nil
}
return artifact.GetClientSetupDetails200JSONResponse{ return artifact.GetClientSetupDetails200JSONResponse{
ClientSetupDetailsResponseJSONResponse: *c.GenerateClientSetupDetails( ClientSetupDetailsResponseJSONResponse: *response,
ctx, packageType, imageParam, tagParam, regInfo.RegistryRef,
),
}, nil }, nil
} }
@ -87,6 +99,7 @@ func (c *APIController) GenerateClientSetupDetails(
image *artifact.ArtifactParam, image *artifact.ArtifactParam,
tag *artifact.VersionParam, tag *artifact.VersionParam,
registryRef string, registryRef string,
registryType artifact.RegistryType,
) *artifact.ClientSetupDetailsResponseJSONResponse { ) *artifact.ClientSetupDetailsResponseJSONResponse {
session, _ := request.AuthSessionFrom(ctx) session, _ := request.AuthSessionFrom(ctx)
username := session.Principal.Email username := session.Principal.Email
@ -96,15 +109,36 @@ func (c *APIController) GenerateClientSetupDetails(
blankString := "" blankString := ""
switch packageType { switch packageType {
case string(artifact.PackageTypeMAVEN): case string(artifact.PackageTypeMAVEN):
return c.generateMavenClientSetupDetail(ctx, image, tag, registryRef, username) return c.generateMavenClientSetupDetail(ctx, image, tag, registryRef, username, registryType)
case string(artifact.PackageTypeHELM): case string(artifact.PackageTypeHELM):
return c.generateHelmClientSetupDetail(ctx, blankString, loginUsernameLabel, loginUsernameValue, return c.generateHelmClientSetupDetail(ctx, blankString, loginUsernameLabel, loginUsernameValue,
loginPasswordLabel, username, registryRef, image, tag) loginPasswordLabel, username, registryRef, image, tag, registryType)
case string(artifact.PackageTypeGENERIC): case string(artifact.PackageTypeGENERIC):
return c.generateGenericClientSetupDetail(ctx, blankString, registryRef, image, tag) return c.generateGenericClientSetupDetail(ctx, blankString, registryRef, image, tag, registryType)
case string(artifact.PackageTypePYTHON): case string(artifact.PackageTypePYTHON):
return c.generatePythonClientSetupDetail(ctx, registryRef, username, image, tag) return c.generatePythonClientSetupDetail(ctx, registryRef, username, image, tag, registryType)
case string(artifact.PackageTypeDOCKER):
return c.generateDockerClientSetupDetail(ctx, blankString, loginUsernameLabel, loginUsernameValue,
loginPasswordLabel, registryType,
username, registryRef, image, tag)
default:
log.Debug().Ctx(ctx).Msgf("Unknown package type for client details: %s", packageType)
return nil
} }
}
func (c *APIController) generateDockerClientSetupDetail(
ctx context.Context,
blankString string,
loginUsernameLabel string,
loginUsernameValue string,
loginPasswordLabel string,
registryType artifact.RegistryType,
username string,
registryRef string,
image *artifact.ArtifactParam,
tag *artifact.VersionParam,
) *artifact.ClientSetupDetailsResponseJSONResponse {
header1 := "Login to Docker" header1 := "Login to Docker"
section1step1Header := "Run this Docker command in your terminal to authenticate the client." section1step1Header := "Run this Docker command in your terminal to authenticate the client."
dockerLoginValue := "docker login <LOGIN_HOSTNAME>" dockerLoginValue := "docker login <LOGIN_HOSTNAME>"
@ -184,14 +218,23 @@ func (c *APIController) GenerateClientSetupDetails(
_ = section3.FromClientSetupStepConfig(artifact.ClientSetupStepConfig{ _ = section3.FromClientSetupStepConfig(artifact.ClientSetupStepConfig{
Steps: &section3Steps, Steps: &section3Steps,
}) })
sections := []artifact.ClientSetupSection{
section1,
section2,
section3,
}
if registryType == artifact.RegistryTypeUPSTREAM {
sections = []artifact.ClientSetupSection{
section1,
section2,
}
}
clientSetupDetails := artifact.ClientSetupDetails{ clientSetupDetails := artifact.ClientSetupDetails{
MainHeader: "Docker Client Setup", MainHeader: "Docker Client Setup",
SecHeader: "Follow these instructions to install/use Docker artifacts or compatible packages.", SecHeader: "Follow these instructions to install/use Docker artifacts or compatible packages.",
Sections: []artifact.ClientSetupSection{ Sections: sections,
section1,
section2,
section3,
},
} }
c.replacePlaceholders(ctx, &clientSetupDetails.Sections, username, registryRef, image, tag, "", "", "") c.replacePlaceholders(ctx, &clientSetupDetails.Sections, username, registryRef, image, tag, "", "", "")
@ -204,8 +247,12 @@ func (c *APIController) GenerateClientSetupDetails(
//nolint:lll //nolint:lll
func (c *APIController) generateGenericClientSetupDetail( func (c *APIController) generateGenericClientSetupDetail(
ctx context.Context, blankString string, ctx context.Context,
registryRef string, image *artifact.ArtifactParam, tag *artifact.VersionParam, blankString string,
registryRef string,
image *artifact.ArtifactParam,
tag *artifact.VersionParam,
registryType artifact.RegistryType,
) *artifact.ClientSetupDetailsResponseJSONResponse { ) *artifact.ClientSetupDetailsResponseJSONResponse {
header1 := "Generate identity token" header1 := "Generate identity token"
section1Header := "An identity token will serve as the password for uploading and downloading artifact." section1Header := "An identity token will serve as the password for uploading and downloading artifact."
@ -267,14 +314,23 @@ func (c *APIController) generateGenericClientSetupDetail(
Steps: &section3steps, Steps: &section3steps,
}) })
sections := []artifact.ClientSetupSection{
section1,
section2,
section3,
}
if registryType == artifact.RegistryTypeUPSTREAM {
sections = []artifact.ClientSetupSection{
section1,
section3,
}
}
clientSetupDetails := artifact.ClientSetupDetails{ clientSetupDetails := artifact.ClientSetupDetails{
MainHeader: "Generic Client Setup", MainHeader: "Generic Client Setup",
SecHeader: "Follow these instructions to install/use Generic artifacts or compatible packages.", SecHeader: "Follow these instructions to install/use Generic artifacts or compatible packages.",
Sections: []artifact.ClientSetupSection{ Sections: sections,
section1,
section2,
section3,
},
} }
//nolint:lll //nolint:lll
c.replacePlaceholders(ctx, &clientSetupDetails.Sections, "", registryRef, image, tag, "", "", c.replacePlaceholders(ctx, &clientSetupDetails.Sections, "", registryRef, image, tag, "", "",
@ -296,6 +352,7 @@ func (c *APIController) generateHelmClientSetupDetail(
registryRef string, registryRef string,
image *artifact.ArtifactParam, image *artifact.ArtifactParam,
tag *artifact.VersionParam, tag *artifact.VersionParam,
registryType artifact.RegistryType,
) *artifact.ClientSetupDetailsResponseJSONResponse { ) *artifact.ClientSetupDetailsResponseJSONResponse {
header1 := "Login to Helm" header1 := "Login to Helm"
section1step1Header := "Run this Helm command in your terminal to authenticate the client." section1step1Header := "Run this Helm command in your terminal to authenticate the client."
@ -368,14 +425,23 @@ func (c *APIController) generateHelmClientSetupDetail(
_ = section3.FromClientSetupStepConfig(artifact.ClientSetupStepConfig{ _ = section3.FromClientSetupStepConfig(artifact.ClientSetupStepConfig{
Steps: &section3Steps, Steps: &section3Steps,
}) })
sections := []artifact.ClientSetupSection{
section1,
section2,
section3,
}
if registryType == artifact.RegistryTypeUPSTREAM {
sections = []artifact.ClientSetupSection{
section1,
section3,
}
}
clientSetupDetails := artifact.ClientSetupDetails{ clientSetupDetails := artifact.ClientSetupDetails{
MainHeader: "Helm Client Setup", MainHeader: "Helm Client Setup",
SecHeader: "Follow these instructions to install/use Helm artifacts or compatible packages.", SecHeader: "Follow these instructions to install/use Helm artifacts or compatible packages.",
Sections: []artifact.ClientSetupSection{ Sections: sections,
section1,
section2,
section3,
},
} }
c.replacePlaceholders(ctx, &clientSetupDetails.Sections, username, registryRef, image, tag, "", "", "") c.replacePlaceholders(ctx, &clientSetupDetails.Sections, username, registryRef, image, tag, "", "", "")
@ -392,6 +458,7 @@ func (c *APIController) generateMavenClientSetupDetail(
version *artifact.VersionParam, version *artifact.VersionParam,
registryRef string, registryRef string,
username string, username string,
registryType artifact.RegistryType,
) *artifact.ClientSetupDetailsResponseJSONResponse { ) *artifact.ClientSetupDetailsResponseJSONResponse {
staticStepType := artifact.ClientSetupStepTypeStatic staticStepType := artifact.ClientSetupStepTypeStatic
generateTokenStepType := artifact.ClientSetupStepTypeGenerateToken generateTokenStepType := artifact.ClientSetupStepTypeGenerateToken
@ -652,31 +719,35 @@ func (c *APIController) generateMavenClientSetupDetail(
}) })
section2 := artifact.ClientSetupSection{} section2 := artifact.ClientSetupSection{}
_ = section2.FromTabSetupStepConfig(artifact.TabSetupStepConfig{ config := artifact.TabSetupStepConfig{
Tabs: &[]artifact.TabSetupStep{ Tabs: &[]artifact.TabSetupStep{
{ {
Header: stringPtr("Maven"), Header: stringPtr("Maven"),
Sections: &[]artifact.ClientSetupSection{ Sections: &[]artifact.ClientSetupSection{
mavenSection1, mavenSection1,
mavenSection2,
}, },
}, },
{ {
Header: stringPtr("Gradle"), Header: stringPtr("Gradle"),
Sections: &[]artifact.ClientSetupSection{ Sections: &[]artifact.ClientSetupSection{
gradleSection1, gradleSection1,
gradleSection2,
}, },
}, },
{ {
Header: stringPtr("Sbt/Scala"), Header: stringPtr("Sbt/Scala"),
Sections: &[]artifact.ClientSetupSection{ Sections: &[]artifact.ClientSetupSection{
sbtSection1, sbtSection1,
sbtSection2,
}, },
}, },
}, },
}) }
if registryType == artifact.RegistryTypeVIRTUAL {
for i, remoteSection := range []artifact.ClientSetupSection{mavenSection2, gradleSection2, sbtSection2} {
*(*config.Tabs)[i].Sections = append(*(*config.Tabs)[i].Sections, remoteSection)
}
}
_ = section2.FromTabSetupStepConfig(config)
clientSetupDetails := artifact.ClientSetupDetails{ clientSetupDetails := artifact.ClientSetupDetails{
MainHeader: "Maven Client Setup", MainHeader: "Maven Client Setup",
@ -714,6 +785,7 @@ func (c *APIController) generatePythonClientSetupDetail(
username string, username string,
image *artifact.ArtifactParam, image *artifact.ArtifactParam,
tag *artifact.VersionParam, tag *artifact.VersionParam,
registryType artifact.RegistryType,
) *artifact.ClientSetupDetailsResponseJSONResponse { ) *artifact.ClientSetupDetailsResponseJSONResponse {
staticStepType := artifact.ClientSetupStepTypeStatic staticStepType := artifact.ClientSetupStepTypeStatic
generateTokenType := artifact.ClientSetupStepTypeGenerateToken generateTokenType := artifact.ClientSetupStepTypeGenerateToken
@ -784,14 +856,23 @@ func (c *APIController) generatePythonClientSetupDetail(
}, },
}) })
sections := []artifact.ClientSetupSection{
section1,
section2,
section3,
}
if registryType == artifact.RegistryTypeUPSTREAM {
sections = []artifact.ClientSetupSection{
section1,
section3,
}
}
clientSetupDetails := artifact.ClientSetupDetails{ clientSetupDetails := artifact.ClientSetupDetails{
MainHeader: "Python Client Setup", MainHeader: "Python Client Setup",
SecHeader: "Follow these instructions to install/use Python packages from this registry.", SecHeader: "Follow these instructions to install/use Python packages from this registry.",
Sections: []artifact.ClientSetupSection{ Sections: sections,
section1,
section2,
section3,
},
} }
registryURL := c.URLProvider.PackageURL(ctx, registryRef, "python") registryURL := c.URLProvider.PackageURL(ctx, registryRef, "python")