@@ -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 }
@@ -331,6 +332,47 @@ type Bundle struct {
331332 // These fields are used to compare bundles in a diff.
332333 PropertiesP * property.Properties
333334 Version semver.Version
335+ Release property.Release
336+ }
337+
338+ func (b * Bundle ) VersionString () string {
339+ if b .Release .Label != "" || (b .Release .Version .Major != 0 || b .Release .Version .Minor != 0 || b .Release .Version .Patch != 0 ) {
340+ return strings .Join ([]string {b .Version .String (), b .Release .String ()}, "-" )
341+ } else {
342+ return b .Version .String ()
343+ }
344+ }
345+
346+ func (b * Bundle ) normalizeName () string {
347+ // if the bundle has release versioning, then the name must include this in standard form:
348+ // <package-name>-<version>-<release label>-<release version>
349+ // if no release versioning exists, then just return the bundle name
350+ if b .Release .Label != "" || (b .Release .Version .Major != 0 || b .Release .Version .Minor != 0 || b .Release .Version .Patch != 0 ) {
351+ return strings .Join ([]string {b .Package .Name , b .Version .String (), b .Release .String ()}, "-" )
352+ } else {
353+ return b .Name
354+ }
355+ }
356+
357+ // order by release, if present
358+ // - label first, if present;
359+ // - then version, if present;
360+ //
361+ // then version
362+ func (b * Bundle ) Compare (other * Bundle ) int {
363+ if b .Name == other .Name {
364+ return 0
365+ }
366+ if b .Release .Label != other .Release .Label {
367+ return strings .Compare (b .Release .Label , other .Release .Label )
368+ }
369+ if b .Release .Version .NE (other .Release .Version ) {
370+ return b .Release .Version .Compare (other .Release .Version )
371+ }
372+ if b .Version .NE (other .Version ) {
373+ return b .Version .Compare (other .Version )
374+ }
375+ return 0
334376}
335377
336378func (b * Bundle ) Validate () error {
@@ -339,6 +381,9 @@ func (b *Bundle) Validate() error {
339381 if b .Name == "" {
340382 result .subErrors = append (result .subErrors , errors .New ("name must be set" ))
341383 }
384+ if b .Name != b .normalizeName () {
385+ result .subErrors = append (result .subErrors , fmt .Errorf ("name %q does not match normalized name %q" , b .Name , b .normalizeName ()))
386+ }
342387 if b .Channel == nil {
343388 result .subErrors = append (result .subErrors , errors .New ("channel must be set" ))
344389 }
0 commit comments