Skip to content

Commit 3221444

Browse files
committed
fix(): Use a reducer to get the latest version
Modify how `reqcheck vcpkg` determines the latest version. Use a reducer that accumulates the latest version released for the library. Remove the duplicate tag code since its redundant now.
1 parent 66c2c87 commit 3221444

3 files changed

Lines changed: 37 additions & 35 deletions

File tree

cmd/reqcheck/vcpkg.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/Masterminds/semver"
1717
"github.com/WebKitForWindows/reqcheck"
18+
"github.com/reactivex/rxgo/v2"
1819
"github.com/sirupsen/logrus"
1920
"github.com/urfave/cli/v2"
2021
"gopkg.in/yaml.v3"
@@ -134,26 +135,21 @@ func vcpkgCmd() *cli.Command {
134135
LimitTo: library.LimitTo,
135136
}
136137

137-
releases, err := reqcheck.
138-
ListReleases(scm, releaseOpts).
139-
Filter(reqcheck.FilterRepeatVersions()).
138+
latestRelease, err := reqcheck.ListReleases(scm, releaseOpts).
140139
Filter(reqcheck.FilterSemanticConstraint(constraint)).
141-
ToSlice(1)
142-
if err != nil {
140+
Reduce(reqcheck.ReduceGreatestVersion).
141+
Get()
142+
if err != nil || latestRelease == rxgo.OptionalSingleEmpty {
143143
return fmt.Errorf("could not get releases %w", err)
144144
}
145145

146-
if len(releases) == 0 {
147-
return fmt.Errorf("could not find any releases for %s: %w", name, ErrCli)
148-
}
149-
150146
release := releaseUpdate{
151147
Name: name,
152148
Current: version,
153-
Upgrade: releases[0].(reqcheck.Release).SemVer.String(),
149+
Upgrade: latestRelease.V.(reqcheck.Release).SemVer.String(),
154150
}
155151

156-
if len(releases) == 1 {
152+
if release.Current == release.Upgrade {
157153
current = append(current, release)
158154
} else {
159155
upgrade = append(upgrade, release)

filter.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
package reqcheck
66

7-
import (
8-
"slices"
9-
10-
"github.com/Masterminds/semver"
11-
)
7+
import "github.com/Masterminds/semver"
128

139
func FilterSemanticVersion(item interface{}) bool {
1410
release := item.(Release)
@@ -25,25 +21,6 @@ func FilterStableRelease(item interface{}) bool {
2521
return release.SemVer.Prerelease() == ""
2622
}
2723

28-
func FilterRepeatVersions() func(interface{}) bool {
29-
seen := make([]string, 0)
30-
return func(item interface{}) bool {
31-
release := item.(Release)
32-
if release.SemVer == nil {
33-
return true
34-
}
35-
36-
versionString := release.SemVer.String()
37-
38-
if slices.Contains(seen, versionString) {
39-
return false
40-
}
41-
42-
seen = append(seen, versionString)
43-
return true
44-
}
45-
}
46-
4724
func FilterSemanticConstraint(c *semver.Constraints) func(interface{}) bool {
4825
return func(item interface{}) bool {
4926
release := item.(Release)

reduce.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2024, the WebKit for Windows project authors. Please see the
2+
// AUTHORS file for details. All rights reserved. Use of this source code is
3+
// governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
package reqcheck
6+
7+
import "context"
8+
9+
func ReduceGreatestVersion(_ context.Context, acc interface{}, elem interface{}) (interface{}, error) {
10+
if acc == nil {
11+
return elem, nil
12+
}
13+
14+
accRelease := acc.(Release)
15+
if accRelease.SemVer == nil {
16+
return elem, nil
17+
}
18+
19+
elemRelease := elem.(Release)
20+
if elemRelease.SemVer == nil {
21+
return acc, nil
22+
}
23+
24+
if accRelease.SemVer.GreaterThan(elemRelease.SemVer) {
25+
return acc, nil
26+
}
27+
28+
return elem, nil
29+
}

0 commit comments

Comments
 (0)