Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apptrust/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
SourceTypeArtifactsFlag = "source-type-artifacts"
PropertiesFlag = "properties"
DeletePropertiesFlag = "delete-properties"
IncludeFilterFlag = "include-filter"
ExcludeFilterFlag = "exclude-filter"
)

// Flag keys mapped to their corresponding components.Flag definition.
Expand Down Expand Up @@ -85,6 +87,8 @@ var flagsMap = map[string]components.Flag{
SourceTypeReleaseBundlesFlag: components.NewStringFlag(SourceTypeReleaseBundlesFlag, "List of semicolon-separated (;) release bundles in the form of 'name=releaseBundleName1, version=version1[, project-key=project1][, repo-key=repo1]; name=releaseBundleName2, version=version2[, project-key=project2][, repo-key=repo2]' to be included in the new version.", func(f *components.StringFlag) { f.Mandatory = false }),
SourceTypeApplicationVersionsFlag: components.NewStringFlag(SourceTypeApplicationVersionsFlag, "List of semicolon-separated (;) application versions in the form of 'application-key=app1, version=version1; application-key=app2, version=version2' to be included in the new version.", func(f *components.StringFlag) { f.Mandatory = false }),
SourceTypePackagesFlag: components.NewStringFlag(SourceTypePackagesFlag, "List of semicolon-separated (;) packages in the form of 'type=packageType1, name=packageName1, version=version1, repo-key=repo1; type=packageType2, name=packageName2, version=version2, repo-key=repo2' to be included in the new version.", func(f *components.StringFlag) { f.Mandatory = false }),
IncludeFilterFlag: components.NewStringFlag(IncludeFilterFlag, "List of semicolon-separated (;) filters of packages and artifacts in the form of 'filter1; filter2...' to be included in the new version. Each filter must be comma-separated: 'filter_type=package/artifact, field1=value1[, field2=value2...]'. Package filters require at least one of: 'type', 'name', or 'version'. Artifact filters require at least one of: 'path' or 'sha256'.", func(f *components.StringFlag) { f.Mandatory = false }),
ExcludeFilterFlag: components.NewStringFlag(ExcludeFilterFlag, "List of semicolon-separated (;) filters of packages and artifacts in the form of 'filter1; filter2...' to be included in the new version. Each filter must be comma-separated: 'filter_type=package/artifact, field1=value1[, field2=value2...]'. Package filters require at least one of: 'type', 'name', or 'version'. Artifact filters require at least one of: 'path' or 'sha256'.", func(f *components.StringFlag) { f.Mandatory = false }),
SourceTypeArtifactsFlag: components.NewStringFlag(SourceTypeArtifactsFlag, "List of semicolon-separated (;) artifacts in the form of 'path=repo/path/to/artifact1[, sha256=hash1]; path=repo/path/to/artifact2[, sha256=hash2]' to be included in the new version.", func(f *components.StringFlag) { f.Mandatory = false }),
PropertiesFlag: components.NewStringFlag(PropertiesFlag, "Sets or updates custom properties for the application version in format 'key1=value1[,value2,...];key2=value3[,value4,...]'", func(f *components.StringFlag) { f.Mandatory = false }),
DeletePropertiesFlag: components.NewStringFlag(DeletePropertiesFlag, "Remove a property key and all its values", func(f *components.StringFlag) { f.Mandatory = false }),
Expand All @@ -103,6 +107,8 @@ var commandFlags = map[string][]string{
SourceTypePackagesFlag,
SourceTypeArtifactsFlag,
SpecFlag,
IncludeFilterFlag,
ExcludeFilterFlag,
SpecVarsFlag,
},
VersionPromote: {
Expand Down
113 changes: 106 additions & 7 deletions apptrust/commands/version/create_app_version_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type createVersionSpec struct {
Builds []model.CreateVersionBuild `json:"builds,omitempty"`
ReleaseBundles []model.CreateVersionReleaseBundle `json:"release_bundles,omitempty"`
Versions []model.CreateVersionReference `json:"versions,omitempty"`
Filters *model.CreateVersionFilters `json:"filters,omitempty"`
}

func (cv *createAppVersionCommand) Run() error {
Expand Down Expand Up @@ -72,13 +73,18 @@ func (cv *createAppVersionCommand) prepareAndRunCommand(ctx *components.Context)
func (cv *createAppVersionCommand) buildRequestPayload(ctx *components.Context) (*model.CreateAppVersionRequest, error) {
var (
sources *model.CreateVersionSources
filters *model.CreateVersionFilters
err error
)

if ctx.IsFlagSet(commands.SpecFlag) {
sources, err = cv.loadFromSpec(ctx)
sources, filters, err = cv.loadFromSpec(ctx)
} else {
sources, err = cv.buildSourcesFromFlags(ctx)
if err != nil {
return nil, err
}
filters, err = cv.buildFiltersFromFlags(ctx)
}

if err != nil {
Expand All @@ -90,6 +96,7 @@ func (cv *createAppVersionCommand) buildRequestPayload(ctx *components.Context)
Version: ctx.Arguments[1],
Sources: sources,
Tag: ctx.GetStringFlagValue(commands.TagFlag),
Filters: filters,
}, nil
}

Expand Down Expand Up @@ -133,13 +140,13 @@ func (cv *createAppVersionCommand) buildSourcesFromFlags(ctx *components.Context
return sources, nil
}

func (cv *createAppVersionCommand) loadFromSpec(ctx *components.Context) (*model.CreateVersionSources, error) {
func (cv *createAppVersionCommand) loadFromSpec(ctx *components.Context) (*model.CreateVersionSources, *model.CreateVersionFilters, error) {
specFilePath := ctx.GetStringFlagValue(commands.SpecFlag)
spec := new(createVersionSpec)
specVars := coreutils.SpecVarsStringToMap(ctx.GetStringFlagValue(commands.SpecVarsFlag))
content, err := fileutils.ReadFile(specFilePath)
if errorutils.CheckError(err) != nil {
return nil, err
return nil, nil, err
}

if len(specVars) > 0 {
Expand All @@ -148,12 +155,12 @@ func (cv *createAppVersionCommand) loadFromSpec(ctx *components.Context) (*model

err = json.Unmarshal(content, spec)
if errorutils.CheckError(err) != nil {
return nil, err
return nil, nil, err
}

// Validation: if all sources are empty, return error
if (len(spec.Packages) == 0) && (len(spec.Builds) == 0) && (len(spec.ReleaseBundles) == 0) && (len(spec.Versions) == 0) && (len(spec.Artifacts) == 0) {
return nil, errorutils.CheckErrorf("Spec file is empty: must provide at least one source (artifacts, packages, builds, release_bundles, or versions)")
return nil, nil, errorutils.CheckErrorf("Spec file is empty: must provide at least one source (artifacts, packages, builds, release_bundles, or versions)")
}

sources := &model.CreateVersionSources{
Expand All @@ -164,7 +171,7 @@ func (cv *createAppVersionCommand) loadFromSpec(ctx *components.Context) (*model
Versions: spec.Versions,
}

return sources, nil
return sources, spec.Filters, nil
}

func (cv *createAppVersionCommand) parseBuilds(buildsStr string) ([]model.CreateVersionBuild, error) {
Expand Down Expand Up @@ -310,6 +317,92 @@ func (cv *createAppVersionCommand) parseArtifacts(artifactsStr string) ([]model.
return artifacts, nil
}

func (cv *createAppVersionCommand) buildFiltersFromFlags(ctx *components.Context) (*model.CreateVersionFilters, error) {
includeFilterValues := ctx.GetStringsArrFlagValue(commands.IncludeFilterFlag)
excludeFilterValues := ctx.GetStringsArrFlagValue(commands.ExcludeFilterFlag)

if len(includeFilterValues) == 0 && len(excludeFilterValues) == 0 {
return nil, nil
}
filters := &model.CreateVersionFilters{}
if includedFilters, err := cv.parseFilterValues(includeFilterValues); err != nil {
return nil, err
} else if len(includedFilters) > 0 {
filters.Included = includedFilters
}
if excludedFilters, err := cv.parseFilterValues(excludeFilterValues); err != nil {
return nil, err
} else if len(excludedFilters) > 0 {
filters.Excluded = excludedFilters
}

return filters, nil
}

func (cv *createAppVersionCommand) parseFilterValues(filterValues []string) ([]*model.CreateVersionSourceFilter, error) {
if len(filterValues) == 0 {
return nil, nil
}
return cv.parseFilters(filterValues)
}

func (cv *createAppVersionCommand) parseFilters(filterStrings []string) ([]*model.CreateVersionSourceFilter, error) {
const (
filterTypeField = "filter_type"
packageTypeField = "type"
packageNameField = "name"
packageVersionField = "version"
artifactPathField = "path"
artifactShaField = "sha256"
)

var filters []*model.CreateVersionSourceFilter

for i, filterStr := range filterStrings {
filterMap, err := utils.ParseKeyValueString(filterStr, ",")
if err != nil {
return nil, errorutils.CheckErrorf("invalid filter format at index %d: %v", i, err)
}
filterType, ok := filterMap[filterTypeField]
if !ok {
return nil, errorutils.CheckErrorf("invalid filter format at index %d: missing 'filter_type' field", i)
}
filter := &model.CreateVersionSourceFilter{}

switch filterType {
case "package":
if val, ok := filterMap[packageTypeField]; ok {
filter.PackageType = val
}
if val, ok := filterMap[packageNameField]; ok {
filter.PackageName = val
}
if val, ok := filterMap[packageVersionField]; ok {
filter.PackageVersion = val
}
if filter.PackageType == "" && filter.PackageName == "" && filter.PackageVersion == "" {
return nil, errorutils.CheckErrorf("invalid package filter at index %d: at least one of 'type', 'name', or 'version' must be specified", i)
}
case "artifact":
if val, ok := filterMap[artifactPathField]; ok {
filter.Path = val
}
if val, ok := filterMap[artifactShaField]; ok {
filter.SHA256 = val
}
if filter.Path == "" && filter.SHA256 == "" {
return nil, errorutils.CheckErrorf("invalid artifact filter at index %d: at least one of 'path' or 'sha256' must be specified", i)
}
default:
return nil, errorutils.CheckErrorf("invalid filter_type '%s' at index %d: must be 'package' or 'artifact'", filterType, i)
}

filters = append(filters, filter)
}

return filters, nil
}

func validateCreateAppVersionContext(ctx *components.Context) error {
if err := validateNoSpecAndFlagsTogether(ctx); err != nil {
return err
Expand Down Expand Up @@ -358,7 +451,7 @@ func GetCreateAppVersionCommand(appContext app.Context) components.Command {
}
}

// Returns error if both --spec and any other source flag are set
// Returns error if both --spec and any other source flag or filter flag are set
func validateNoSpecAndFlagsTogether(ctx *components.Context) error {
if ctx.IsFlagSet(commands.SpecFlag) {
otherSourceFlags := []string{
Expand All @@ -373,6 +466,12 @@ func validateNoSpecAndFlagsTogether(ctx *components.Context) error {
return errorutils.CheckErrorf("--spec provided: all other source flags (e.g., --%s) are not allowed.", flag)
}
}
if ctx.IsFlagSet(commands.IncludeFilterFlag) {
return errorutils.CheckErrorf("--spec provided: filter flags (e.g., --%s) are not allowed.", commands.IncludeFilterFlag)
}
if ctx.IsFlagSet(commands.ExcludeFilterFlag) {
return errorutils.CheckErrorf("--spec provided: filter flags (e.g., --%s) are not allowed.", commands.ExcludeFilterFlag)
}
}
return nil
}
Expand Down
Loading
Loading