@@ -3,6 +3,7 @@ package model
33import (
44 "errors"
55 "fmt"
6+ "slices"
67 "sort"
78 "strings"
89
@@ -103,24 +104,24 @@ func (m *Package) Validate() error {
103104}
104105
105106func (m * Package ) validateUniqueBundleVersions () error {
106- versionsMap := map [string ]semver. Version {}
107+ versionsMap := map [string ]string {}
107108 bundlesWithVersion := map [string ]sets.Set [string ]{}
108109 for _ , ch := range m .Channels {
109110 for _ , b := range ch .Bundles {
110- versionsMap [b .Version . String ()] = b .Version
111- if bundlesWithVersion [b .Version . String ()] == nil {
112- bundlesWithVersion [b .Version . String ()] = sets .New [string ]()
111+ versionsMap [b .VersionString ()] = b .VersionString ()
112+ if bundlesWithVersion [b .VersionString ()] == nil {
113+ bundlesWithVersion [b .VersionString ()] = sets .New [string ]()
113114 }
114- bundlesWithVersion [b .Version . String ()].Insert (b .Name )
115+ bundlesWithVersion [b .VersionString ()].Insert (b .Name )
115116 }
116117 }
117118
118119 versionsSlice := maps .Values (versionsMap )
119- semver .Sort (versionsSlice )
120+ slices .Sort (versionsSlice )
120121
121122 var errs []error
122123 for _ , v := range versionsSlice {
123- bundles := sets .List (bundlesWithVersion [v . String () ])
124+ bundles := sets .List (bundlesWithVersion [v ])
124125 if len (bundles ) > 1 {
125126 errs = append (errs , fmt .Errorf ("{%s: [%s]}" , v , strings .Join (bundles , ", " )))
126127 }
@@ -327,6 +328,47 @@ type Bundle struct {
327328 // These fields are used to compare bundles in a diff.
328329 PropertiesP * property.Properties
329330 Version semver.Version
331+ Release property.Release
332+ }
333+
334+ func (b * Bundle ) VersionString () string {
335+ if b .Release .Label != "" || (b .Release .Version .Major != 0 || b .Release .Version .Minor != 0 || b .Release .Version .Patch != 0 ) {
336+ return strings .Join ([]string {b .Version .String (), b .Release .String ()}, "-" )
337+ } else {
338+ return b .Version .String ()
339+ }
340+ }
341+
342+ func (b * Bundle ) normalizeName () string {
343+ // if the bundle has release versioning, then the name must include this in standard form:
344+ // <package-name>-<version>-<release label>-<release version>
345+ // if no release versioning exists, then just return the bundle name
346+ if b .Release .Label != "" || (b .Release .Version .Major != 0 || b .Release .Version .Minor != 0 || b .Release .Version .Patch != 0 ) {
347+ return strings .Join ([]string {b .Package .Name , b .Version .String (), b .Release .String ()}, "-" )
348+ } else {
349+ return b .Name
350+ }
351+ }
352+
353+ // order by release, if present
354+ // - label first, if present;
355+ // - then version, if present;
356+ //
357+ // then version
358+ func (b * Bundle ) Compare (other * Bundle ) int {
359+ if b .Name == other .Name {
360+ return 0
361+ }
362+ if b .Release .Label != other .Release .Label {
363+ return strings .Compare (b .Release .Label , other .Release .Label )
364+ }
365+ if b .Release .Version .NE (other .Release .Version ) {
366+ return b .Release .Version .Compare (other .Release .Version )
367+ }
368+ if b .Version .NE (other .Version ) {
369+ return b .Version .Compare (other .Version )
370+ }
371+ return 0
330372}
331373
332374func (b * Bundle ) Validate () error {
@@ -335,6 +377,9 @@ func (b *Bundle) Validate() error {
335377 if b .Name == "" {
336378 result .subErrors = append (result .subErrors , errors .New ("name must be set" ))
337379 }
380+ if b .Name != b .normalizeName () {
381+ result .subErrors = append (result .subErrors , fmt .Errorf ("name %q does not match normalized name %q" , b .Name , b .normalizeName ()))
382+ }
338383 if b .Channel == nil {
339384 result .subErrors = append (result .subErrors , errors .New ("channel must be set" ))
340385 }
0 commit comments