Skip to content

Commit

Permalink
fix release sorting for docs link detection
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Sep 29, 2024
1 parent eac6efe commit 49100b6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ dump-swagger: build

.PHONY: render
render: build quickrender
hack/tidy-html.sh

.PHONY: quickrender
quickrender:
ASSET_STAMP=$(GIT_HEAD) _build/render
hack/tidy-html.sh

.PHONY: deploy
deploy:
Expand Down
3 changes: 2 additions & 1 deletion pkg/apidocs/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
"sort"
"strings"

"github.com/Masterminds/sprig/v3"
"go.xrstf.de/kube-api.ninja/pkg/apidocs/api"

"github.com/Masterminds/sprig/v3"
)

//go:embed templates/*
Expand Down
19 changes: 18 additions & 1 deletion pkg/timeline/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func CreateTimeline(releases []*database.KubernetesRelease, now time.Time) (*Tim
return version.CompareAPIVersions(apiGroup.APIVersions[j].Version, apiGroup.APIVersions[i].Version)
})

apiGroup.ReleasesOfInterest = version.SortReleases(apiGroup.ReleasesOfInterest)

timeline.APIGroups[idx] = apiGroup
}

Expand Down Expand Up @@ -165,6 +167,21 @@ func mergeAPIGroupOverviews(dest *APIGroup, groupinfo *types.APIGroup, groupName
}
}

// version-sort the .Releases on each created APIResource
for i, apiVersion := range dest.APIVersions {
apiVersion.Releases = version.SortReleases(apiVersion.Releases)
apiVersion.ReleasesOfInterest = version.SortReleases(apiVersion.ReleasesOfInterest)

dest.APIVersions[i] = apiVersion

for j, apiResource := range apiVersion.Resources {
apiResource.Releases = version.SortReleases(apiResource.Releases)
apiResource.ReleasesOfInterest = version.SortReleases(apiResource.ReleasesOfInterest)

dest.APIVersions[i].Resources[j] = apiResource
}
}

return nil
}

Expand Down Expand Up @@ -212,7 +229,7 @@ func mergeAPIResourceOverviews(dest *APIResource, resourceinfo *types.Resource,
dest.Plural = resourceinfo.Plural
dest.Singular = resourceinfo.Singular
dest.Description = resourceinfo.Description
dest.Releases = sets.List(sets.New(dest.Releases...).Insert(release))
dest.Releases = sets.New(dest.Releases...).Insert(release).UnsortedList()

// remember the scope, which _could_ technically change between versions and/or releases
if dest.Scopes == nil {
Expand Down
34 changes: 34 additions & 0 deletions pkg/version/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

import (
"fmt"
"slices"

kversion "k8s.io/apimachinery/pkg/util/version"
)
Expand Down Expand Up @@ -32,3 +33,36 @@ func (s *Semver) LessThan(other *Semver) bool {
func (s *Semver) MajorMinor() string {
return fmt.Sprintf("%d.%d", s.v.Major(), s.v.Minor())
}

func SortReleases(releases []string) []string {
releases = slices.Clone(releases)
slices.SortFunc(releases, func(a, b string) int {
compared, err := kversion.MustParseGeneric(a).Compare(b)
if err != nil {
panic(err)
}

return compared
})

return releases
}

func Sort(versions []string) []string {
versions = slices.Clone(versions)
slices.SortFunc(versions, func(a, b string) int {
aVersion, err := ParseSemver(a)
if err != nil {
panic(err)
}

compared, err := aVersion.v.Compare(b)
if err != nil {
panic(err)
}

return compared
})

return versions
}

0 comments on commit 49100b6

Please sign in to comment.