Skip to content

Commit

Permalink
feat: handle obsolete versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 17, 2023
1 parent c5cc65b commit cbc6d7b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
47 changes: 44 additions & 3 deletions menu/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
stateLatest = "LATEST"
stateExperimental = "EXPERIMENTAL"
statePreFinalRelease = "PRE_FINAL_RELEASE"
stateObsolete = "OBSOLETE"
)

type optionVersion struct {
Expand Down Expand Up @@ -87,14 +88,15 @@ func buildVersions(currentVersion string, branches []string, latestTagName, expe
return nil, fmt.Errorf("failed to parse latest tag version %s: %w", latestTagName, err)
}

rawVersions, heads := parseBranches(branches)

var versions []optionVersion
for _, branch := range branches {
versionName := strings.Replace(branch, baseRemote, "", 1)
for _, versionName := range rawVersions {
selected := currentVersion == versionName

switch versionName {
case latestTagName:
// skip, because we must the branch instead of the tag
// skip, because we must use the branch instead of the tag
case experimentalBranchName:
versions = append(versions, optionVersion{
Path: experimentalBranchName,
Expand All @@ -103,6 +105,7 @@ func buildVersions(currentVersion string, branches []string, latestTagName, expe
State: stateExperimental,
Selected: selected,
})

default:
simpleVersion, err := version.NewVersion(versionName)
if err != nil {
Expand All @@ -126,17 +129,55 @@ func buildVersions(currentVersion string, branches []string, latestTagName, expe
default:
v.Path = versionName
v.Text = versionName
if !isHeads(heads, simpleVersion) {
v.State = stateObsolete
}
}

versions = append(versions, v)
}
}

return versions, nil
}

func parseBranches(branches []string) ([]string, map[int]*version.Version) {
heads := map[int]*version.Version{}

var rawVersions []string
for _, branch := range branches {
versionName := strings.Replace(branch, baseRemote, "", 1)
rawVersions = append(rawVersions, versionName)

v, err := version.NewVersion(versionName)
if err != nil {
continue
}

if p, ok := heads[v.Segments()[0]]; ok {
if v.GreaterThan(p) {
heads[v.Segments()[0]] = v
}
} else {
heads[v.Segments()[0]] = v
}
}
return rawVersions, heads
}

func sameMinor(v1, v2 *version.Version) bool {
v1Parts := v1.Segments()
v2Parts := v2.Segments()

return v1Parts[0] == v2Parts[0] && v1Parts[1] == v2Parts[1]
}

func isHeads(heads map[int]*version.Version, v *version.Version) bool {
for _, head := range heads {
if v.Equal(head) {
return true
}
}

return false
}
16 changes: 16 additions & 0 deletions menu/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,26 @@ func Test_buildVersions(t *testing.T) {
Path: "v1.3",
Text: "v1.3",
Name: "v1.3",
State: stateObsolete,
Selected: false,
},
},
},
{
desc: "all v2",
branches: []string{"origin/v2.9", "origin/v2.8", "origin/master", "origin/v1.7", "v1.4.6", "origin/v1.4"},
latestTagName: "v2.9.0",
experimentalBranchName: "master",
currentVersion: "v1.4",
expected: []optionVersion{
{Path: "", Text: "v2.9 Latest", Name: "v2.9", State: "LATEST", Selected: false},
{Path: "v2.8", Text: "v2.8", Name: "v2.8", State: stateObsolete, Selected: false},
{Path: "master", Text: "Experimental", Name: "master", State: "EXPERIMENTAL", Selected: false},
{Path: "v1.7", Text: "v1.7", Name: "v1.7", State: "", Selected: false},
{Path: "v1.4.6", Text: "v1.4.6", Name: "v1.4.6", State: stateObsolete, Selected: false},
{Path: "v1.4", Text: "v1.4", Name: "v1.4", State: stateObsolete, Selected: true},
},
},
}

for _, test := range testCases {
Expand Down
14 changes: 14 additions & 0 deletions traefik-menu.js.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ var versions = [
{{- end}}
];

{{- range $version := .Versions }}
{{ if and (eq $version.Name $.Current) (eq $version.State "OBSOLETE") }}
function addBannerMaterial() {
// TODO
}

function addBannerUnited() {
// TODO
}
{{- end}}
{{- end}}

// Material theme

function addMaterialMenu(elt, versions) {
Expand Down Expand Up @@ -119,7 +131,9 @@ const materialSelector = 'div.md-container main.md-main div.md-main__inner.md-gr
let elt = document.querySelector(materialSelector);
if (elt) {
addMaterialMenu(elt, versions);
addBannerMaterial();
} else {
const elt = document.querySelector(unitedSelector);
addMenu(elt, versions);
addBannerUnited();
}

0 comments on commit cbc6d7b

Please sign in to comment.